From 0db8e6bf95577dea350301b9ff164057f961ae5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Mon, 1 Jan 2024 00:57:52 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E6=96=B0=E5=A2=9E=E6=B0=B4=E7=BE=A4?= =?UTF-8?q?=E5=B9=B4=E5=BA=A6=E6=8E=92=E8=A1=8C=E6=A6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 5 +- config/task.go | 1 + tasks/tasks.go | 3 ++ tasks/watergroup/utils.go | 2 + tasks/watergroup/year.go | 97 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tasks/watergroup/year.go diff --git a/config.yaml b/config.yaml index 61f9ff2..bc63a77 100644 --- a/config.yaml +++ b/config.yaml @@ -21,14 +21,15 @@ mysql: task: enable: false syncFriends: - enable: true + enable: false cron: '*/5 * * * *' # 五分钟一次 waterGroup: - enable: false + enable: true cron: yesterday: '30 9 * * *' # 每天9:30 week: '30 9 * * 1' # 每周一9:30 month: '30 9 1 * *' # 每月1号9:30 + year: '55 0 1 1 *' # 每年1月1号9:30 # AI回复 ai: diff --git a/config/task.go b/config/task.go index 151ddc7..9db7ed3 100644 --- a/config/task.go +++ b/config/task.go @@ -28,4 +28,5 @@ type waterGroupCron struct { Yesterday string `json:"yesterday" yaml:"yesterday"` // 昨日排行榜 Week string `json:"week" yaml:"week"` // 周排行榜 Month string `json:"month" yaml:"month"` // 月排行榜 + Year string `json:"year" yaml:"year"` // 年排行榜 } diff --git a/tasks/tasks.go b/tasks/tasks.go index 0315d70..d6ef579 100644 --- a/tasks/tasks.go +++ b/tasks/tasks.go @@ -32,6 +32,9 @@ func InitTasks() { if config.Conf.Task.WaterGroup.Cron.Month != "" { _, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Month).Do(watergroup.Month) } + if config.Conf.Task.WaterGroup.Cron.Year != "" { + _, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Year).Do(watergroup.Year) + } } // 更新好友列表 diff --git a/tasks/watergroup/utils.go b/tasks/watergroup/utils.go index 16a24e2..87cdb53 100644 --- a/tasks/watergroup/utils.go +++ b/tasks/watergroup/utils.go @@ -33,6 +33,8 @@ func getRankData(groupId, date string) (rank []rankUser, err error) { tx.Where("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(now()) - 1") case "month": tx.Where("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 1") + case "year": + tx.Where("YEAR(tm.create_at) = YEAR(NOW()) - 1") } // 查询指定时间段全部数据 diff --git a/tasks/watergroup/year.go b/tasks/watergroup/year.go new file mode 100644 index 0000000..908929a --- /dev/null +++ b/tasks/watergroup/year.go @@ -0,0 +1,97 @@ +package watergroup + +import ( + "fmt" + "go-wechat/config" + "go-wechat/service" + "go-wechat/utils" + "log" + "strings" + "time" +) + +// Year +// @description: 年排行榜 +func Year() { + groups, err := service.GetAllEnableChatRank() + if err != nil { + log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err) + return + } + + for _, group := range groups { + // 消息统计 + dealYear(group.Wxid) + + res, ok := config.Conf.Resource["wordcloud"] + if !ok { + continue + } + + // 获取上周周数 + year := time.Now().Local().AddDate(0, 0, -1).Year() + // 发送词云 + fileName := fmt.Sprintf("%d_%s.png", year, group.Wxid) + utils.SendImage(group.Wxid, fmt.Sprintf(res.Path, fileName), 0) + } +} + +// dealYear +// @description: 处理年度排行榜 +// @param gid +func dealYear(gid string) { + notifyMsgs := []string{"#年度水群排行榜"} + + // 获取上周消息总数 + records, err := getRankData(gid, "year") + if err != nil { + log.Printf("获取去年消息排行失败, 错误信息: %v", err) + return + } + log.Printf("去年消息总数: %+v", records) + // 莫得消息,直接返回 + if len(records) == 0 { + log.Printf("去年本群[%s]无对话记录", gid) + return + } + // 计算消息总数 + var msgCount int64 + for _, v := range records { + msgCount += v.Count + } + // 组装消息总数推送信息 + notifyMsgs = append(notifyMsgs, " ") + notifyMsgs = append(notifyMsgs, "亲爱的群友们,新年已经悄悄来临,让我们一起迎接这充满希望和美好的时刻。在这个特殊的日子里,我要向你们致以最真挚的祝福。") + notifyMsgs = append(notifyMsgs, "首先,我想对去年在群中表现出色、积极参与的成员们表示衷心的祝贺和感谢!你们的活跃与奉献让群聊更加充满了生机和活力。你们的贡献不仅仅是为了自己,更是为了我们整个群体的进步与成长。") + notifyMsgs = append(notifyMsgs, "特此给去年年度活跃成员排行榜上的朋友们送上真诚的祝福。你们的热情、智慧和参与度,令我们很是钦佩。愿新的一年中,你们继续保持着你们的活力和激情,为群中带来更多的惊喜和启迪。") + notifyMsgs = append(notifyMsgs, "对于那些未上榜的朋友们,我要说,你们也是我们群聊中非常重要的一部分。你们或许没有在排行榜上留下痕迹,但你们的存在和参与同样不可或缺。你们为群聊注入了新的思维和观点,为我们提供了不同的视角和见解。") + notifyMsgs = append(notifyMsgs, "因此,我想特别鼓励未上榜的朋友们,继续发扬你们的热情和积极性。无论是在分享知识、讨论问题、还是互相支持鼓励,你们的贡献都是宝贵的。让我们共同创造一个更加活跃和有意义的群聊环境。") + notifyMsgs = append(notifyMsgs, "最后,让我们一起迈向新的一年,相信自己的潜力和可能性,用我们的友谊和互助支持彼此。愿新的一年给我们带来更多的快乐、成功和成长。") + notifyMsgs = append(notifyMsgs, "祝福你们新年快乐!让我们一起迎接2022年的到来!") + notifyMsgs = append(notifyMsgs, " -- OpnWechat") + notifyMsgs = append(notifyMsgs, " ") + notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ 去年本群 %d 位朋友共产生 %d 条发言", len(records), msgCount)) + notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵") + + notifyMsgs = append(notifyMsgs, " ") + for i, r := range records { + // 只取前十条 + if i >= 10 { + break + } + log.Printf("账号: %s[%s] -> %d", r.Nickname, r.GroupUser, r.Count) + badge := "🏆" + switch i { + case 0: + badge = "🥇" + case 1: + badge = "🥈" + case 2: + badge = "🥉" + } + notifyMsgs = append(notifyMsgs, fmt.Sprintf("%s %s -> %d条", badge, r.Nickname, r.Count)) + } + + log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n")) + go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n"), 0) +}