From 3da8b327d064c3c98b956e852ab989ecff033bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Fri, 12 Apr 2024 10:48:46 +0800 Subject: [PATCH] =?UTF-8?q?chore(go-wxhelper):=20add=20AI=20command=20func?= =?UTF-8?q?tionality=20=F0=9F=A4=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add AI command functionality to handle AI commands in the WeChat plugin. Includes options to enable or disable AI features. --- plugin/plugins/command.go | 2 ++ plugin/plugins/command/ai.go | 65 ++++++++++++++++++++++++++++++++++ plugin/plugins/command/help.go | 7 ++++ readme.md | 4 +-- tasks/friends/friends.go | 3 ++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 plugin/plugins/command/ai.go diff --git a/plugin/plugins/command.go b/plugin/plugins/command.go index 408efc3a..d3b10823 100644 --- a/plugin/plugins/command.go +++ b/plugin/plugins/command.go @@ -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) } diff --git a/plugin/plugins/command/ai.go b/plugin/plugins/command/ai.go new file mode 100644 index 00000000..db1aa928 --- /dev/null +++ b/plugin/plugins/command/ai.go @@ -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 +} diff --git a/plugin/plugins/command/help.go b/plugin/plugins/command/help.go index c469c363..4ef4448f 100644 --- a/plugin/plugins/command/help.go +++ b/plugin/plugins/command/help.go @@ -23,6 +23,13 @@ option: 指令选项,可选值: #2. 肯德基疯狂星期四文案 /kfc、/肯德基 + +#3. AI助手 +/ai option +option: 指令选项,可选值: + 启用: '启用'、'打开'、'enable' + 停用: '停用'、'禁用'、'关闭'、'disable' + ` utils.SendMessage(m.FromUser, m.GroupUser, str, 0) diff --git a/readme.md b/readme.md index 7ae30c53..467ac9f3 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/tasks/friends/friends.go b/tasks/friends/friends.go index 61cc4fce..6ab5b118 100644 --- a/tasks/friends/friends.go +++ b/tasks/friends/friends.go @@ -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,