From 37bf8de13262afbe218d634b9c213c687b8f6aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 7 Dec 2023 15:58:55 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E8=BF=8E=E6=96=B0?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E5=BC=95=E5=85=A5`=E8=B5=84?= =?UTF-8?q?=E6=BA=90`=E6=A6=82=E5=BF=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/current/robot.go | 19 +++++++++++++ config.yaml | 9 +++--- config/config.go | 9 +++--- config/resource.go | 8 ++++++ handler/parse.go | 1 + handler/sys_message.go | 32 +++++++++++++++++---- initialization/{mysql.go => config.go} | 0 initialization/wechat.go | 31 ++++++++++++++++++++ main.go | 5 ++-- model/userinfo.go | 18 ++++++++++++ utils/send.go | 39 ++++++++++++++++++++++++++ 11 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 common/current/robot.go create mode 100644 config/resource.go rename initialization/{mysql.go => config.go} (100%) create mode 100644 initialization/wechat.go create mode 100644 model/userinfo.go diff --git a/common/current/robot.go b/common/current/robot.go new file mode 100644 index 00000000..b68591df --- /dev/null +++ b/common/current/robot.go @@ -0,0 +1,19 @@ +package current + +import "go-wechat/model" + +var robotInfo model.RobotUserInfo + +// SetRobotInfo +// @description: 设置机器人信息 +// @param info +func SetRobotInfo(info model.RobotUserInfo) { + robotInfo = info +} + +// GetRobotInfo +// @description: 获取机器人信息 +// @return model.RobotUserInfo +func GetRobotInfo() model.RobotUserInfo { + return robotInfo +} diff --git a/config.yaml b/config.yaml index a6dd3724..3828de29 100644 --- a/config.yaml +++ b/config.yaml @@ -43,8 +43,9 @@ ai: # 人设 personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。 -# 资源配置(这玩意儿是机器人那个机器上的smb文件夹) -# 暂时没用 - 2023-12-6 +# 资源配置 resource: - localPath: /files - remotePath: D:\Share\ + # 欢迎新成员表情包 + welcomeNew: + type: emotion + path: 58e4150be2bba8f7b71974b10391f9e9 diff --git a/config/config.go b/config/config.go index bdb40cf9..9a59ff54 100644 --- a/config/config.go +++ b/config/config.go @@ -5,8 +5,9 @@ var Conf Config // Config // @description: 配置 type Config struct { - Task task `json:"task" yaml:"task"` // 定时任务配置 - MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置 - Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手 - Ai ai `json:"ai" yaml:"ai"` // AI配置 + Task task `json:"task" yaml:"task"` // 定时任务配置 + MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置 + Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手 + Ai ai `json:"ai" yaml:"ai"` // AI配置 + Resource map[string]resourceItem `json:"resource" yaml:"resource"` // 资源配置 } diff --git a/config/resource.go b/config/resource.go new file mode 100644 index 00000000..c022d798 --- /dev/null +++ b/config/resource.go @@ -0,0 +1,8 @@ +package config + +// resourceItem +// @description: 资源项 +type resourceItem struct { + Type string `json:"type" yaml:"type"` // 类型 + Path string `json:"path" yaml:"path"` // 路径 +} diff --git a/handler/parse.go b/handler/parse.go index c81b7542..494261ea 100644 --- a/handler/parse.go +++ b/handler/parse.go @@ -42,6 +42,7 @@ func Parse(remoteAddr net.Addr, msg []byte) { // 异步处理消息 go func() { if m.IsNewUserJoin() { + log.Printf("%s -> 开始迎新 -> %s", m.FromUser, m.Content) // 欢迎新成员 go handleNewUserJoin(m) } else if m.IsAt() { diff --git a/handler/sys_message.go b/handler/sys_message.go index df1220e1..d963bcdd 100644 --- a/handler/sys_message.go +++ b/handler/sys_message.go @@ -2,9 +2,11 @@ package handler import ( "go-wechat/client" + "go-wechat/config" "go-wechat/entity" "go-wechat/model" "go-wechat/utils" + "log" ) // handleNewUserJoin @@ -13,13 +15,33 @@ import ( func handleNewUserJoin(m model.Message) { // 判断是否开启迎新 var count int64 - _ = client.MySQL.Model(&entity.Friend{}).Where("enable_welcome IS TRUE").Where("wxid = ?", m.FromUser).Count(&count).Error + err := client.MySQL.Model(&entity.Friend{}). + Where("enable_welcome IS TRUE"). + Where("wxid = ?", m.FromUser). + Count(&count).Error + if err != nil { + log.Printf("查询是否开启迎新失败: %s", err.Error()) + return + } if count < 1 { return } - // 发一张图乐呵乐呵 - - // 自己欢迎自己图片地址 D:\Share\emoticon\welcome-yourself.gif - utils.SendImage(m.FromUser, "D:\\Share\\emoticon\\welcome-yourself.gif", 0) + // 读取欢迎新成员配置 + conf, ok := config.Conf.Resource["welcomeNew"] + if !ok { + // 未配置,跳过 + return + } + switch conf.Type { + case "text": + // 文字类型 + utils.SendMessage(m.FromUser, "", conf.Path, 0) + case "image": + // 图片类型 + utils.SendImage(m.FromUser, conf.Path, 0) + case "emotion": + // 表情类型 + utils.SendEmotion(m.FromUser, conf.Path, 0) + } } diff --git a/initialization/mysql.go b/initialization/config.go similarity index 100% rename from initialization/mysql.go rename to initialization/config.go diff --git a/initialization/wechat.go b/initialization/wechat.go new file mode 100644 index 00000000..08f99b5a --- /dev/null +++ b/initialization/wechat.go @@ -0,0 +1,31 @@ +package initialization + +import ( + "github.com/go-resty/resty/v2" + "go-wechat/common/current" + "go-wechat/config" + "go-wechat/model" + "log" +) + +// InitWechatRobotInfo +// @description: 初始化微信机器人信息 +func InitWechatRobotInfo() { + // 获取数据 + var base model.Response[model.RobotUserInfo] + _, err := resty.New().R(). + SetHeader("Content-Type", "application/json;chartset=utf-8"). + SetResult(&base). + Post(config.Conf.Wechat.GetURL("/api/userInfo")) + if err != nil { + log.Printf("获取机器人信息失败: %s", err.Error()) + return + } + + log.Printf("机器人Id: %s", base.Data.WxId) + log.Printf("机器人微信号: %s", base.Data.Account) + log.Printf("机器人名称: %s", base.Data.Name) + + // 设置为单例 + current.SetRobotInfo(base.Data) +} diff --git a/main.go b/main.go index 5d73ec35..c128d4e3 100644 --- a/main.go +++ b/main.go @@ -16,8 +16,9 @@ import ( ) func init() { - initialization.InitConfig() - tasks.InitTasks() + initialization.InitConfig() // 初始化配置 + initialization.InitWechatRobotInfo() // 初始化机器人信息 + tasks.InitTasks() // 初始化定时任务 } func main() { diff --git a/model/userinfo.go b/model/userinfo.go new file mode 100644 index 00000000..15cae66f --- /dev/null +++ b/model/userinfo.go @@ -0,0 +1,18 @@ +package model + +// RobotUserInfo +// @description: 机器人用户信息 +type RobotUserInfo struct { + WxId string `json:"wxid"` // 微信Id + Account string `json:"account"` // 微信号 + Name string `json:"name"` // 昵称 + HeadImage string `json:"headImage"` // 头像 + Mobile string `json:"mobile"` // 手机 + Signature string `json:"signature"` // 个人签名 + Country string `json:"country"` // 国家 + Province string `json:"province"` // 省 + City string `json:"city"` // 城市 + CurrentDataPath string `json:"currentDataPath"` // 当前数据目录,登录的账号目录 + DataSavePath string `json:"dataSavePath"` // 微信保存目录 + DbKey string `json:"dbKey"` // 数据库的SQLCipher的加密key,可以使用该key配合decrypt.py解密数据库 +} diff --git a/utils/send.go b/utils/send.go index 11d13d57..3b8c34dd 100644 --- a/utils/send.go +++ b/utils/send.go @@ -2,7 +2,9 @@ package utils import ( "encoding/json" + "fmt" "github.com/go-resty/resty/v2" + "go-wechat/common/current" "go-wechat/config" "log" "time" @@ -82,3 +84,40 @@ func SendImage(toId, imgPath string, retryCount int) { } log.Printf("发送图片消息结果: %s", resp.String()) } + +// SendEmotion +// @description: 发送自定义表情包 +// @param toId string 群或者好友Id +// @param emotionHash string 表情包hash(md5值) +// @param retryCount int 重试次数 +func SendEmotion(toId, emotionHash string, retryCount int) { + if retryCount > 5 { + log.Printf("重试五次失败,停止发送") + return + } + + // 组装表情包本地地址 + // 规则:机器人数据目录\FileStorage\CustomEmotion\表情包hash前两位\表情包hash + emotionPath := fmt.Sprintf("%sFileStorage\\CustomEmotion\\%s\\%s", + current.GetRobotInfo().CurrentDataPath, emotionHash[:2], emotionHash) + + // 组装参数 + param := map[string]any{ + "wxid": toId, // 群或好友Id + "filePath": emotionPath, // 图片地址 + } + pbs, _ := json.Marshal(param) + + res := resty.New() + resp, err := res.R(). + SetHeader("Content-Type", "application/json;chartset=utf-8"). + SetBody(string(pbs)). + Post(config.Conf.Wechat.GetURL("/api/sendImagesMsg")) + if err != nil { + log.Printf("发送表情包消息失败: %s", err.Error()) + // 休眠五秒后重新发送 + time.Sleep(5 * time.Second) + SendImage(toId, emotionHash, retryCount+1) + } + log.Printf("发送表情包消息结果: %s", resp.String()) +}