From e3bb1155609d6495d53f03dec51e393fed111651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Tue, 28 Nov 2023 16:09:36 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= =?UTF-8?q?=EF=BC=8C=E5=90=AF=E7=94=A8AI=E5=92=8C=E6=B0=B4=E7=BE=A4?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E6=94=B9=E4=B8=BA=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E9=85=8D=E7=BD=AE(=E6=9C=AA=E5=85=A8=E9=83=A8=E5=AE=8C?= =?UTF-8?q?=E6=88=90)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 10 ---------- config/ai.go | 11 +++++------ config/task.go | 6 ++---- entity/friend.go | 27 +++++++++++++++------------ handler/at_message.go | 19 ++++++++++++++++--- readme.md | 13 +++++-------- service/friend.go | 23 +++++++++++++++++++++++ tasks/month.go | 31 ++++++++++++++----------------- tasks/water_group.go | 31 ++++++++++++++----------------- tasks/week.go | 31 ++++++++++++++----------------- 10 files changed, 108 insertions(+), 94 deletions(-) create mode 100644 service/friend.go diff --git a/config.yaml b/config.yaml index c77feee2..a0bf77ab 100644 --- a/config.yaml +++ b/config.yaml @@ -26,13 +26,6 @@ task: yesterday: '30 9 * * *' # 每天9:30 week: '30 9 * * 1' # 每周一9:30 month: '30 9 1 * *' # 每月1号9:30 - # 需要发送水群排行榜的群Id - groups: - - '18958257758@chatroom' - - '49448748645@chatroom' - # 不计入统计范围的用户Id - blacklist: - - 'wxid_7788687886912' # AI回复 ai: @@ -46,6 +39,3 @@ ai: baseUrl: https://sxxx # 人设 personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。 - # 禁用的群 - disableGroup: - - 49448748645@chatroom \ No newline at end of file diff --git a/config/ai.go b/config/ai.go index 3769daf8..b27e3eaa 100644 --- a/config/ai.go +++ b/config/ai.go @@ -3,10 +3,9 @@ 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"` // 禁用群组 + 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"` // 人设 } diff --git a/config/task.go b/config/task.go index ff9caa2e..151ddc7b 100644 --- a/config/task.go +++ b/config/task.go @@ -18,10 +18,8 @@ type syncFriends struct { // 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"` // 黑名单 + Enable bool `json:"enable" yaml:"enable"` // 是否启用 + Cron waterGroupCron `json:"cron" yaml:"cron"` // 定时任务表达式 } // waterGroupCron diff --git a/entity/friend.go b/entity/friend.go index 0508b267..7c02bbc8 100644 --- a/entity/friend.go +++ b/entity/friend.go @@ -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 { diff --git a/handler/at_message.go b/handler/at_message.go index 982fd6ff..a2a158f6 100644 --- a/handler/at_message.go +++ b/handler/at_message.go @@ -3,10 +3,10 @@ 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/service" "go-wechat/utils" "log" "regexp" @@ -21,8 +21,21 @@ func handleAtMessage(m entity.Message) { return } - // 如果在禁用的群组里面,就不处理 - if slice.Contain(config.Conf.Ai.DisableGroup, m.FromUser) { + // 取出所有启用了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 } diff --git a/readme.md b/readme.md index 12503890..87e2b4c6 100644 --- a/readme.md +++ b/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`文件 diff --git a/service/friend.go b/service/friend.go new file mode 100644 index 00000000..f80cc870 --- /dev/null +++ b/service/friend.go @@ -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 +} diff --git a/tasks/month.go b/tasks/month.go index 3f6ea24f..04cd9a68 100644 --- a/tasks/month.go +++ b/tasks/month.go @@ -3,8 +3,8 @@ package tasks import ( "fmt" "go-wechat/client" - "go-wechat/config" "go-wechat/entity" + "go-wechat/service" "go-wechat/utils" "log" "strings" @@ -14,14 +14,20 @@ import ( // month // @description: 月排行榜 func month() { - for _, id := range config.Conf.Task.WaterGroup.Groups { + groups, err := service.GetAllEnableChatRank() + if err != nil { + log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err) + return + } + + for _, group := range groups { // 消息统计 - dealMonth(id) + dealMonth(group.Wxid) // 获取上个月月份 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) + fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid) + utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0) } } @@ -58,23 +64,14 @@ func dealMonth(gid string) { } 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"). + err = 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 = ?", 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 + Limit(10).Find(&records).Error if err != nil { log.Printf("获取上月消息失败, 错误信息: %v", err) diff --git a/tasks/water_group.go b/tasks/water_group.go index f877bf1d..aa6bee85 100644 --- a/tasks/water_group.go +++ b/tasks/water_group.go @@ -3,8 +3,8 @@ package tasks import ( "fmt" "go-wechat/client" - "go-wechat/config" "go-wechat/entity" + "go-wechat/service" "go-wechat/utils" "log" "strings" @@ -16,14 +16,20 @@ import ( // yesterday // @description: 昨日排行榜 func yesterday() { - for _, id := range config.Conf.Task.WaterGroup.Groups { + groups, err := service.GetAllEnableChatRank() + if err != nil { + log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err) + return + } + + for _, group := range groups { // 消息统计 - dealYesterday(id) + dealYesterday(group.Wxid) // 获取昨日日期 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) + fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid) + utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0) } } @@ -60,23 +66,14 @@ func dealYesterday(gid string) { } 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"). + err = 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 = ?", 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 + Limit(10).Find(&records).Error if err != nil { log.Printf("获取昨日消息失败, 错误信息: %v", err) diff --git a/tasks/week.go b/tasks/week.go index 588a73b1..aa52bf35 100644 --- a/tasks/week.go +++ b/tasks/week.go @@ -3,8 +3,8 @@ package tasks import ( "fmt" "go-wechat/client" - "go-wechat/config" "go-wechat/entity" + "go-wechat/service" "go-wechat/utils" "log" "strings" @@ -14,14 +14,20 @@ import ( // week // @description: 周排行榜 func week() { - for _, id := range config.Conf.Task.WaterGroup.Groups { + groups, err := service.GetAllEnableChatRank() + if err != nil { + log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err) + return + } + + for _, group := range groups { // 消息统计 - dealWeek(id) + dealWeek(group.Wxid) // 获取上周周数 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) + fileName := fmt.Sprintf("%d%d_%s.png", year, weekNo, group.Wxid) + utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0) } } @@ -58,23 +64,14 @@ func dealWeek(gid string) { } 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"). + err = 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 = ?", 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 + Limit(10).Find(&records).Error if err != nil { log.Printf("获取上周消息失败, 错误信息: %v", err)