From d2ca38aeefb9ebce6a6ba4d145e2a9f56b5dc426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Mon, 25 Sep 2023 14:16:59 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E6=B0=B4?= =?UTF-8?q?=E7=BE=A4=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 1 + tasks/tasks.go | 1 + tasks/water_group.go | 48 ++++++++++++++++++++++++++++++++++++-------- utils/send.go | 32 +++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 utils/send.go diff --git a/Dockerfile b/Dockerfile index 01e915cd..82dc5fec 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ COPY . . ENV GO111MODULE=on ENV GOPROXY=https://goproxy.cn,direct +RUN go version RUN go mod download && go build -o app RUN ls -lh && chmod +x ./app diff --git a/tasks/tasks.go b/tasks/tasks.go index 501fd335..0da09ca7 100644 --- a/tasks/tasks.go +++ b/tasks/tasks.go @@ -14,6 +14,7 @@ func InitTasks() { // 每天早上九点半发送前一天的水群排行 _, _ = s.Every(1).Day().At("09:30").Do(yesterday) + //_, _ = s.Every(5).Minute().Do(yesterday) // 每小时更新一次好友列表 //_, _ = s.Every(5).Minute().Do(syncFriends) diff --git a/tasks/water_group.go b/tasks/water_group.go index eb304c56..fcf9b9ba 100644 --- a/tasks/water_group.go +++ b/tasks/water_group.go @@ -1,9 +1,13 @@ package tasks import ( + "fmt" "go-wechat/client" "go-wechat/entity" + "go-wechat/utils" "log" + "os" + "strings" ) // 水群排行榜 @@ -11,10 +15,23 @@ import ( // yesterday // @description: 昨日排行榜 func yesterday() { + // 从环境变量读取需要处理的群Id + gid := strings.Split(os.Getenv("GROUP_ID"), ",") + for _, id := range gid { + dealYesterday(id) + } +} + +// dealYesterday +// @description: 处理请求 +// @param gid +func dealYesterday(gid string) { + notifyMsgs := []string{"#昨日水群排行榜"} + // 获取昨日消息总数 var yesterdayMsgCount int64 err := client.MySQL.Model(&entity.Message{}). - Where("from_user = ?", "18958257758@chatroom"). + Where("from_user = ?", gid). Where("DATEDIFF(create_at,NOW()) = -1"). Count(&yesterdayMsgCount).Error if err != nil { @@ -22,25 +39,40 @@ func yesterday() { return } log.Printf("昨日消息总数: %d", yesterdayMsgCount) + if yesterdayMsgCount == 0 { + return + } + + notifyMsgs = append(notifyMsgs, " ") + notifyMsgs = append(notifyMsgs, fmt.Sprintf("昨日消息总数: %d", yesterdayMsgCount)) // 返回数据 type record struct { GroupUser string + Nickname string Count int64 } var records []record - err = client.MySQL.Model(&entity.Message{}). - Select("group_user", "count( 1 ) AS `count`"). - Where("from_user = ?", "18958257758@chatroom"). - Where("DATEDIFF(create_at,NOW()) = -1"). - Group("group_user").Order("`count` DESC"). + err = client.MySQL.Table("t_message AS tm"). + Joins("LEFT JOIN t_group_user AS tgu ON tgu.wxid = tm.group_user"). + Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`"). + Where("tm.from_user = ?", gid). + Where("DATEDIFF(tm.create_at,NOW()) = -1"). + Group("tm.group_user, tgu.nickname").Order("`count` DESC"). Limit(5).Find(&records).Error if err != nil { log.Printf("获取昨日消息失败, 错误信息: %v", err) return } - for _, r := range records { - log.Printf("账号: %s -> %d", r.GroupUser, r.Count) + notifyMsgs = append(notifyMsgs, " ") + for i, r := range records { + log.Printf("账号: %s[%s] -> %d", r.Nickname, r.GroupUser, r.Count) + notifyMsgs = append(notifyMsgs, fmt.Sprintf("#%d: %s -> %d条", i+1, r.Nickname, r.Count)) } + + notifyMsgs = append(notifyMsgs, " \n请未上榜的群友多多反思。") + + log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n")) + go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n")) } diff --git a/utils/send.go b/utils/send.go new file mode 100644 index 00000000..a284d7bb --- /dev/null +++ b/utils/send.go @@ -0,0 +1,32 @@ +package utils + +import ( + "encoding/json" + "github.com/go-resty/resty/v2" + "log" +) + +// SendMessage +// @description: 发送消息 +// @param toId +// @param atId +// @param msg +func SendMessage(toId, atId, msg string) { + // 组装参数 + param := map[string]any{ + "wxid": toId, // 群或好友Id + "msg": msg, // 消息 + } + pbs, _ := json.Marshal(param) + + res := resty.New() + resp, err := res.R(). + SetHeader("Content-Type", "application/json;chartset=utf-8"). + SetBody(string(pbs)). + Post("http://10.0.0.73:19088/api/sendTextMsg") + if err != nil { + log.Printf("发送文本消息失败: %s", err.Error()) + return + } + log.Printf("发送文本消息结果: %s", resp.String()) +}