新增水群通知

This commit is contained in:
李寻欢 2023-09-25 14:16:59 +08:00
parent 3e8051906f
commit d2ca38aeef
4 changed files with 74 additions and 8 deletions

View File

@ -5,6 +5,7 @@ COPY . .
ENV GO111MODULE=on ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn,direct ENV GOPROXY=https://goproxy.cn,direct
RUN go version
RUN go mod download && go build -o app RUN go mod download && go build -o app
RUN ls -lh && chmod +x ./app RUN ls -lh && chmod +x ./app

View File

@ -14,6 +14,7 @@ func InitTasks() {
// 每天早上九点半发送前一天的水群排行 // 每天早上九点半发送前一天的水群排行
_, _ = s.Every(1).Day().At("09:30").Do(yesterday) _, _ = s.Every(1).Day().At("09:30").Do(yesterday)
//_, _ = s.Every(5).Minute().Do(yesterday)
// 每小时更新一次好友列表 // 每小时更新一次好友列表
//_, _ = s.Every(5).Minute().Do(syncFriends) //_, _ = s.Every(5).Minute().Do(syncFriends)

View File

@ -1,9 +1,13 @@
package tasks package tasks
import ( import (
"fmt"
"go-wechat/client" "go-wechat/client"
"go-wechat/entity" "go-wechat/entity"
"go-wechat/utils"
"log" "log"
"os"
"strings"
) )
// 水群排行榜 // 水群排行榜
@ -11,10 +15,23 @@ import (
// yesterday // yesterday
// @description: 昨日排行榜 // @description: 昨日排行榜
func yesterday() { 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 var yesterdayMsgCount int64
err := client.MySQL.Model(&entity.Message{}). err := client.MySQL.Model(&entity.Message{}).
Where("from_user = ?", "18958257758@chatroom"). Where("from_user = ?", gid).
Where("DATEDIFF(create_at,NOW()) = -1"). Where("DATEDIFF(create_at,NOW()) = -1").
Count(&yesterdayMsgCount).Error Count(&yesterdayMsgCount).Error
if err != nil { if err != nil {
@ -22,25 +39,40 @@ func yesterday() {
return return
} }
log.Printf("昨日消息总数: %d", yesterdayMsgCount) log.Printf("昨日消息总数: %d", yesterdayMsgCount)
if yesterdayMsgCount == 0 {
return
}
notifyMsgs = append(notifyMsgs, " ")
notifyMsgs = append(notifyMsgs, fmt.Sprintf("昨日消息总数: %d", yesterdayMsgCount))
// 返回数据 // 返回数据
type record struct { type record struct {
GroupUser string GroupUser string
Nickname string
Count int64 Count int64
} }
var records []record var records []record
err = client.MySQL.Model(&entity.Message{}). err = client.MySQL.Table("t_message AS tm").
Select("group_user", "count( 1 ) AS `count`"). Joins("LEFT JOIN t_group_user AS tgu ON tgu.wxid = tm.group_user").
Where("from_user = ?", "18958257758@chatroom"). Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("DATEDIFF(create_at,NOW()) = -1"). Where("tm.from_user = ?", gid).
Group("group_user").Order("`count` DESC"). Where("DATEDIFF(tm.create_at,NOW()) = -1").
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
Limit(5).Find(&records).Error Limit(5).Find(&records).Error
if err != nil { if err != nil {
log.Printf("获取昨日消息失败, 错误信息: %v", err) log.Printf("获取昨日消息失败, 错误信息: %v", err)
return return
} }
for _, r := range records { notifyMsgs = append(notifyMsgs, " ")
log.Printf("账号: %s -> %d", r.GroupUser, r.Count) 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"))
} }

32
utils/send.go Normal file
View File

@ -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())
}