Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3b4862d5bc | ||
|
|
997ad806f0 | ||
|
|
86435e9707 | ||
|
|
b4470b0888 | ||
|
|
500e241f8d | ||
|
|
6c2bf3fc9c | ||
|
|
91d2fc50e2 | ||
|
|
119c2a5359 | ||
|
|
06a64c5e5a | ||
|
|
60bfa0e8a0 |
23
config.yaml
23
config.yaml
@@ -22,11 +22,30 @@ task:
|
|||||||
cron: '*/5 * * * *' # 五分钟一次
|
cron: '*/5 * * * *' # 五分钟一次
|
||||||
waterGroup:
|
waterGroup:
|
||||||
enable: false
|
enable: false
|
||||||
cron: '30 9 * * *'
|
cron:
|
||||||
|
yesterday: '30 9 * * *' # 每天9:30
|
||||||
|
week: '30 9 * * 1' # 每周一9:30
|
||||||
|
month: '30 9 1 * *' # 每月1号9:30
|
||||||
# 需要发送水群排行榜的群Id
|
# 需要发送水群排行榜的群Id
|
||||||
groups:
|
groups:
|
||||||
- '18958257758@chatroom'
|
- '18958257758@chatroom'
|
||||||
- '49448748645@chatroom'
|
- '49448748645@chatroom'
|
||||||
# 不计入统计范围的用户Id
|
# 不计入统计范围的用户Id
|
||||||
blacklist:
|
blacklist:
|
||||||
- 'wxid_7788687886912'
|
- 'wxid_7788687886912'
|
||||||
|
|
||||||
|
# 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
|
||||||
|
# 人设
|
||||||
|
personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。
|
||||||
|
# 禁用的群
|
||||||
|
disableGroup:
|
||||||
|
- 49448748645@chatroom
|
||||||
12
config/ai.go
Normal file
12
config/ai.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
// ai
|
||||||
|
// @description: AI配置
|
||||||
|
type ai struct {
|
||||||
|
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"` // 人设
|
||||||
|
DisableGroup []string `json:"disableGroup" yaml:"disableGroup"` // 禁用群组
|
||||||
|
}
|
||||||
@@ -8,20 +8,5 @@ type Config struct {
|
|||||||
Task task `json:"task" yaml:"task"` // 定时任务配置
|
Task task `json:"task" yaml:"task"` // 定时任务配置
|
||||||
MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置
|
MySQL mysql `json:"mysql" yaml:"mysql"` // MySQL 配置
|
||||||
Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手
|
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"` // 水群排行榜
|
|
||||||
}
|
}
|
||||||
|
|||||||
33
config/task.go
Normal file
33
config/task.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
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"` // 定时任务表达式
|
||||||
|
Groups []string `json:"groups" yaml:"groups"` // 启用的群Id
|
||||||
|
Blacklist []string `json:"blacklist" yaml:"blacklist"` // 黑名单
|
||||||
|
}
|
||||||
|
|
||||||
|
// waterGroupCron
|
||||||
|
// @description: 水群排行榜定时任务
|
||||||
|
type waterGroupCron struct {
|
||||||
|
Yesterday string `json:"yesterday" yaml:"yesterday"` // 昨日排行榜
|
||||||
|
Week string `json:"week" yaml:"week"` // 周排行榜
|
||||||
|
Month string `json:"month" yaml:"month"` // 月排行榜
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
@@ -7,6 +7,7 @@ require (
|
|||||||
github.com/fsnotify/fsnotify v1.6.0
|
github.com/fsnotify/fsnotify v1.6.0
|
||||||
github.com/go-co-op/gocron v1.34.1
|
github.com/go-co-op/gocron v1.34.1
|
||||||
github.com/go-resty/resty/v2 v2.8.0
|
github.com/go-resty/resty/v2 v2.8.0
|
||||||
|
github.com/sashabaranov/go-openai v1.17.5
|
||||||
github.com/spf13/viper v1.17.0
|
github.com/spf13/viper v1.17.0
|
||||||
gorm.io/driver/mysql v1.5.1
|
gorm.io/driver/mysql v1.5.1
|
||||||
gorm.io/gorm v1.25.4
|
gorm.io/gorm v1.25.4
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -177,6 +177,8 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c
|
|||||||
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
|
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
|
||||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||||
|
github.com/sashabaranov/go-openai v1.17.5 h1:ItBzlrrfTtkFWOFlgfOhk3y/xRBC4PJol4gdbiK7hgg=
|
||||||
|
github.com/sashabaranov/go-openai v1.17.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||||
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
|
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
|
||||||
|
|||||||
80
handler/at_message.go
Normal file
80
handler/at_message.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package handler
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/slice"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
"go-wechat/config"
|
||||||
|
"go-wechat/entity"
|
||||||
|
"go-wechat/utils"
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// handleAtMessage
|
||||||
|
// @description: 处理At机器人的消息
|
||||||
|
// @param m
|
||||||
|
func handleAtMessage(m entity.Message) {
|
||||||
|
if !config.Conf.Ai.Enable {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果在禁用的群组里面,就不处理
|
||||||
|
if slice.Contain(config.Conf.Ai.DisableGroup, m.FromUser) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 预处理一下发送的消息,用正则去掉@机器人的内容
|
||||||
|
re := regexp.MustCompile(`@([^ ]+)`)
|
||||||
|
matches := re.FindStringSubmatch(m.Content)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
// 过滤掉第一个匹配到的
|
||||||
|
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 != "" {
|
||||||
|
conf.BaseURL = fmt.Sprintf("%s/v1", config.Conf.Ai.BaseUrl)
|
||||||
|
}
|
||||||
|
client := openai.NewClientWithConfig(conf)
|
||||||
|
resp, err := client.CreateChatCompletion(
|
||||||
|
context.Background(),
|
||||||
|
openai.ChatCompletionRequest{
|
||||||
|
Model: model,
|
||||||
|
Messages: messages,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("OpenAI聊天发起失败: %v", err.Error())
|
||||||
|
utils.SendMessage(m.FromUser, m.GroupUser, "AI炸啦~", 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送消息
|
||||||
|
utils.SendMessage(m.FromUser, m.GroupUser, "\n"+resp.Choices[0].Message.Content, 0)
|
||||||
|
}
|
||||||
@@ -56,5 +56,10 @@ func Parse(remoteAddr net.Addr, msg []byte) {
|
|||||||
ent.DisplayFullContent = m.DisplayFullContent
|
ent.DisplayFullContent = m.DisplayFullContent
|
||||||
ent.Raw = string(msg)
|
ent.Raw = string(msg)
|
||||||
|
|
||||||
|
// 处理At机器人的消息
|
||||||
|
if strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你") {
|
||||||
|
go handleAtMessage(ent)
|
||||||
|
}
|
||||||
|
|
||||||
go service.SaveMessage(ent)
|
go service.SaveMessage(ent)
|
||||||
}
|
}
|
||||||
|
|||||||
93
tasks/month.go
Normal file
93
tasks/month.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package tasks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go-wechat/client"
|
||||||
|
"go-wechat/config"
|
||||||
|
"go-wechat/entity"
|
||||||
|
"go-wechat/utils"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// month
|
||||||
|
// @description: 月排行榜
|
||||||
|
func month() {
|
||||||
|
for _, id := range config.Conf.Task.WaterGroup.Groups {
|
||||||
|
// 消息统计
|
||||||
|
dealMonth(id)
|
||||||
|
// 获取上个月月份
|
||||||
|
yd := time.Now().Local().AddDate(0, 0, -1).Format("200601")
|
||||||
|
// 发送词云
|
||||||
|
fileName := fmt.Sprintf("%s_%s.png", yd, id)
|
||||||
|
utils.SendImage(id, "D:\\Share\\wordcloud\\"+fileName, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dealMonth
|
||||||
|
// @description: 处理请求
|
||||||
|
// @param gid
|
||||||
|
func dealMonth(gid string) {
|
||||||
|
notifyMsgs := []string{"#上月水群排行榜"}
|
||||||
|
|
||||||
|
// 获取上月消息总数
|
||||||
|
var yesterdayMsgCount int64
|
||||||
|
err := client.MySQL.Model(&entity.Message{}).
|
||||||
|
Where("from_user = ?", gid).
|
||||||
|
Where("`type` < 10000").
|
||||||
|
Where("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 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("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 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)
|
||||||
|
}
|
||||||
@@ -20,8 +20,16 @@ func InitTasks() {
|
|||||||
|
|
||||||
// 水群排行
|
// 水群排行
|
||||||
if config.Conf.Task.WaterGroup.Enable {
|
if config.Conf.Task.WaterGroup.Enable {
|
||||||
log.Printf("水群排行任务已启用,执行表达式: %s", config.Conf.Task.WaterGroup.Cron)
|
log.Printf("水群排行任务已启用,执行表达式: %+v", config.Conf.Task.WaterGroup.Cron)
|
||||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron).Do(yesterday)
|
if config.Conf.Task.WaterGroup.Cron.Yesterday != "" {
|
||||||
|
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Yesterday).Do(yesterday)
|
||||||
|
}
|
||||||
|
if config.Conf.Task.WaterGroup.Cron.Week != "" {
|
||||||
|
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Week).Do(week)
|
||||||
|
}
|
||||||
|
if config.Conf.Task.WaterGroup.Cron.Month != "" {
|
||||||
|
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Month).Do(month)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新好友列表
|
// 更新好友列表
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"go-wechat/utils"
|
"go-wechat/utils"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 水群排行榜
|
// 水群排行榜
|
||||||
@@ -16,7 +17,13 @@ import (
|
|||||||
// @description: 昨日排行榜
|
// @description: 昨日排行榜
|
||||||
func yesterday() {
|
func yesterday() {
|
||||||
for _, id := range config.Conf.Task.WaterGroup.Groups {
|
for _, id := range config.Conf.Task.WaterGroup.Groups {
|
||||||
|
// 消息统计
|
||||||
dealYesterday(id)
|
dealYesterday(id)
|
||||||
|
// 获取昨日日期
|
||||||
|
yd := time.Now().Local().AddDate(0, 0, -1).Format("20060102")
|
||||||
|
// 发送词云
|
||||||
|
fileName := fmt.Sprintf("%s_%s.png", yd, id)
|
||||||
|
utils.SendImage(id, "D:\\Share\\wordcloud\\"+fileName, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,6 +37,7 @@ func dealYesterday(gid string) {
|
|||||||
var yesterdayMsgCount int64
|
var yesterdayMsgCount int64
|
||||||
err := client.MySQL.Model(&entity.Message{}).
|
err := client.MySQL.Model(&entity.Message{}).
|
||||||
Where("from_user = ?", gid).
|
Where("from_user = ?", gid).
|
||||||
|
Where("`type` < 10000").
|
||||||
Where("DATEDIFF(create_at,NOW()) = -1").
|
Where("DATEDIFF(create_at,NOW()) = -1").
|
||||||
Count(&yesterdayMsgCount).Error
|
Count(&yesterdayMsgCount).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
93
tasks/week.go
Normal file
93
tasks/week.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package tasks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"go-wechat/client"
|
||||||
|
"go-wechat/config"
|
||||||
|
"go-wechat/entity"
|
||||||
|
"go-wechat/utils"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// week
|
||||||
|
// @description: 周排行榜
|
||||||
|
func week() {
|
||||||
|
for _, id := range config.Conf.Task.WaterGroup.Groups {
|
||||||
|
// 消息统计
|
||||||
|
dealWeek(id)
|
||||||
|
// 获取上周周数
|
||||||
|
year, weekNo := time.Now().Local().AddDate(0, 0, -1).ISOWeek()
|
||||||
|
// 发送词云
|
||||||
|
fileName := fmt.Sprintf("%d%d_%s.png", year, weekNo, id)
|
||||||
|
utils.SendImage(id, "D:\\Share\\wordcloud\\"+fileName, 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dealWeek
|
||||||
|
// @description: 处理请求
|
||||||
|
// @param gid
|
||||||
|
func dealWeek(gid string) {
|
||||||
|
notifyMsgs := []string{"#上周水群排行榜"}
|
||||||
|
|
||||||
|
// 获取上周消息总数
|
||||||
|
var yesterdayMsgCount int64
|
||||||
|
err := client.MySQL.Model(&entity.Message{}).
|
||||||
|
Where("from_user = ?", gid).
|
||||||
|
Where("type < 10000").
|
||||||
|
Where("YEARWEEK(date_format(create_at, '%Y-%m-%d')) = YEARWEEK(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("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(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)
|
||||||
|
}
|
||||||
@@ -18,18 +18,30 @@ func SendMessage(toId, atId, msg string, retryCount int) {
|
|||||||
log.Printf("重试五次失败,停止发送")
|
log.Printf("重试五次失败,停止发送")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 组装参数
|
// 组装参数
|
||||||
param := map[string]any{
|
param := map[string]any{
|
||||||
"wxid": toId, // 群或好友Id
|
"wxid": toId, // 群或好友Id
|
||||||
"msg": msg, // 消息
|
"msg": msg, // 消息
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 接口地址
|
||||||
|
apiUrl := config.Conf.Wechat.GetURL("/api/sendTextMsg")
|
||||||
|
if atId != "" {
|
||||||
|
apiUrl = config.Conf.Wechat.GetURL("/api/sendAtText")
|
||||||
|
param = map[string]any{
|
||||||
|
"chatRoomId": toId,
|
||||||
|
"wxids": atId,
|
||||||
|
"msg": msg, // 消息
|
||||||
|
}
|
||||||
|
}
|
||||||
pbs, _ := json.Marshal(param)
|
pbs, _ := json.Marshal(param)
|
||||||
|
|
||||||
res := resty.New()
|
res := resty.New()
|
||||||
resp, err := res.R().
|
resp, err := res.R().
|
||||||
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
SetHeader("Content-Type", "application/json;chartset=utf-8").
|
||||||
SetBody(string(pbs)).
|
SetBody(string(pbs)).
|
||||||
Post(config.Conf.Wechat.GetURL("/api/sendTextMsg"))
|
Post(apiUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("发送文本消息失败: %s", err.Error())
|
log.Printf("发送文本消息失败: %s", err.Error())
|
||||||
// 休眠五秒后重新发送
|
// 休眠五秒后重新发送
|
||||||
|
|||||||
Reference in New Issue
Block a user