From 72dbfa7bcbfd58d13b1663da183aeca8b83f347c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=B5=B7?= Date: Sat, 9 Mar 2024 00:54:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=8C=87=E4=BB=A4=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbase/keyword/model.go | 17 ++++-- dbase/tables/chatroom.go | 6 +- dbase/tables/contact.go | 4 +- dbase/tables/cronjob.go | 2 +- dbase/tables/keyword.go | 15 ++--- dbase/tables/llmodel.go | 6 +- dbase/tables/message.go | 32 +++++----- dbase/tables/profile.go | 20 +++--- dbase/tables/setting.go | 8 +-- httpd/wcfrest/receiver_url.go | 2 - httpd/wcfrest/receiver_ws.go | 2 - public/swagger/swagger.json | 91 +++++++++++++++++----------- wcferry/msg_client.go | 4 +- wclient/robot/common.go | 83 +++++++++++++++++++++++++ wclient/robot/daemon.go | 41 ------------- wclient/robot/handler.go | 81 +++++++++++++++---------- wclient/robot/handler_bad.go | 20 +++--- wclient/robot/handler_help.go | 22 ++++++- wclient/robot/handler_wget.go | 1 + wclient/robot/receiver.go | 42 ------------- webview/src/apps/keyword/create.html | 15 ++++- webview/src/apps/keyword/create.ts | 10 +-- webview/src/apps/keyword/list.html | 6 +- webview/src/apps/keyword/list.ts | 5 +- webview/src/apps/keyword/update.html | 15 ++++- webview/src/apps/keyword/update.ts | 5 +- webview/src/openapi/const.ts | 54 +++++++++-------- webview/src/openapi/wrobot.ts | 16 ++++- 28 files changed, 370 insertions(+), 255 deletions(-) create mode 100644 wclient/robot/common.go delete mode 100644 wclient/robot/daemon.go diff --git a/dbase/keyword/model.go b/dbase/keyword/model.go index e874a0f5..87d75682 100644 --- a/dbase/keyword/model.go +++ b/dbase/keyword/model.go @@ -10,6 +10,7 @@ import ( type CreateParam struct { Rd uint `json:"rd"` + Group string `binding:"required" json:"group"` Roomid string `binding:"required" json:"roomid"` Phrase string `binding:"required" json:"phrase"` Target string `json:"target"` @@ -19,6 +20,7 @@ type CreateParam struct { func Create(data *CreateParam) (uint, error) { item := &tables.Keyword{ + Group: data.Group, Roomid: data.Roomid, Phrase: data.Phrase, Target: data.Target, @@ -42,6 +44,7 @@ func Update(data *UpdateParam) error { Rd: data.Rd, }). Updates(tables.Keyword{ + Group: data.Group, Roomid: data.Roomid, Phrase: data.Phrase, Target: data.Target, @@ -60,6 +63,7 @@ func Replace(data *ReplaceParam) error { item, err := Fetch(&FetchParam{ Rd: data.Rd, + Group: data.Group, Roomid: data.Roomid, Phrase: data.Phrase, }) @@ -79,8 +83,10 @@ func Replace(data *ReplaceParam) error { type FetchParam struct { Rd uint `json:"rd"` + Group string `json:"group"` Roomid string `json:"roomid"` Phrase string `json:"phrase"` + Target string `json:"target"` } func Fetch(data *FetchParam) (*tables.Keyword, error) { @@ -90,6 +96,7 @@ func Fetch(data *FetchParam) (*tables.Keyword, error) { result := dborm.Db. Where(&tables.Keyword{ Rd: data.Rd, + Group: data.Group, Roomid: data.Roomid, Phrase: data.Phrase, }). @@ -114,6 +121,7 @@ func Delete(data *DeleteParam) error { result := dborm.Db. Where(&tables.Keyword{ Rd: data.Rd, + Group: data.Group, Roomid: data.Roomid, Phrase: data.Phrase, }). @@ -126,9 +134,8 @@ func Delete(data *DeleteParam) error { // 获取关键词列表 type FetchAllParam struct { + Group string `json:"group"` Roomid string `json:"roomid"` - Target string `json:"target"` - Level int32 `json:"level"` } func FetchAll(data *FetchAllParam) ([]*tables.Keyword, error) { @@ -137,9 +144,8 @@ func FetchAll(data *FetchAllParam) ([]*tables.Keyword, error) { result := dborm.Db. Where(&tables.Keyword{ + Group: data.Group, Roomid: data.Roomid, - Target: data.Target, - Level: data.Level, }). Find(&items) @@ -158,9 +164,8 @@ func Count(data *CountParam) (int64, error) { result := dborm.Db. Model(&tables.Keyword{}). Where(&tables.Keyword{ + Group: data.Group, Roomid: data.Roomid, - Target: data.Target, - Level: data.Level, }). Count(&count) diff --git a/dbase/tables/chatroom.go b/dbase/tables/chatroom.go index 1c6cecd8..67555ac2 100644 --- a/dbase/tables/chatroom.go +++ b/dbase/tables/chatroom.go @@ -3,10 +3,10 @@ package tables // 群聊配置 type Chatroom struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 - Roomid string `gorm:"uniqueIndex" json:"roomid"` // 群聊 id + Rd uint `json:"rd" gorm:"primaryKey"` // 主键 + Roomid string `json:"roomid" gorm:"uniqueIndex"` // 群聊 id Name string `json:"name"` // 群聊名称 - Level int32 `gorm:"default:-1" json:"level"` // 等级 + Level int32 `json:"level" gorm:"default:-1"` // 等级 Remark string `json:"remark"` // 备注 JoinArgot string `json:"join_argot"` // 加群指令 PatReturn string `json:"pat_return"` // 响应拍拍我 diff --git a/dbase/tables/contact.go b/dbase/tables/contact.go index 4998aded..7c26117c 100644 --- a/dbase/tables/contact.go +++ b/dbase/tables/contact.go @@ -3,8 +3,8 @@ package tables // 联系人 type Contact struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 - Wxid string `gorm:"uniqueIndex json:wxid"` // 微信 id + Rd uint `json:"rd" gorm:"primaryKey"` // 主键 + Wxid string `json:wxid" gorm:"uniqueIndex` // 微信 id Code string `json:"code"` // 微信号 Remark string `json:"remark"` // 备注 Name string `json:"name"` // 微信昵称 diff --git a/dbase/tables/cronjob.go b/dbase/tables/cronjob.go index 1cc99167..64cb91ba 100644 --- a/dbase/tables/cronjob.go +++ b/dbase/tables/cronjob.go @@ -3,7 +3,7 @@ package tables // 计划任务 type Cronjob struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 + Rd uint `json:"rd" gorm:"primaryKey"` // 主键 Name string `json:"name"` // 名称 Second string `json:"second"` // 秒 Minute string `json:"minute"` // 分 diff --git a/dbase/tables/keyword.go b/dbase/tables/keyword.go index d633c7aa..356f6c89 100644 --- a/dbase/tables/keyword.go +++ b/dbase/tables/keyword.go @@ -3,11 +3,12 @@ package tables // 关键词 type Keyword struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 - Roomid string `gorm:"uniqueIndex:r_phrase" json:"roomid"` // 群聊 id - Phrase string `gorm:"uniqueIndex:r_phrase" json:"phrase"` // 短语 - Target string `gorm:"index;default:-" json:"target"` // 目标 - Level int32 `gorm:"default:-1" json:"level"` // 等级 - CreatedAt int64 `json:"created_at"` // 创建时间戳 - UpdatedAt int64 `json:"updated_at"` // 最后更新时间戳 + Rd uint `json:"rd" gorm:"primaryKey"` // 主键 + Group string `json:"group" gorm:"uniqueIndex:t_r_phrase"` // 分组 + Roomid string `json:"roomid" gorm:"uniqueIndex:t_r_phrase;default:-"` // 群聊 id + Phrase string `json:"phrase" gorm:"uniqueIndex:t_r_phrase"` // 短语 + Target string `json:"target"` // 目标 + Level int32 `json:"level" gorm:"default:-1"` // 等级 + CreatedAt int64 `json:"created_at"` // 创建时间戳 + UpdatedAt int64 `json:"updated_at"` // 最后更新时间戳 } diff --git a/dbase/tables/llmodel.go b/dbase/tables/llmodel.go index 606a6097..be068b66 100644 --- a/dbase/tables/llmodel.go +++ b/dbase/tables/llmodel.go @@ -3,9 +3,9 @@ package tables // 大语言模型 type LLModel struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 - Mid string `gorm:"uniqueIndex" json:"mid"` // 模型 Id,用于生成模型切换指令 - Level int32 `gorm:"default:-1" json:"level"` // 等级,用于限制用户访问 + Rd uint `json:"rd" gorm:"primaryKey"` // 主键 + Mid string `json:"mid" gorm:"uniqueIndex"` // 模型 Id,用于生成模型切换指令 + Level int32 `json:"level" gorm:"default:-1"` // 等级,用于限制用户访问 Family string `json:"family"` // 模型家族,用于在指令中描述模型类型 Provider string `json:"provider"` // 服务商 [google, openai, xunfei] Model string `json:"model"` // 模型,必须和服务商对应 diff --git a/dbase/tables/message.go b/dbase/tables/message.go index 309890c4..b2b7e905 100644 --- a/dbase/tables/message.go +++ b/dbase/tables/message.go @@ -3,20 +3,20 @@ package tables // 消息 type Message struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 - Id uint64 `gorm:"uniqueIndex" json:"id"` // 消息 id - IsSelf bool `json:"is_self"` // 是否自己发送的 - IsGroup bool `json:"is_group"` // 是否群消息 - Type uint32 `json:"type"` // 消息类型 - Ts uint32 `json:"ts"` // 消息类型 - Roomid string `json:"roomid"` // 群 id(如果是群消息的话) - Content string `json:"content"` // 消息内容 - Sender string `json:"sender"` // 消息发送者 - Sign string `json:"sign"` // Sign - Thumb string `json:"thumb"` // 缩略图 - Extra string `json:"extra"` // 附加内容 - Xml string `json:"xml"` // 消息 xml - Remark string `json:"remark"` // 备注,非微信字段 - CreatedAt int64 `json:"created_at"` // 创建时间戳 - UpdatedAt int64 `json:"updated_at"` // 最后更新时间戳 + Rd uint `json:"rd" gorm:"primaryKey"` // 主键 + Id uint64 `json:"id" gorm:"uniqueIndex"` // 消息 id + IsSelf bool `json:"is_self"` // 是否自己发送的 + IsGroup bool `json:"is_group"` // 是否群消息 + Type uint32 `json:"type"` // 消息类型 + Ts uint32 `json:"ts"` // 消息类型 + Roomid string `json:"roomid" gorm:"default:-"` // 群 id(如果是群消息的话) + Content string `json:"content"` // 消息内容 + Sender string `json:"sender"` // 消息发送者 + Sign string `json:"sign"` // Sign + Thumb string `json:"thumb"` // 缩略图 + Extra string `json:"extra"` // 附加内容 + Xml string `json:"xml"` // 消息 xml + Remark string `json:"remark"` // 备注,非微信字段 + CreatedAt int64 `json:"created_at"` // 创建时间戳 + UpdatedAt int64 `json:"updated_at"` // 最后更新时间戳 } diff --git a/dbase/tables/profile.go b/dbase/tables/profile.go index bc2973eb..2d5b2d63 100644 --- a/dbase/tables/profile.go +++ b/dbase/tables/profile.go @@ -3,14 +3,14 @@ package tables // 用户配置 type Profile struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 - Wxid string `gorm:"index" json:"wxid"` // 微信 id - Roomid string `gorm:"index" json:"roomid"` // 群聊 id - Level int32 `gorm:"default:-1" json:"level"` // 等级 - Remark string `json:"remark"` // 备注 - AiArgot string `json:"ai_argot"` // 唤醒词 - AiModel string `json:"ai_model"` // 会话模型 - BanExpire int64 `json:"ban_expire"` // 拉黑截止时间 - CreatedAt int64 `json:"created_at"` // 创建时间戳 - UpdatedAt int64 `json:"updated_at"` // 最后更新时间戳 + Rd uint `json:"rd" gorm:"primaryKey" ` // 主键 + Wxid string `json:"wxid" gorm:"index"` // 微信 id + Roomid string `json:"roomid" gorm:"index;default:-"` // 群聊 id + Level int32 `json:"level" gorm:"default:-1"` // 等级 + Remark string `json:"remark"` // 备注 + AiArgot string `json:"ai_argot"` // 唤醒词 + AiModel string `json:"ai_model"` // 会话模型 + BanExpire int64 `json:"ban_expire"` // 拉黑截止时间 + CreatedAt int64 `json:"created_at"` // 创建时间戳 + UpdatedAt int64 `json:"updated_at"` // 最后更新时间戳 } diff --git a/dbase/tables/setting.go b/dbase/tables/setting.go index b7522644..9da38baf 100644 --- a/dbase/tables/setting.go +++ b/dbase/tables/setting.go @@ -3,10 +3,10 @@ package tables // 全局配置 type Setting struct { - Rd uint `gorm:"primaryKey" json:"rd"` // 主键 - Name string `gorm:"uniqueIndex" json:"name"` // 键 - Type string `gorm:"default:string" json:"type"` // 类型 - Group string `gorm:"index" json:"group"` // 分组 + Rd uint `json:"rd" gorm:"primaryKey"` // 主键 + Name string `json:"name" gorm:"uniqueIndex"` // 键 + Type string `json:"type" gorm:"default:string"` // 类型 + Group string `json:"group" gorm:"index"` // 分组 Value string `json:"value"` // 值 Title string `json:"title"` // 标题 Remark string `json:"remark"` // 备注 diff --git a/httpd/wcfrest/receiver_url.go b/httpd/wcfrest/receiver_url.go index 7053edca..26635a8e 100644 --- a/httpd/wcfrest/receiver_url.go +++ b/httpd/wcfrest/receiver_url.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/opentdp/go-helper/logman" - "github.com/opentdp/go-helper/recovery" "github.com/opentdp/go-helper/request" "github.com/opentdp/wechat-rest/wcferry" @@ -23,7 +22,6 @@ func (wc *Controller) enableUrlReceiver(url string) error { if len(urlReceiverList) == 0 { key, err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) { - defer recovery.Handler() ret := wcferry.ParseWxMsg(msg) for u := range urlReceiverList { logman.Info("call receiver", "url", u, "Id", ret.Id) diff --git a/httpd/wcfrest/receiver_ws.go b/httpd/wcfrest/receiver_ws.go index 3027c7a0..8b98b86f 100644 --- a/httpd/wcfrest/receiver_ws.go +++ b/httpd/wcfrest/receiver_ws.go @@ -6,7 +6,6 @@ import ( "github.com/gorilla/websocket" "github.com/opentdp/go-helper/logman" - "github.com/opentdp/go-helper/recovery" "github.com/opentdp/wechat-rest/wcferry" ) @@ -37,7 +36,6 @@ func (wc *Controller) enableSocketReceiver(ws *websocket.Conn) error { if len(socketReceiverList) == 0 { key, err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) { - defer recovery.Handler() ret := wcferry.ParseWxMsg(msg) for s := range socketReceiverList { logman.Info("call receiver", "addr", s.RemoteAddr(), "Id", ret.Id) diff --git a/public/swagger/swagger.json b/public/swagger/swagger.json index 43bd4f40..8ab1fe9d 100644 --- a/public/swagger/swagger.json +++ b/public/swagger/swagger.json @@ -1416,36 +1416,6 @@ } } }, - "/wcf/geocr_result": { - "post": { - "produces": [ - "application/json" - ], - "tags": [ - "WCF::消息收取" - ], - "summary": "获取OCR识别结果", - "parameters": [ - { - "description": "获取OCR识别结果参数", - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/wcfrest.GetOcrRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/wcfrest.CommonPayload" - } - } - } - } - }, "/wcf/invite_chatroom_members": { "post": { "produces": [ @@ -1536,6 +1506,36 @@ } } }, + "/wcf/ocr_result": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "WCF::消息收取" + ], + "summary": "获取OCR识别结果", + "parameters": [ + { + "description": "获取OCR识别结果参数", + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/wcfrest.GetOcrRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/wcfrest.CommonPayload" + } + } + } + } + }, "/wcf/receive_transfer": { "post": { "produces": [ @@ -2118,10 +2118,14 @@ "keyword.CreateParam": { "type": "object", "required": [ + "group", "phrase", "roomid" ], "properties": { + "group": { + "type": "string" + }, "level": { "type": "integer" }, @@ -2142,6 +2146,9 @@ "keyword.DeleteParam": { "type": "object", "properties": { + "group": { + "type": "string" + }, "phrase": { "type": "string" }, @@ -2150,19 +2157,19 @@ }, "roomid": { "type": "string" + }, + "target": { + "type": "string" } } }, "keyword.FetchAllParam": { "type": "object", "properties": { - "level": { - "type": "integer" - }, - "roomid": { + "group": { "type": "string" }, - "target": { + "roomid": { "type": "string" } } @@ -2170,6 +2177,9 @@ "keyword.FetchParam": { "type": "object", "properties": { + "group": { + "type": "string" + }, "phrase": { "type": "string" }, @@ -2178,16 +2188,23 @@ }, "roomid": { "type": "string" + }, + "target": { + "type": "string" } } }, "keyword.UpdateParam": { "type": "object", "required": [ + "group", "phrase", "roomid" ], "properties": { + "group": { + "type": "string" + }, "level": { "type": "integer" }, @@ -2632,6 +2649,10 @@ "description": "创建时间戳", "type": "integer" }, + "group": { + "description": "分组", + "type": "string" + }, "level": { "description": "等级", "type": "integer" diff --git a/wcferry/msg_client.go b/wcferry/msg_client.go index 518f1355..eebd83f8 100644 --- a/wcferry/msg_client.go +++ b/wcferry/msg_client.go @@ -2,6 +2,7 @@ package wcferry import ( "github.com/opentdp/go-helper/logman" + "github.com/opentdp/go-helper/recovery" "github.com/opentdp/go-helper/strutil" ) @@ -45,11 +46,12 @@ func (c *MsgClient) Register(cb MsgCallback) (string, error) { } go func() { defer c.Destroy() + defer recovery.Handler() for len(c.callbacks) > 0 { if resp, err := c.recv(); err == nil { msg := resp.GetWxmsg() for _, f := range c.callbacks { - go f(msg) // 异步处理 + f(msg) // 推送消息 } } else { logman.Error("msg receiver", "error", err) diff --git a/wclient/robot/common.go b/wclient/robot/common.go new file mode 100644 index 00000000..2e93f86e --- /dev/null +++ b/wclient/robot/common.go @@ -0,0 +1,83 @@ +package robot + +import ( + "strings" + + "github.com/opentdp/go-helper/logman" + "github.com/opentdp/wechat-rest/dbase/setting" + "github.com/opentdp/wechat-rest/wcferry" + "github.com/opentdp/wechat-rest/wclient" +) + +var wc *wcferry.Client + +var clientId string +var selfInfo *wcferry.UserInfo + +func Start() { + + setting.Laod() + + if !setting.BotEnable { + logman.Warn("robot disabled") + return + } + + if clientId != "" { + logman.Warn("robot already started") + return + } + + initHandlers() + + wc = wclient.Register() + clientId, _ = wc.EnrollReceiver(true, receiver) + +} + +func Redo() { + + selfInfo = nil + initHandlers() + +} + +// 个人信息 +func self() *wcferry.UserInfo { + + if selfInfo == nil { + selfInfo = wc.CmdClient.GetSelfInfo() + } + return selfInfo + +} + +// 会话场景 +func prid(msg *wcferry.WxMsg) string { + + if msg.IsGroup { + return msg.Roomid + } + return "-" + +} + +// 回复消息 +func reply(msg *wcferry.WxMsg, text string) int32 { + + if msg.IsSelf { + return -2 + } + + if text = strings.TrimSpace(text); text == "" { + return -1 + } + + if msg.IsGroup { + user := wc.CmdClient.GetInfoByWxid(msg.Sender) + return wc.CmdClient.SendTxt("@"+user.Name+"\n"+text, msg.Roomid, msg.Sender) + } else { + return wc.CmdClient.SendTxt(text, msg.Sender, "") + } + +} diff --git a/wclient/robot/daemon.go b/wclient/robot/daemon.go deleted file mode 100644 index f91191ad..00000000 --- a/wclient/robot/daemon.go +++ /dev/null @@ -1,41 +0,0 @@ -package robot - -import ( - "github.com/opentdp/go-helper/logman" - "github.com/opentdp/wechat-rest/dbase/setting" - "github.com/opentdp/wechat-rest/wcferry" - "github.com/opentdp/wechat-rest/wclient" -) - -var wc *wcferry.Client - -var clientId string -var selfInfo *wcferry.UserInfo - -func Start() { - - setting.Laod() - - if !setting.BotEnable { - logman.Warn("robot disabled") - return - } - - if clientId != "" { - logman.Warn("robot already started") - return - } - - initHandlers() - - wc = wclient.Register() - clientId, _ = wc.EnrollReceiver(true, receiver) - -} - -func Redo() { - - selfInfo = nil - initHandlers() - -} diff --git a/wclient/robot/handler.go b/wclient/robot/handler.go index 0d61eeaf..cd29ec7e 100644 --- a/wclient/robot/handler.go +++ b/wclient/robot/handler.go @@ -4,50 +4,63 @@ import ( "sort" "strings" + "github.com/opentdp/wechat-rest/dbase/keyword" "github.com/opentdp/wechat-rest/dbase/profile" "github.com/opentdp/wechat-rest/dbase/setting" "github.com/opentdp/wechat-rest/wcferry" ) +var Handlers = []*Handler{} +var HandlerMap = map[string]*Handler{} + type Handler struct { Level int32 // 0:不限制 7:群管理 9:创始人 Order int32 // 排序,越小越靠前 ChatAble bool // 是否允许在私聊使用 RoomAble bool // 是否允许在群聊使用 Command string // 指令 - Alias string // 指令别名 Describe string // 指令的描述信息 PreCheck func(*wcferry.WxMsg) string // 前置检查,可拦截文本聊天内容 Callback func(*wcferry.WxMsg) string // 指令回调,返回回复内容 } -var Handlers = []*Handler{} -var HandlerMap = map[string]*Handler{} - func initHandlers() { - list := []*Handler{} - lmap := map[string]*Handler{} - - list = append(list, aiHandler()...) - list = append(list, apiHandler()...) - list = append(list, badHandler()...) - list = append(list, banHandler()...) - list = append(list, topHandler()...) - list = append(list, roomHandler()...) - list = append(list, wgetHandler()...) - list = append(list, helpHandler()...) - - // 格式化 - for _, v := range list { - lmap[v.Command] = v - } - sort.Slice(list, func(i, j int) bool { - return list[i].Order < list[j].Order + hlst := []*Handler{} + hmap := map[string]*Handler{} + + // 获取指令列表 + hlst = append(hlst, aiHandler()...) + hlst = append(hlst, apiHandler()...) + hlst = append(hlst, badHandler()...) + hlst = append(hlst, banHandler()...) + hlst = append(hlst, topHandler()...) + hlst = append(hlst, roomHandler()...) + hlst = append(hlst, wgetHandler()...) + hlst = append(hlst, helpHandler()...) + + // 指令列表排序 + sort.Slice(hlst, func(i, j int) bool { + return hlst[i].Order < hlst[j].Order }) - // 更新列表 - Handlers, HandlerMap = list, lmap + // 获取指令映射 + for _, v := range hlst { + hmap[v.Command] = v + } + + // 获取别名数据 + kws, err := keyword.FetchAll(&keyword.FetchAllParam{Group: "handler"}) + if err == nil && len(kws) > 0 { + for _, v := range kws { + if hmap[v.Target] != nil { + hmap[v.Phrase+"@"+v.Roomid] = hmap[v.Target] + } + } + } + + // 更新全局数据 + Handlers, HandlerMap = hlst, hmap } @@ -69,13 +82,19 @@ func applyHandlers(msg *wcferry.WxMsg) string { } // 解析指令 - matches := strings.SplitN(msg.Content, " ", 2) - handler := HandlerMap[matches[0]] + params := strings.SplitN(msg.Content, " ", 2) + handler := HandlerMap[params[0]] if handler == nil { - if msg.Content[0] == '/' { - return setting.InvalidHandler + handler = HandlerMap[params[0]+"@"+prid(msg)] + if handler == nil { + handler = HandlerMap[params[0]+"@-"] + if handler == nil { + if msg.Content[0] == '/' { + return setting.InvalidHandler + } + return "" + } } - return "" } // 验证权限 @@ -98,8 +117,8 @@ func applyHandlers(msg *wcferry.WxMsg) string { } // 重写消息 - if len(matches) == 2 { - msg.Content = strings.TrimSpace(matches[1]) + if len(params) == 2 { + msg.Content = strings.TrimSpace(params[1]) } else { msg.Content = "" } diff --git a/wclient/robot/handler_bad.go b/wclient/robot/handler_bad.go index b2757268..7a55b0d6 100644 --- a/wclient/robot/handler_bad.go +++ b/wclient/robot/handler_bad.go @@ -11,7 +11,7 @@ import ( ) var badMember = map[string]int{} -var keywordList = []*tables.Keyword{} +var badwordList = []*tables.Keyword{} func badHandler() []*Handler { @@ -28,7 +28,7 @@ func badHandler() []*Handler { Describe: "添加违规关键词", Callback: func(msg *wcferry.WxMsg) string { _, err := keyword.Create(&keyword.CreateParam{ - Roomid: prid(msg), Phrase: msg.Content, Target: "ban", Level: 1, + Group: "badword", Roomid: prid(msg), Phrase: msg.Content, Level: 1, }) if err == nil { updateBadWord() @@ -47,14 +47,12 @@ func badHandler() []*Handler { Command: "/unbad", Describe: "删除违规关键词", Callback: func(msg *wcferry.WxMsg) string { - item, err := keyword.Fetch(&keyword.FetchParam{Roomid: prid(msg), Phrase: msg.Content}) - if err != nil || item.Target != "ban" { - return msg.Content + "不是违规关键词" - } - err = keyword.Delete(&keyword.DeleteParam{Rd: item.Rd}) + err := keyword.Delete(&keyword.DeleteParam{ + Group: "badword", Roomid: prid(msg), Phrase: msg.Content, + }) if err == nil { updateBadWord() - return "已删除违规关键词" + msg.Content + return "违规关键词删除成功" } return "违规关键词删除失败" }, @@ -78,7 +76,7 @@ func badPreCheck(msg *wcferry.WxMsg) string { } // 遍历关键词 - for _, v := range keywordList { + for _, v := range badwordList { if v.Roomid != "-" { if msg.IsGroup { if v.Roomid != msg.Roomid { @@ -106,8 +104,8 @@ func badPreCheck(msg *wcferry.WxMsg) string { func updateBadWord() { - keywordList, _ = keyword.FetchAll(&keyword.FetchAllParam{ - Target: "ban", + badwordList, _ = keyword.FetchAll(&keyword.FetchAllParam{ + Group: "badword", }) } diff --git a/wclient/robot/handler_help.go b/wclient/robot/handler_help.go index ec8a13b8..695d5c6b 100644 --- a/wclient/robot/handler_help.go +++ b/wclient/robot/handler_help.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/opentdp/wechat-rest/dbase/keyword" "github.com/opentdp/wechat-rest/dbase/llmodel" "github.com/opentdp/wechat-rest/dbase/profile" "github.com/opentdp/wechat-rest/dbase/setting" @@ -33,6 +34,18 @@ func helpCallback(msg *wcferry.WxMsg) string { up, _ := profile.Fetch(&profile.FetchParam{Wxid: msg.Sender, Roomid: prid(msg)}) + // 别名映射表 + aliasMap := map[string]map[string]string{} + keywords, err := keyword.FetchAll(&keyword.FetchAllParam{Group: "handler"}) + if err == nil { + for _, v := range keywords { + if aliasMap[v.Roomid] == nil { + aliasMap[v.Roomid] = map[string]string{} + } + aliasMap[v.Roomid][v.Target] = v.Phrase + } + } + // 生成指令菜单 helper := []string{} for _, v := range Handlers { @@ -42,8 +55,13 @@ func helpCallback(msg *wcferry.WxMsg) string { } } if (msg.IsGroup && v.RoomAble) || (!msg.IsGroup && v.ChatAble) { - o := fmt.Sprintf("%s %s", v.Command, v.Describe) - helper = append(helper, o) + cmd := v.Command + if aliasMap[msg.Roomid] != nil && aliasMap[msg.Roomid][v.Command] != "" { + cmd = aliasMap[msg.Roomid][v.Command] + } else if aliasMap["-"] != nil && aliasMap["-"][v.Command] != "" { + cmd = aliasMap["-"][v.Command] + } + helper = append(helper, fmt.Sprintf("%s %s", cmd, v.Describe)) } } diff --git a/wclient/robot/handler_wget.go b/wclient/robot/handler_wget.go index bad83f17..a0c8af83 100644 --- a/wclient/robot/handler_wget.go +++ b/wclient/robot/handler_wget.go @@ -15,6 +15,7 @@ func wgetHandler() []*Handler { Order: 90, ChatAble: true, RoomAble: true, + Command: "/wget", Describe: "获取图片或文件", Callback: func(msg *wcferry.WxMsg) string { u := msg.Content diff --git a/wclient/robot/receiver.go b/wclient/robot/receiver.go index 7d8a278b..5a93f693 100644 --- a/wclient/robot/receiver.go +++ b/wclient/robot/receiver.go @@ -1,8 +1,6 @@ package robot import ( - "strings" - "github.com/opentdp/wechat-rest/dbase/chatroom" "github.com/opentdp/wechat-rest/dbase/profile" "github.com/opentdp/wechat-rest/dbase/setting" @@ -32,46 +30,6 @@ func receiver(msg *wcferry.WxMsg) { } -// 个人信息 -func self() *wcferry.UserInfo { - - if selfInfo == nil { - selfInfo = wc.CmdClient.GetSelfInfo() - } - return selfInfo - -} - -// 会话场景 -func prid(msg *wcferry.WxMsg) string { - - if msg.IsGroup { - return msg.Roomid - } - return "-" - -} - -// 回复消息 -func reply(msg *wcferry.WxMsg, text string) int32 { - - if msg.IsSelf { - return -2 - } - - if text = strings.TrimSpace(text); text == "" { - return -1 - } - - if msg.IsGroup { - user := wc.CmdClient.GetInfoByWxid(msg.Sender) - return wc.CmdClient.SendTxt("@"+user.Name+"\n"+text, msg.Roomid, msg.Sender) - } else { - return wc.CmdClient.SendTxt(text, msg.Sender, "") - } - -} - // 白名单限制 // return 验证结果 [true 受限, false 忽略] func whiteLimit(msg *wcferry.WxMsg) bool { diff --git a/webview/src/apps/keyword/create.html b/webview/src/apps/keyword/create.html index 5f9f00c9..8fe27d1f 100644 --- a/webview/src/apps/keyword/create.html +++ b/webview/src/apps/keyword/create.html @@ -17,6 +17,17 @@ 用于违规检测,暂不支持统配符 +
+ + +
+ 关键词分组 +
+
- @for (item of keywordTargets | keyvalue; track item.key) { + + @for (item of commandList | keyvalue; track item.key) { } @@ -43,6 +55,7 @@
+ @for (item of keywordGroups | keyvalue; track item.key) { + + } + +
+ 关键词分组 +
+
- @for (item of keywordTargets | keyvalue; track item.key) { + + @for (item of commandList | keyvalue; track item.key) { } @@ -43,6 +55,7 @@