From d08937563afe204874faed5d11cadba61e833917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Fri, 3 Nov 2023 11:59:40 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E8=BF=8E?= =?UTF-8?q?=E6=96=B0=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/parse.go | 1 + handler/sys_message.go | 19 +++++++++++++++++++ utils/send.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 handler/sys_message.go diff --git a/handler/parse.go b/handler/parse.go index 46c0bad..115858f 100644 --- a/handler/parse.go +++ b/handler/parse.go @@ -31,6 +31,7 @@ func Parse(remoteAddr net.Addr, msg []byte) { // 消息撤回 case types.MsgTypeSys: // 系统消息 + go handleSysMessage(m) default: // 默认消息处理 groupUser = strings.Split(m.Content, "\n")[0] diff --git a/handler/sys_message.go b/handler/sys_message.go new file mode 100644 index 0000000..88e9b42 --- /dev/null +++ b/handler/sys_message.go @@ -0,0 +1,19 @@ +package handler + +import ( + "go-wechat/model" + "go-wechat/utils" + "strings" +) + +// handleSysMessage +// @description: 系统消息处理 +// @param m +func handleSysMessage(m model.Message) { + // 有人进群 + if strings.Contains(m.Content, "\"邀请\"") && strings.Contains(m.Content, "\"加入了群聊") { + // 发一张图乐呵乐呵 + // 自己欢迎自己图片地址 D:\Share\emoticon\welcome-yourself.gif + utils.SendImage(m.FromUser, "D:\\Share\\emoticon\\welcome-yourself.gif", 0) + } +} diff --git a/utils/send.go b/utils/send.go index ef250ad..732f97c 100644 --- a/utils/send.go +++ b/utils/send.go @@ -38,3 +38,35 @@ func SendMessage(toId, atId, msg string, retryCount int) { } log.Printf("发送文本消息结果: %s", resp.String()) } + +// SendImage +// @description: 发送图片 +// @param toId string 群或者好友Id +// @param imgPath string 图片路径 +// @param retryCount int 重试次数 +func SendImage(toId, imgPath string, retryCount int) { + if retryCount > 5 { + log.Printf("重试五次失败,停止发送") + return + } + + // 组装参数 + param := map[string]any{ + "wxid": toId, // 群或好友Id + "imagePath": imgPath, // 图片地址 + } + 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, imgPath, retryCount+1) + } + log.Printf("发送图片消息结果: %s", resp.String()) +}