1
0
forked from lxh/go-wxhelper

Merge pull request ' 肯德基疯狂星期四文案指令新增支持群消息' (#4) from dev into main

Reviewed-on: lxh/go-wxhelper#4
This commit is contained in:
李寻欢 2023-12-22 09:59:16 +08:00
commit 710aa53562
5 changed files with 86 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import (
"go-wechat/model" "go-wechat/model"
plugin "go-wechat/plugin" plugin "go-wechat/plugin"
"go-wechat/plugin/plugins" "go-wechat/plugin/plugins"
"go-wechat/service"
) )
// Plugin // Plugin
@ -24,8 +25,8 @@ func Plugin() {
// 私聊指令消息 // 私聊指令消息
dispatcher.RegisterHandler(func(m *model.Message) bool { dispatcher.RegisterHandler(func(m *model.Message) bool {
// 私聊消息直接进去 // 私聊消息 或 群聊艾特机器人并且以/开头的消息
return m.IsPrivateText() return (m.IsPrivateText() || (m.IsAt() && m.CleanContentStartWith("/"))) && service.CheckIsEnableCommand(m.FromUser)
}, plugins.Command) }, plugins.Command)
// AI消息插件 // AI消息插件

View File

@ -3,6 +3,7 @@ package model
import ( import (
"encoding/xml" "encoding/xml"
"go-wechat/types" "go-wechat/types"
"regexp"
"strings" "strings"
) )
@ -99,3 +100,28 @@ func (m Message) IsPrivateText() bool {
// 发信人不以@chatroom结尾且消息类型为文本 // 发信人不以@chatroom结尾且消息类型为文本
return !strings.HasSuffix(m.FromUser, "chatroom") && m.Type == types.MsgTypeText 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)
}

View File

@ -4,6 +4,7 @@ import (
"go-wechat/plugin" "go-wechat/plugin"
"go-wechat/plugin/plugins/command" "go-wechat/plugin/plugins/command"
"go-wechat/utils" "go-wechat/utils"
"regexp"
"strings" "strings"
) )
@ -11,13 +12,27 @@ import (
// @description: 自定义指令 // @description: 自定义指令
// @param m // @param m
func Command(m *plugin.MessageContext) { 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 return
} }
// 用空格分割消息下标0表示指令 // 用空格分割消息下标0表示指令
msgArray := strings.Split(m.Content, " ") msgArray := strings.Split(content, " ")
cmd := msgArray[0] cmd := msgArray[0]
switch cmd { switch cmd {

View File

@ -10,11 +10,14 @@ import (
// @description: 肯德基疯狂星期四文案 // @description: 肯德基疯狂星期四文案
// @param userId string 发信人 // @param userId string 发信人
func KfcCrazyThursdayCmd(userId string) { func KfcCrazyThursdayCmd(userId string) {
// 接口调用 // 随机选一个接口调用
str := kfcApi1() str := kfcApi1()
if str == "" { if str == "" {
str = kfcApi2() str = kfcApi2()
} }
if str == "" {
str = kfcApi3()
}
if str == "" { if str == "" {
str = "文案获取失败" str = "文案获取失败"
} }
@ -64,3 +67,29 @@ func kfcApi2() string {
} }
return resp.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()
}

View File

@ -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 err = client.MySQL.Where("enable_chat_rank = ?", 1).Where("wxid LIKE '%@chatroom'").Find(&records).Error
return 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
}