1
0
forked from lxh/go-wxhelper

chore(go-wxhelper): add AI command functionality 🤖

Add AI command functionality to handle AI commands in the WeChat plugin. Includes options to enable or disable AI features.
This commit is contained in:
李寻欢 2024-04-12 10:48:46 +08:00
parent f775f1d67d
commit 3da8b327d0
5 changed files with 78 additions and 3 deletions

View File

@ -42,6 +42,8 @@ func Command(m *plugin.MessageContext) {
command.LeiGodCmd(m.FromUser, msgArray[1], msgArray[2:]...)
case "/肯德基", "/kfc":
command.KfcCrazyThursdayCmd(m.FromUser)
case "/ai":
command.AiCmd(m.FromUser, m.GroupUser, msgArray[1])
default:
utils.SendMessage(m.FromUser, m.GroupUser, "指令错误", 0)
}

View File

@ -0,0 +1,65 @@
package command
import (
"fmt"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/utils"
"log"
"strings"
)
// AiCmd
// @description: AI指令
// @param userId
// @param groupUserId
// @param cmd
func AiCmd(userId, groupUserId, cmd string) {
// 判断发信人是不是群主
can := false
if strings.Contains(userId, "@chatroom") {
// 判断是不是群主
err := client.MySQL.Model(&entity.GroupUser{}).
Where("group_id = ?", userId).
Where("wxid = ?", groupUserId).
Pluck("is_admin", &can).Error
if err != nil {
log.Printf("查询群主失败: %v", err)
return
}
}
if !can {
utils.SendMessage(userId, groupUserId, "您不是群主,无法使用指令", 0)
return
}
var err error
replyMsg := "操作成功"
switch cmd {
case "enable", "启用", "打开":
err = setAiEnable(userId, true)
case "disable", "停用", "禁用", "关闭":
err = setAiEnable(userId, false)
default:
replyMsg = "指令错误"
}
if err != nil {
log.Printf("AI指令执行失败: %v", err)
replyMsg = fmt.Sprintf("指令执行错误: %v", err)
}
utils.SendMessage(userId, groupUserId, replyMsg, 0)
}
// setAiEnable
// @description: 设置AI启用状态
// @param userId
// @param enable
// @return err
func setAiEnable(userId string, enable bool) (err error) {
// 更新
err = client.MySQL.Model(&entity.Friend{}).
Where("wxid = ?", userId).
Update("enable_ai", enable).Error
return
}

View File

@ -23,6 +23,13 @@ option: 指令选项,可选值:
#2. 肯德基疯狂星期四文案
/kfc/肯德基
#3. AI助手
/ai option
option: 指令选项可选值:
启用: '启用'、'打开'、'enable'
停用: '停用'、'禁用'、'关闭'、'disable'
`
utils.SendMessage(m.FromUser, m.GroupUser, str, 0)

View File

@ -70,9 +70,6 @@ services:
image: mysql:8
container_name: gw-db
restart: unless-stopped
depends_on:
wechat:
condition: service_healthy
environment:
- MYSQL_ROOT_PASSWORD=wechat
- MYSQL_USER=wechat
@ -88,6 +85,7 @@ services:
restart: unless-stopped
depends_on:
- mysql
- wechat
volumes:
# 配置文件请参阅项目根目录的config.yaml文件
- ./config/config.yaml:/app/config.yaml

View File

@ -8,6 +8,7 @@ import (
"go-wechat/config"
"go-wechat/entity"
"go-wechat/model"
"go-wechat/utils"
"gorm.io/gorm"
"log"
"slices"
@ -72,6 +73,8 @@ func Sync() {
log.Printf("新增好友失败: %s", err.Error())
continue
}
// 发送一条新消息
utils.SendMessage(friend.Wxid, "", "大家好我是一个AI机器人可以直接@我询问你想问的问题。该功能默认未启用,请群主艾特我并发送 /ai enable 指令启用", 0)
} else {
pm := map[string]any{
"nickname": friend.Nickname,