Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6626f45af2 | ||
|
|
4b5d074517 | ||
|
|
e3bb115560 | ||
|
|
3b4862d5bc | ||
|
|
997ad806f0 | ||
|
|
86435e9707 | ||
|
|
b4470b0888 | ||
|
|
500e241f8d | ||
|
|
6c2bf3fc9c |
18
config.yaml
18
config.yaml
@@ -22,20 +22,20 @@ task:
|
||||
cron: '*/5 * * * *' # 五分钟一次
|
||||
waterGroup:
|
||||
enable: false
|
||||
cron: '30 9 * * *'
|
||||
# 需要发送水群排行榜的群Id
|
||||
groups:
|
||||
- '18958257758@chatroom'
|
||||
- '49448748645@chatroom'
|
||||
# 不计入统计范围的用户Id
|
||||
blacklist:
|
||||
- 'wxid_7788687886912'
|
||||
cron:
|
||||
yesterday: '30 9 * * *' # 每天9:30
|
||||
week: '30 9 * * 1' # 每周一9:30
|
||||
month: '30 9 1 * *' # 每月1号9:30
|
||||
|
||||
# AI回复
|
||||
ai:
|
||||
# 是否启用
|
||||
enable: false
|
||||
# 模型,不填默认gpt-3.5-turbo-0613
|
||||
model: gpt-3.5-turbo-0613
|
||||
# OpenAI Api key
|
||||
apiKey: sk-xxxx
|
||||
# 接口代理域名,不填默认ChatGPT官方地址
|
||||
baseUrl: https://sxxx
|
||||
baseUrl: https://sxxx
|
||||
# 人设
|
||||
personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。
|
||||
|
||||
@@ -3,7 +3,9 @@ package config
|
||||
// ai
|
||||
// @description: AI配置
|
||||
type ai struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用AI
|
||||
ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key
|
||||
BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用AI
|
||||
Model string `json:"model" yaml:"model"` // 模型
|
||||
ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key
|
||||
BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址
|
||||
Personality string `json:"personality" yaml:"personality"` // 人设
|
||||
}
|
||||
|
||||
@@ -10,19 +10,3 @@ type Config struct {
|
||||
Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手
|
||||
Ai ai `json:"ai" yaml:"ai"` // AI配置
|
||||
}
|
||||
|
||||
// task
|
||||
// @description: 定时任务
|
||||
type task struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
SyncFriends struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron string `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
} `json:"syncFriends" yaml:"syncFriends"` // 同步好友
|
||||
WaterGroup struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron string `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
Groups []string `json:"groups" yaml:"groups"` // 启用的群Id
|
||||
Blacklist []string `json:"blacklist" yaml:"blacklist"` // 黑名单
|
||||
} `json:"waterGroup" yaml:"waterGroup"` // 水群排行榜
|
||||
}
|
||||
|
||||
31
config/task.go
Normal file
31
config/task.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package config
|
||||
|
||||
// task
|
||||
// @description: 定时任务
|
||||
type task struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
SyncFriends syncFriends `json:"syncFriends" yaml:"syncFriends"` // 同步好友
|
||||
WaterGroup waterGroup `json:"waterGroup" yaml:"waterGroup"` // 水群排行榜
|
||||
}
|
||||
|
||||
// syncFriends
|
||||
// @description: 同步好友
|
||||
type syncFriends struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron string `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
}
|
||||
|
||||
// waterGroup
|
||||
// @description: 水群排行榜
|
||||
type waterGroup struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron waterGroupCron `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
}
|
||||
|
||||
// waterGroupCron
|
||||
// @description: 水群排行榜定时任务
|
||||
type waterGroupCron struct {
|
||||
Yesterday string `json:"yesterday" yaml:"yesterday"` // 昨日排行榜
|
||||
Week string `json:"week" yaml:"week"` // 周排行榜
|
||||
Month string `json:"month" yaml:"month"` // 月排行榜
|
||||
}
|
||||
@@ -5,11 +5,13 @@ import "time"
|
||||
// Friend
|
||||
// @description: 好友列表
|
||||
type Friend struct {
|
||||
CustomAccount string `json:"customAccount"` // 微信号
|
||||
Nickname string `json:"nickname"` // 昵称
|
||||
Pinyin string `json:"pinyin"` // 昵称拼音大写首字母
|
||||
PinyinAll string `json:"pinyinAll"` // 昵称全拼
|
||||
Wxid string `json:"wxid"` // 微信原始Id
|
||||
CustomAccount string `json:"customAccount"` // 微信号
|
||||
Nickname string `json:"nickname"` // 昵称
|
||||
Pinyin string `json:"pinyin"` // 昵称拼音大写首字母
|
||||
PinyinAll string `json:"pinyinAll"` // 昵称全拼
|
||||
Wxid string `json:"wxid"` // 微信原始Id
|
||||
EnableAi bool `json:"enableAI" gorm:"type:tinyint(1) default 0 not null"` // 是否使用AI
|
||||
EnableChatRank bool `json:"enableChatRank" gorm:"type:tinyint(1) default 0 not null"` // 是否使用聊天排行
|
||||
}
|
||||
|
||||
func (Friend) TableName() string {
|
||||
@@ -19,13 +21,14 @@ func (Friend) TableName() string {
|
||||
// GroupUser
|
||||
// @description: 群成员
|
||||
type GroupUser struct {
|
||||
GroupId string `json:"groupId"` // 群Id
|
||||
Account string `json:"account"` // 账号
|
||||
HeadImage string `json:"headImage"` // 头像
|
||||
Nickname string `json:"nickname"` // 昵称
|
||||
Wxid string `json:"wxid"` // 微信Id
|
||||
IsMember bool `json:"isMember" gorm:"type:tinyint(1)"` // 是否群成员
|
||||
LeaveTime *time.Time `json:"leaveTime"` // 离开时间
|
||||
GroupId string `json:"groupId"` // 群Id
|
||||
Account string `json:"account"` // 账号
|
||||
HeadImage string `json:"headImage"` // 头像
|
||||
Nickname string `json:"nickname"` // 昵称
|
||||
Wxid string `json:"wxid"` // 微信Id
|
||||
IsMember bool `json:"isMember" gorm:"type:tinyint(1) default 0 not null"` // 是否群成员
|
||||
LeaveTime *time.Time `json:"leaveTime"` // 离开时间
|
||||
SkipChatRank bool `json:"skipChatRank" gorm:"type:tinyint(1) default 0 not null"` // 是否跳过聊天排行
|
||||
}
|
||||
|
||||
func (GroupUser) TableName() string {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"go-wechat/config"
|
||||
"go-wechat/entity"
|
||||
"go-wechat/service"
|
||||
"go-wechat/utils"
|
||||
"log"
|
||||
"regexp"
|
||||
@@ -20,6 +21,24 @@ func handleAtMessage(m entity.Message) {
|
||||
return
|
||||
}
|
||||
|
||||
// 取出所有启用了AI的好友或群组
|
||||
us, err := service.GetAllEnableAI()
|
||||
if err != nil {
|
||||
utils.SendMessage(m.FromUser, m.GroupUser, "#系统异常\n"+err.Error(), 0)
|
||||
return
|
||||
}
|
||||
// 判断是否启用,如果没有启用,直接返回
|
||||
var canUse bool
|
||||
for _, u := range us {
|
||||
if u.Wxid == m.FromUser {
|
||||
canUse = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !canUse {
|
||||
return
|
||||
}
|
||||
|
||||
// 预处理一下发送的消息,用正则去掉@机器人的内容
|
||||
re := regexp.MustCompile(`@([^ ]+)`)
|
||||
matches := re.FindStringSubmatch(m.Content)
|
||||
@@ -28,6 +47,27 @@ func handleAtMessage(m entity.Message) {
|
||||
m.Content = strings.Replace(m.Content, matches[0], "", 1)
|
||||
}
|
||||
|
||||
// 组装消息体
|
||||
messages := make([]openai.ChatCompletionMessage, 0)
|
||||
if config.Conf.Ai.Personality != "" {
|
||||
// 填充人设
|
||||
messages = append(messages, openai.ChatCompletionMessage{
|
||||
Role: openai.ChatMessageRoleSystem,
|
||||
Content: config.Conf.Ai.Personality,
|
||||
})
|
||||
}
|
||||
// 填充用户消息
|
||||
messages = append(messages, openai.ChatCompletionMessage{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: m.Content,
|
||||
})
|
||||
|
||||
// 配置模型
|
||||
model := openai.GPT3Dot5Turbo0613
|
||||
if config.Conf.Ai.Model != "" {
|
||||
model = config.Conf.Ai.Model
|
||||
}
|
||||
|
||||
// 默认使用AI回复
|
||||
conf := openai.DefaultConfig(config.Conf.Ai.ApiKey)
|
||||
if config.Conf.Ai.BaseUrl != "" {
|
||||
@@ -37,13 +77,8 @@ func handleAtMessage(m entity.Message) {
|
||||
resp, err := client.CreateChatCompletion(
|
||||
context.Background(),
|
||||
openai.ChatCompletionRequest{
|
||||
Model: openai.GPT3Dot5Turbo0613,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: m.Content,
|
||||
},
|
||||
},
|
||||
Model: model,
|
||||
Messages: messages,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
13
readme.md
13
readme.md
@@ -7,7 +7,7 @@ cd wechat-hook
|
||||
1. 创建配置文件`config.yaml`
|
||||
```shell
|
||||
mkdir config # 先创建一个文件夹保存配置文件,文件名不要变
|
||||
vim config.yaml # 编辑配置文件,内容如下
|
||||
vim config.yaml # 编辑配置文件,内容如下,最新配置请参考项目里的config.yaml文件
|
||||
```
|
||||
```yaml
|
||||
# 微信HOOK配置
|
||||
@@ -34,13 +34,10 @@ task:
|
||||
cron: '0 * * * *'
|
||||
waterGroup:
|
||||
enable: true
|
||||
cron: '30 9 * * *'
|
||||
# 需要发送水群排行榜的群Id
|
||||
groups:
|
||||
- '11111@chatroom' # 自行配置
|
||||
# 不计入统计范围的用户Id
|
||||
blacklist:
|
||||
- 'wxid_xxxx' # 自行配置
|
||||
cron:
|
||||
yesterday: '30 9 * * *' # 每天9:30
|
||||
week: '30 9 * * 1' # 每周一9:30
|
||||
month: '30 9 1 * *' # 每月1号9:30
|
||||
```
|
||||
|
||||
2. 创建`docker-compose.yaml`文件
|
||||
|
||||
23
service/friend.go
Normal file
23
service/friend.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"go-wechat/client"
|
||||
"go-wechat/entity"
|
||||
)
|
||||
|
||||
// GetAllEnableAI
|
||||
// @description: 取出所有启用了AI的好友或群组
|
||||
// @return []entity.Friend
|
||||
func GetAllEnableAI() (records []entity.Friend, err error) {
|
||||
err = client.MySQL.Where("enable_ai = ?", 1).Find(&records).Error
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllEnableChatRank
|
||||
// @description: 取出所有启用了聊天排行榜的群组
|
||||
// @return records
|
||||
// @return err
|
||||
func GetAllEnableChatRank() (records []entity.Friend, err error) {
|
||||
err = client.MySQL.Where("enable_chat_rank = ?", 1).Where("wxid LIKE '%@chatroom'").Find(&records).Error
|
||||
return
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package tasks
|
||||
package friends
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -20,9 +20,9 @@ import (
|
||||
// http客户端
|
||||
var hc = resty.New()
|
||||
|
||||
// syncFriends
|
||||
// Sync
|
||||
// @description: 同步好友列表
|
||||
func syncFriends() {
|
||||
func Sync() {
|
||||
var base model.Response[[]model.FriendItem]
|
||||
|
||||
resp, err := hc.R().
|
||||
@@ -3,6 +3,8 @@ package tasks
|
||||
import (
|
||||
"github.com/go-co-op/gocron"
|
||||
"go-wechat/config"
|
||||
"go-wechat/tasks/friends"
|
||||
"go-wechat/tasks/watergroup"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
@@ -20,14 +22,22 @@ func InitTasks() {
|
||||
|
||||
// 水群排行
|
||||
if config.Conf.Task.WaterGroup.Enable {
|
||||
log.Printf("水群排行任务已启用,执行表达式: %s", config.Conf.Task.WaterGroup.Cron)
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron).Do(yesterday)
|
||||
log.Printf("水群排行任务已启用,执行表达式: %+v", config.Conf.Task.WaterGroup.Cron)
|
||||
if config.Conf.Task.WaterGroup.Cron.Yesterday != "" {
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Yesterday).Do(watergroup.Yesterday)
|
||||
}
|
||||
if config.Conf.Task.WaterGroup.Cron.Week != "" {
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Week).Do(watergroup.Week)
|
||||
}
|
||||
if config.Conf.Task.WaterGroup.Cron.Month != "" {
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Month).Do(watergroup.Month)
|
||||
}
|
||||
}
|
||||
|
||||
// 更新好友列表
|
||||
if config.Conf.Task.SyncFriends.Enable {
|
||||
log.Printf("更新好友列表任务已启用,执行表达式: %s", config.Conf.Task.SyncFriends.Cron)
|
||||
_, _ = s.Cron(config.Conf.Task.SyncFriends.Cron).Do(syncFriends)
|
||||
_, _ = s.Cron(config.Conf.Task.SyncFriends.Cron).Do(friends.Sync)
|
||||
}
|
||||
|
||||
// 开启定时任务
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-wechat/client"
|
||||
"go-wechat/config"
|
||||
"go-wechat/entity"
|
||||
"go-wechat/utils"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 水群排行榜
|
||||
|
||||
// yesterday
|
||||
// @description: 昨日排行榜
|
||||
func yesterday() {
|
||||
for _, id := range config.Conf.Task.WaterGroup.Groups {
|
||||
dealYesterday(id)
|
||||
}
|
||||
}
|
||||
|
||||
// dealYesterday
|
||||
// @description: 处理请求
|
||||
// @param gid
|
||||
func dealYesterday(gid string) {
|
||||
notifyMsgs := []string{"#昨日水群排行榜"}
|
||||
|
||||
// 获取昨日消息总数
|
||||
var yesterdayMsgCount int64
|
||||
err := client.MySQL.Model(&entity.Message{}).
|
||||
Where("from_user = ?", gid).
|
||||
Where("DATEDIFF(create_at,NOW()) = -1").
|
||||
Count(&yesterdayMsgCount).Error
|
||||
if err != nil {
|
||||
log.Printf("获取昨日消息总数失败, 错误信息: %v", err)
|
||||
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
|
||||
tx := client.MySQL.Table("t_message AS tm").
|
||||
Joins("LEFT JOIN t_group_user AS tgu ON tgu.wxid = tm.group_user AND tm.from_user = tgu.group_id").
|
||||
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
|
||||
Where("tm.from_user = ?", gid).
|
||||
Where("tm.type < 10000").
|
||||
Where("DATEDIFF(tm.create_at,NOW()) = -1").
|
||||
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
|
||||
Limit(10)
|
||||
|
||||
// 黑名单
|
||||
blacklist := config.Conf.Task.WaterGroup.Blacklist
|
||||
// 如果有黑名单,过滤掉
|
||||
if len(blacklist) > 0 {
|
||||
tx.Where("tm.group_user NOT IN (?)", blacklist)
|
||||
}
|
||||
|
||||
err = tx.Find(&records).Error
|
||||
|
||||
if err != nil {
|
||||
log.Printf("获取昨日消息失败, 错误信息: %v", err)
|
||||
return
|
||||
}
|
||||
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"), 0)
|
||||
}
|
||||
85
tasks/watergroup/month.go
Normal file
85
tasks/watergroup/month.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package watergroup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-wechat/service"
|
||||
"go-wechat/utils"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Month
|
||||
// @description: 月排行榜
|
||||
func Month() {
|
||||
groups, err := service.GetAllEnableChatRank()
|
||||
if err != nil {
|
||||
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, group := range groups {
|
||||
// 消息统计
|
||||
dealMonth(group.Wxid)
|
||||
// 获取上个月月份
|
||||
yd := time.Now().Local().AddDate(0, 0, -1).Format("200601")
|
||||
// 发送词云
|
||||
fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
|
||||
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// dealMonth
|
||||
// @description: 处理请求
|
||||
// @param gid
|
||||
func dealMonth(gid string) {
|
||||
monthStr := time.Now().Local().AddDate(0, 0, -1).Format("2006年01月")
|
||||
notifyMsgs := []string{fmt.Sprintf("#%s水群排行榜", monthStr)}
|
||||
|
||||
// 获取上月消息总数
|
||||
records, err := getRankData(gid, "month")
|
||||
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, fmt.Sprintf("🗣️ %s本群 %d 位朋友共产生 %d 条发言", monthStr, 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))
|
||||
}
|
||||
|
||||
notifyMsgs = append(notifyMsgs, fmt.Sprintf(" \n🎉感谢以上群友%s对群活跃做出的卓越贡献,也请未上榜的群友多多反思。", monthStr))
|
||||
|
||||
log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n"))
|
||||
go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n"), 0)
|
||||
}
|
||||
41
tasks/watergroup/utils.go
Normal file
41
tasks/watergroup/utils.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package watergroup
|
||||
|
||||
import "go-wechat/client"
|
||||
|
||||
// rankUser
|
||||
// @description: 排行榜用户
|
||||
type rankUser struct {
|
||||
GroupUser string // 微信Id
|
||||
Nickname string // 昵称
|
||||
Count int64 // 消息数
|
||||
}
|
||||
|
||||
// getRankData
|
||||
// @description: 获取消息排行榜
|
||||
// @param groupId string 群Id
|
||||
// @param d string 模式(yesterday | week | month)
|
||||
// @return rank
|
||||
// @return err
|
||||
func getRankData(groupId, date string) (rank []rankUser, err error) {
|
||||
tx := client.MySQL.Table("t_message AS tm").
|
||||
Joins("LEFT JOIN t_group_user AS tgu ON tgu.wxid = tm.group_user AND tm.from_user = tgu.group_id AND tgu.skip_chat_rank = 0").
|
||||
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
|
||||
Where("tm.from_user = ?", groupId).
|
||||
Where("tm.type < 10000").
|
||||
Group("tm.group_user, tgu.nickname").
|
||||
Order("`count` DESC")
|
||||
|
||||
// 根据参数获取不同日期的数据
|
||||
switch date {
|
||||
case "yesterday":
|
||||
tx.Where("DATEDIFF(tm.create_at,NOW()) = -1")
|
||||
case "week":
|
||||
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")
|
||||
}
|
||||
|
||||
// 查询指定时间段全部数据
|
||||
err = tx.Find(&rank).Error
|
||||
return
|
||||
}
|
||||
83
tasks/watergroup/week.go
Normal file
83
tasks/watergroup/week.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package watergroup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-wechat/service"
|
||||
"go-wechat/utils"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Week
|
||||
// @description: 周排行榜
|
||||
func Week() {
|
||||
groups, err := service.GetAllEnableChatRank()
|
||||
if err != nil {
|
||||
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, group := range groups {
|
||||
// 消息统计
|
||||
dealWeek(group.Wxid)
|
||||
// 获取上周周数
|
||||
year, weekNo := time.Now().Local().AddDate(0, 0, -1).ISOWeek()
|
||||
// 发送词云
|
||||
fileName := fmt.Sprintf("%d%d_%s.png", year, weekNo, group.Wxid)
|
||||
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// dealWeek
|
||||
// @description: 处理请求
|
||||
// @param gid
|
||||
func dealWeek(gid string) {
|
||||
notifyMsgs := []string{"#上周水群排行榜"}
|
||||
|
||||
// 获取上周消息总数
|
||||
records, err := getRankData(gid, "week")
|
||||
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, 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))
|
||||
}
|
||||
|
||||
notifyMsgs = append(notifyMsgs, " \n🎉感谢以上群友上周对群活跃做出的卓越贡献,也请未上榜的群友多多反思。")
|
||||
|
||||
log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n"))
|
||||
go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n"), 0)
|
||||
}
|
||||
85
tasks/watergroup/yesterday.go
Normal file
85
tasks/watergroup/yesterday.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package watergroup
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-wechat/service"
|
||||
"go-wechat/utils"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 水群排行榜
|
||||
|
||||
// Yesterday
|
||||
// @description: 昨日排行榜
|
||||
func Yesterday() {
|
||||
groups, err := service.GetAllEnableChatRank()
|
||||
if err != nil {
|
||||
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, group := range groups {
|
||||
// 消息统计
|
||||
dealYesterday(group.Wxid)
|
||||
// 获取昨日日期
|
||||
yd := time.Now().Local().AddDate(0, 0, -1).Format("20060102")
|
||||
// 发送词云
|
||||
fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
|
||||
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
|
||||
}
|
||||
}
|
||||
|
||||
// dealYesterday
|
||||
// @description: 处理请求
|
||||
// @param gid
|
||||
func dealYesterday(gid string) {
|
||||
notifyMsgs := []string{"#昨日水群排行榜"}
|
||||
|
||||
// 获取昨日消息总数
|
||||
records, err := getRankData(gid, "yesterday")
|
||||
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, 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))
|
||||
}
|
||||
|
||||
notifyMsgs = append(notifyMsgs, " \n🎉感谢以上群友昨日对群活跃做出的卓越贡献,也请未上榜的群友多多反思。")
|
||||
|
||||
log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n"))
|
||||
go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n"), 0)
|
||||
}
|
||||
Reference in New Issue
Block a user