Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
710aa53562 | ||
|
|
6d127d1492 | ||
|
|
c1cb14b938 | ||
|
|
3fcbbd3308 |
@@ -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消息插件
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package plugins
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go-wechat/plugin"
|
"go-wechat/plugin"
|
||||||
|
"go-wechat/plugin/plugins/command"
|
||||||
"go-wechat/utils"
|
"go-wechat/utils"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,20 +12,36 @@ 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 {
|
||||||
case "/帮助", "/h", "/help", "/?", "/?":
|
case "/帮助", "/h", "/help", "/?", "/?":
|
||||||
helpCmd(m)
|
command.HelpCmd(m)
|
||||||
case "/ls", "/雷神":
|
case "/雷神", "/ls":
|
||||||
leiGodCmd(m.FromUser, msgArray[1], msgArray[2:]...)
|
command.LeiGodCmd(m.FromUser, msgArray[1], msgArray[2:]...)
|
||||||
|
case "/肯德基", "/kfc":
|
||||||
|
command.KfcCrazyThursdayCmd(m.FromUser)
|
||||||
default:
|
default:
|
||||||
utils.SendMessage(m.FromUser, m.GroupUser, "指令错误", 0)
|
utils.SendMessage(m.FromUser, m.GroupUser, "指令错误", 0)
|
||||||
}
|
}
|
||||||
@@ -31,54 +49,3 @@ func Command(m *plugin.MessageContext) {
|
|||||||
// 中止后续消息处理
|
// 中止后续消息处理
|
||||||
m.Abort()
|
m.Abort()
|
||||||
}
|
}
|
||||||
|
|
||||||
// helpCmd
|
|
||||||
// @description: 帮助指令
|
|
||||||
func helpCmd(m *plugin.MessageContext) {
|
|
||||||
str := `帮助菜单:
|
|
||||||
指令消息必须以'/'开头,比如: '/帮助'。
|
|
||||||
支持的指令:
|
|
||||||
|
|
||||||
#1. 雷神加速器
|
|
||||||
/ls option args
|
|
||||||
option: 指令选项,可选值:
|
|
||||||
绑定账户:'绑定'、'b',参数: 账户名 密码 [-f],-f表示强制绑定,非必传项
|
|
||||||
详情: '详情'、'i'
|
|
||||||
暂停: '暂停'、'p'
|
|
||||||
示例: 绑定:
|
|
||||||
/ls 绑定 123456 123456 或者 /ls b 123456 123456
|
|
||||||
`
|
|
||||||
utils.SendMessage(m.FromUser, m.GroupUser, str, 0)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// leiGodCmd
|
|
||||||
// @description: 雷神加速器指令
|
|
||||||
// @param userId
|
|
||||||
// @param cmd
|
|
||||||
// @param args
|
|
||||||
// @return string
|
|
||||||
func leiGodCmd(userId, cmd string, args ...string) {
|
|
||||||
lg := newLeiGod(userId)
|
|
||||||
|
|
||||||
var replyMsg string
|
|
||||||
switch cmd {
|
|
||||||
case "绑定", "b":
|
|
||||||
var force bool
|
|
||||||
if len(args) == 3 && args[2] == "-f" {
|
|
||||||
force = true
|
|
||||||
}
|
|
||||||
replyMsg = lg.binding(args[0], args[1], force)
|
|
||||||
case "详情", "i":
|
|
||||||
replyMsg = lg.info()
|
|
||||||
case "暂停", "p":
|
|
||||||
replyMsg = lg.pause()
|
|
||||||
default:
|
|
||||||
replyMsg = "指令错误"
|
|
||||||
}
|
|
||||||
|
|
||||||
// 返回消息
|
|
||||||
if strings.TrimSpace(replyMsg) != "" {
|
|
||||||
utils.SendMessage(userId, "", replyMsg, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
29
plugin/plugins/command/help.go
Normal file
29
plugin/plugins/command/help.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go-wechat/plugin"
|
||||||
|
"go-wechat/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HelpCmd
|
||||||
|
// @description: 帮助指令
|
||||||
|
func HelpCmd(m *plugin.MessageContext) {
|
||||||
|
str := `帮助菜单:
|
||||||
|
指令消息必须以'/'开头,比如: '/帮助'。
|
||||||
|
支持的指令:
|
||||||
|
|
||||||
|
#1. 雷神加速器
|
||||||
|
/ls option args
|
||||||
|
option: 指令选项,可选值:
|
||||||
|
绑定账户:'绑定'、'b',参数: 账户名 密码 [-f],-f表示强制绑定,非必传项
|
||||||
|
详情: '详情'、'i'
|
||||||
|
暂停: '暂停'、'p'
|
||||||
|
示例: 绑定:
|
||||||
|
/ls 绑定 123456 123456 或者 /ls b 123456 123456
|
||||||
|
|
||||||
|
#2. 肯德基疯狂星期四文案
|
||||||
|
/kfc、/肯德基
|
||||||
|
`
|
||||||
|
utils.SendMessage(m.FromUser, m.GroupUser, str, 0)
|
||||||
|
|
||||||
|
}
|
||||||
95
plugin/plugins/command/kfc.go
Normal file
95
plugin/plugins/command/kfc.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-resty/resty/v2"
|
||||||
|
"go-wechat/utils"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// KfcCrazyThursdayCmd
|
||||||
|
// @description: 肯德基疯狂星期四文案
|
||||||
|
// @param userId string 发信人
|
||||||
|
func KfcCrazyThursdayCmd(userId string) {
|
||||||
|
// 随机选一个接口调用
|
||||||
|
str := kfcApi1()
|
||||||
|
if str == "" {
|
||||||
|
str = kfcApi2()
|
||||||
|
}
|
||||||
|
if str == "" {
|
||||||
|
str = kfcApi3()
|
||||||
|
}
|
||||||
|
if str == "" {
|
||||||
|
str = "文案获取失败"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送消息
|
||||||
|
utils.SendMessage(userId, "", str, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// kfcApi1
|
||||||
|
// @description: 肯德基疯狂星期四文案接口1
|
||||||
|
// @return string
|
||||||
|
func kfcApi1() string {
|
||||||
|
res := resty.New()
|
||||||
|
resp, err := res.R().
|
||||||
|
Post("https://api.jixs.cc/api/wenan-fkxqs/index.php")
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("KFC接口1文案获取失败: %s", err.Error())
|
||||||
|
}
|
||||||
|
log.Printf("KFC接口1文案获取结果: %s", resp.String())
|
||||||
|
return resp.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// kfcApi2
|
||||||
|
// @description: 肯德基疯狂星期四文案接口2
|
||||||
|
// @return string
|
||||||
|
func kfcApi2() string {
|
||||||
|
type result struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Text string `json:"text"`
|
||||||
|
Data struct {
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var resData result
|
||||||
|
|
||||||
|
res := resty.New()
|
||||||
|
resp, err := res.R().
|
||||||
|
SetResult(&resData).
|
||||||
|
Post("https://api.jixs.cc/api/wenan-fkxqs/index.php")
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("KFC接口2文案获取失败: %s", err.Error())
|
||||||
|
}
|
||||||
|
log.Printf("KFC接口2文案获取结果: %s", resp.String())
|
||||||
|
if resData.Data.Msg != "" {
|
||||||
|
return resData.Data.Msg
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package plugins
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"go-wechat/vo"
|
"go-wechat/vo"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"log"
|
"log"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// leiGod
|
// leiGod
|
||||||
@@ -33,6 +34,37 @@ func newLeiGod(userId string) leiGodI {
|
|||||||
return &leiGod{userId: userId}
|
return &leiGod{userId: userId}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LeiGodCmd
|
||||||
|
// @description: 雷神加速器指令
|
||||||
|
// @param userId
|
||||||
|
// @param cmd
|
||||||
|
// @param args
|
||||||
|
// @return string
|
||||||
|
func LeiGodCmd(userId, cmd string, args ...string) {
|
||||||
|
lg := newLeiGod(userId)
|
||||||
|
|
||||||
|
var replyMsg string
|
||||||
|
switch cmd {
|
||||||
|
case "绑定", "b":
|
||||||
|
var force bool
|
||||||
|
if len(args) == 3 && args[2] == "-f" {
|
||||||
|
force = true
|
||||||
|
}
|
||||||
|
replyMsg = lg.binding(args[0], args[1], force)
|
||||||
|
case "详情", "i":
|
||||||
|
replyMsg = lg.info()
|
||||||
|
case "暂停", "p":
|
||||||
|
replyMsg = lg.pause()
|
||||||
|
default:
|
||||||
|
replyMsg = "指令错误"
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回消息
|
||||||
|
if strings.TrimSpace(replyMsg) != "" {
|
||||||
|
utils.SendMessage(userId, "", replyMsg, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// binding
|
// binding
|
||||||
// @description: 绑定雷神加速器账号
|
// @description: 绑定雷神加速器账号
|
||||||
// @receiver l
|
// @receiver l
|
||||||
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user