Merge pull request '✨ 肯德基疯狂星期四文案
指令新增支持群消息' (#4) from dev into main
All checks were successful
BuildImage / build-image (push) Successful in 1m15s
All checks were successful
BuildImage / build-image (push) Successful in 1m15s
Reviewed-on: #4
This commit is contained in:
commit
710aa53562
@ -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消息插件
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user