From 6d127d1492b3128cb60f08e927c1b60f1a614a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Fri, 22 Dec 2023 09:58:27 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20`=E8=82=AF=E5=BE=B7=E5=9F=BA?= =?UTF-8?q?=E7=96=AF=E7=8B=82=E6=98=9F=E6=9C=9F=E5=9B=9B=E6=96=87=E6=A1=88?= =?UTF-8?q?`=E6=8C=87=E4=BB=A4=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=BE=A4=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- initialization/plugin.go | 5 +++-- model/message.go | 26 ++++++++++++++++++++++++++ plugin/plugins/command.go | 19 +++++++++++++++++-- plugin/plugins/command/kfc.go | 31 ++++++++++++++++++++++++++++++- service/friend.go | 10 ++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) diff --git a/initialization/plugin.go b/initialization/plugin.go index 5340a9a..b65e21e 100644 --- a/initialization/plugin.go +++ b/initialization/plugin.go @@ -5,6 +5,7 @@ import ( "go-wechat/model" plugin "go-wechat/plugin" "go-wechat/plugin/plugins" + "go-wechat/service" ) // Plugin @@ -24,8 +25,8 @@ func Plugin() { // 私聊指令消息 dispatcher.RegisterHandler(func(m *model.Message) bool { - // 私聊消息直接进去 - return m.IsPrivateText() + // 私聊消息 或 群聊艾特机器人并且以/开头的消息 + return (m.IsPrivateText() || (m.IsAt() && m.CleanContentStartWith("/"))) && service.CheckIsEnableCommand(m.FromUser) }, plugins.Command) // AI消息插件 diff --git a/model/message.go b/model/message.go index 1d5cf27..5e3a96f 100644 --- a/model/message.go +++ b/model/message.go @@ -3,6 +3,7 @@ package model import ( "encoding/xml" "go-wechat/types" + "regexp" "strings" ) @@ -99,3 +100,28 @@ func (m Message) IsPrivateText() bool { // 发信人不以@chatroom结尾且消息类型为文本 return !strings.HasSuffix(m.FromUser, "chatroom") && m.Type == types.MsgTypeText } + +// CleanContentStartWith +// @description: 判断是否包含指定消息前缀 +// @receiver m +// @param prefix +// @return bool +func (m Message) CleanContentStartWith(prefix string) bool { + content := m.Content + + // 如果是@消息,过滤掉@的内容 + if m.IsAt() { + re := regexp.MustCompile(`@([^ | ]+)`) + matches := re.FindStringSubmatch(content) + if len(matches) > 0 { + // 过滤掉第一个匹配到的 + content = strings.Replace(content, matches[0], "", 1) + } + } + + // 去掉最前面的空格 + content = strings.TrimLeft(content, " ") + content = strings.TrimLeft(content, " ") + + return strings.HasPrefix(content, prefix) +} diff --git a/plugin/plugins/command.go b/plugin/plugins/command.go index b009fa0..408efc3 100644 --- a/plugin/plugins/command.go +++ b/plugin/plugins/command.go @@ -4,6 +4,7 @@ import ( "go-wechat/plugin" "go-wechat/plugin/plugins/command" "go-wechat/utils" + "regexp" "strings" ) @@ -11,13 +12,27 @@ import ( // @description: 自定义指令 // @param m func Command(m *plugin.MessageContext) { + // 如果是群聊,提取出消息 + content := m.Content + + if m.IsGroup() { + re := regexp.MustCompile(`@([^ | ]+)`) + matches := re.FindStringSubmatch(content) + if len(matches) > 0 { + // 过滤掉第一个匹配到的 + content = strings.Replace(content, matches[0], "", 1) + } + // 去掉最前面的空格 + content = strings.TrimLeft(content, " ") + content = strings.TrimLeft(content, " ") + } // 判断是不是指令 - if !strings.HasPrefix(m.Content, "/") { + if !strings.HasPrefix(content, "/") { return } // 用空格分割消息,下标0表示指令 - msgArray := strings.Split(m.Content, " ") + msgArray := strings.Split(content, " ") cmd := msgArray[0] switch cmd { diff --git a/plugin/plugins/command/kfc.go b/plugin/plugins/command/kfc.go index 5ca4d9f..56a111f 100644 --- a/plugin/plugins/command/kfc.go +++ b/plugin/plugins/command/kfc.go @@ -10,11 +10,14 @@ import ( // @description: 肯德基疯狂星期四文案 // @param userId string 发信人 func KfcCrazyThursdayCmd(userId string) { - // 接口调用 + // 随机选一个接口调用 str := kfcApi1() if str == "" { str = kfcApi2() } + if str == "" { + str = kfcApi3() + } if str == "" { str = "文案获取失败" } @@ -64,3 +67,29 @@ func kfcApi2() string { } return resp.String() } + +// kfcApi3 +// @description: 肯德基疯狂星期四文案接口3 +// @return string +func kfcApi3() string { + type result struct { + Code int `json:"code"` + Msg string `json:"msg"` + Text string `json:"text"` + } + + var resData result + + res := resty.New() + resp, err := res.R(). + SetResult(&resData). + Post("https://api.pearktrue.cn/api/kfc") + if err != nil { + log.Panicf("KFC接口3文案获取失败: %s", err.Error()) + } + log.Printf("KFC接口3文案获取结果: %s", resp.String()) + if resData.Text != "" { + return resData.Text + } + return resp.String() +} diff --git a/service/friend.go b/service/friend.go index 548580c..ce2b92e 100644 --- a/service/friend.go +++ b/service/friend.go @@ -50,3 +50,13 @@ func GetAllEnableChatRank() (records []entity.Friend, err error) { err = client.MySQL.Where("enable_chat_rank = ?", 1).Where("wxid LIKE '%@chatroom'").Find(&records).Error return } + +// CheckIsEnableCommand +// @description: 检查用户是否启用了指令 +// @param userId +// @return flag +func CheckIsEnableCommand(userId string) (flag bool) { + var coo int64 + client.MySQL.Model(&entity.Friend{}).Where("enable_command = 1").Where("wxid = ?", userId).Count(&coo) + return coo > 0 +} -- 2.45.2