Compare commits
No commits in common. "710aa535623866d6c48cd322c03eeccef45dde44" and "c1cb14b938015cc985d3baf7227c55e1abc295cd" have entirely different histories.
710aa53562
...
c1cb14b938
@ -5,7 +5,6 @@ import (
|
||||
"go-wechat/model"
|
||||
plugin "go-wechat/plugin"
|
||||
"go-wechat/plugin/plugins"
|
||||
"go-wechat/service"
|
||||
)
|
||||
|
||||
// Plugin
|
||||
@ -25,8 +24,8 @@ func Plugin() {
|
||||
|
||||
// 私聊指令消息
|
||||
dispatcher.RegisterHandler(func(m *model.Message) bool {
|
||||
// 私聊消息 或 群聊艾特机器人并且以/开头的消息
|
||||
return (m.IsPrivateText() || (m.IsAt() && m.CleanContentStartWith("/"))) && service.CheckIsEnableCommand(m.FromUser)
|
||||
// 私聊消息直接进去
|
||||
return m.IsPrivateText()
|
||||
}, plugins.Command)
|
||||
|
||||
// AI消息插件
|
||||
|
@ -3,7 +3,6 @@ package model
|
||||
import (
|
||||
"encoding/xml"
|
||||
"go-wechat/types"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -100,28 +99,3 @@ 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,7 +4,6 @@ import (
|
||||
"go-wechat/plugin"
|
||||
"go-wechat/plugin/plugins/command"
|
||||
"go-wechat/utils"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@ -12,27 +11,13 @@ 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(content, "/") {
|
||||
if !strings.HasPrefix(m.Content, "/") {
|
||||
return
|
||||
}
|
||||
|
||||
// 用空格分割消息,下标0表示指令
|
||||
msgArray := strings.Split(content, " ")
|
||||
msgArray := strings.Split(m.Content, " ")
|
||||
cmd := msgArray[0]
|
||||
|
||||
switch cmd {
|
||||
|
@ -10,14 +10,11 @@ import (
|
||||
// @description: 肯德基疯狂星期四文案
|
||||
// @param userId string 发信人
|
||||
func KfcCrazyThursdayCmd(userId string) {
|
||||
// 随机选一个接口调用
|
||||
// 接口调用
|
||||
str := kfcApi1()
|
||||
if str == "" {
|
||||
str = kfcApi2()
|
||||
}
|
||||
if str == "" {
|
||||
str = kfcApi3()
|
||||
}
|
||||
if str == "" {
|
||||
str = "文案获取失败"
|
||||
}
|
||||
@ -67,29 +64,3 @@ 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,13 +50,3 @@ 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