🎨 逻辑优化,启用AI和水群统计改为数据库配置(未全部完成)

This commit is contained in:
李寻欢 2023-11-28 16:09:36 +08:00
parent 3b4862d5bc
commit e3bb115560
10 changed files with 108 additions and 94 deletions

View File

@ -26,13 +26,6 @@ task:
yesterday: '30 9 * * *' # 每天9:30 yesterday: '30 9 * * *' # 每天9:30
week: '30 9 * * 1' # 每周一9:30 week: '30 9 * * 1' # 每周一9:30
month: '30 9 1 * *' # 每月1号9:30 month: '30 9 1 * *' # 每月1号9:30
# 需要发送水群排行榜的群Id
groups:
- '18958257758@chatroom'
- '49448748645@chatroom'
# 不计入统计范围的用户Id
blacklist:
- 'wxid_7788687886912'
# AI回复 # AI回复
ai: ai:
@ -46,6 +39,3 @@ ai:
baseUrl: https://sxxx baseUrl: https://sxxx
# 人设 # 人设
personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。 personality: 你的名字叫张三,你是一个百科机器人,你的爱好是看电影,你的性格是开朗的,你的专长是讲故事,你的梦想是当一名童话故事作家。你对政治没有一点儿兴趣,也不会讨论任何与政治相关的话题,你甚至可以拒绝回答这一类话题。
# 禁用的群
disableGroup:
- 49448748645@chatroom

View File

@ -3,10 +3,9 @@ package config
// ai // ai
// @description: AI配置 // @description: AI配置
type ai struct { type ai struct {
Enable bool `json:"enable" yaml:"enable"` // 是否启用AI Enable bool `json:"enable" yaml:"enable"` // 是否启用AI
Model string `json:"model" yaml:"model"` // 模型 Model string `json:"model" yaml:"model"` // 模型
ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key ApiKey string `json:"apiKey" yaml:"apiKey"` // API Key
BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址 BaseUrl string `json:"baseUrl" yaml:"baseUrl"` // API地址
Personality string `json:"personality" yaml:"personality"` // 人设 Personality string `json:"personality" yaml:"personality"` // 人设
DisableGroup []string `json:"disableGroup" yaml:"disableGroup"` // 禁用群组
} }

View File

@ -18,10 +18,8 @@ type syncFriends struct {
// waterGroup // waterGroup
// @description: 水群排行榜 // @description: 水群排行榜
type waterGroup struct { type waterGroup struct {
Enable bool `json:"enable" yaml:"enable"` // 是否启用 Enable bool `json:"enable" yaml:"enable"` // 是否启用
Cron waterGroupCron `json:"cron" yaml:"cron"` // 定时任务表达式 Cron waterGroupCron `json:"cron" yaml:"cron"` // 定时任务表达式
Groups []string `json:"groups" yaml:"groups"` // 启用的群Id
Blacklist []string `json:"blacklist" yaml:"blacklist"` // 黑名单
} }
// waterGroupCron // waterGroupCron

View File

@ -5,11 +5,13 @@ import "time"
// Friend // Friend
// @description: 好友列表 // @description: 好友列表
type Friend struct { type Friend struct {
CustomAccount string `json:"customAccount"` // 微信号 CustomAccount string `json:"customAccount"` // 微信号
Nickname string `json:"nickname"` // 昵称 Nickname string `json:"nickname"` // 昵称
Pinyin string `json:"pinyin"` // 昵称拼音大写首字母 Pinyin string `json:"pinyin"` // 昵称拼音大写首字母
PinyinAll string `json:"pinyinAll"` // 昵称全拼 PinyinAll string `json:"pinyinAll"` // 昵称全拼
Wxid string `json:"wxid"` // 微信原始Id 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 { func (Friend) TableName() string {
@ -19,13 +21,14 @@ func (Friend) TableName() string {
// GroupUser // GroupUser
// @description: 群成员 // @description: 群成员
type GroupUser struct { type GroupUser struct {
GroupId string `json:"groupId"` // 群Id GroupId string `json:"groupId"` // 群Id
Account string `json:"account"` // 账号 Account string `json:"account"` // 账号
HeadImage string `json:"headImage"` // 头像 HeadImage string `json:"headImage"` // 头像
Nickname string `json:"nickname"` // 昵称 Nickname string `json:"nickname"` // 昵称
Wxid string `json:"wxid"` // 微信Id Wxid string `json:"wxid"` // 微信Id
IsMember bool `json:"isMember" gorm:"type:tinyint(1)"` // 是否群成员 IsMember bool `json:"isMember" gorm:"type:tinyint(1) default 0 not null"` // 是否群成员
LeaveTime *time.Time `json:"leaveTime"` // 离开时间 LeaveTime *time.Time `json:"leaveTime"` // 离开时间
SkipChatRank bool `json:"skipChatRank" gorm:"type:tinyint(1) default 0 not null"` // 是否跳过聊天排行
} }
func (GroupUser) TableName() string { func (GroupUser) TableName() string {

View File

@ -3,10 +3,10 @@ package handler
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/duke-git/lancet/v2/slice"
"github.com/sashabaranov/go-openai" "github.com/sashabaranov/go-openai"
"go-wechat/config" "go-wechat/config"
"go-wechat/entity" "go-wechat/entity"
"go-wechat/service"
"go-wechat/utils" "go-wechat/utils"
"log" "log"
"regexp" "regexp"
@ -21,8 +21,21 @@ func handleAtMessage(m entity.Message) {
return return
} }
// 如果在禁用的群组里面,就不处理 // 取出所有启用了AI的好友或群组
if slice.Contain(config.Conf.Ai.DisableGroup, m.FromUser) { 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 return
} }

View File

@ -7,7 +7,7 @@ cd wechat-hook
1. 创建配置文件`config.yaml` 1. 创建配置文件`config.yaml`
```shell ```shell
mkdir config # 先创建一个文件夹保存配置文件,文件名不要变 mkdir config # 先创建一个文件夹保存配置文件,文件名不要变
vim config.yaml # 编辑配置文件,内容如下 vim config.yaml # 编辑配置文件,内容如下最新配置请参考项目里的config.yaml文件
``` ```
```yaml ```yaml
# 微信HOOK配置 # 微信HOOK配置
@ -34,13 +34,10 @@ task:
cron: '0 * * * *' cron: '0 * * * *'
waterGroup: waterGroup:
enable: true enable: true
cron: '30 9 * * *' cron:
# 需要发送水群排行榜的群Id yesterday: '30 9 * * *' # 每天9:30
groups: week: '30 9 * * 1' # 每周一9:30
- '11111@chatroom' # 自行配置 month: '30 9 1 * *' # 每月1号9:30
# 不计入统计范围的用户Id
blacklist:
- 'wxid_xxxx' # 自行配置
``` ```
2. 创建`docker-compose.yaml`文件 2. 创建`docker-compose.yaml`文件

23
service/friend.go Normal file
View 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
}

View File

@ -3,8 +3,8 @@ package tasks
import ( import (
"fmt" "fmt"
"go-wechat/client" "go-wechat/client"
"go-wechat/config"
"go-wechat/entity" "go-wechat/entity"
"go-wechat/service"
"go-wechat/utils" "go-wechat/utils"
"log" "log"
"strings" "strings"
@ -14,14 +14,20 @@ import (
// month // month
// @description: 月排行榜 // @description: 月排行榜
func month() { 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") yd := time.Now().Local().AddDate(0, 0, -1).Format("200601")
// 发送词云 // 发送词云
fileName := fmt.Sprintf("%s_%s.png", yd, id) fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
utils.SendImage(id, "D:\\Share\\wordcloud\\"+fileName, 0) utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
} }
} }
@ -58,23 +64,14 @@ func dealMonth(gid string) {
} }
var records []record var records []record
tx := client.MySQL.Table("t_message AS tm"). 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"). 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`"). Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("tm.from_user = ?", gid). Where("tm.from_user = ?", gid).
Where("tm.type < 10000"). Where("tm.type < 10000").
Where("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 1"). Where("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 1").
Group("tm.group_user, tgu.nickname").Order("`count` DESC"). Group("tm.group_user, tgu.nickname").Order("`count` DESC").
Limit(10) Limit(10).Find(&records).Error
// 黑名单
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 { if err != nil {
log.Printf("获取上月消息失败, 错误信息: %v", err) log.Printf("获取上月消息失败, 错误信息: %v", err)

View File

@ -3,8 +3,8 @@ package tasks
import ( import (
"fmt" "fmt"
"go-wechat/client" "go-wechat/client"
"go-wechat/config"
"go-wechat/entity" "go-wechat/entity"
"go-wechat/service"
"go-wechat/utils" "go-wechat/utils"
"log" "log"
"strings" "strings"
@ -16,14 +16,20 @@ import (
// yesterday // yesterday
// @description: 昨日排行榜 // @description: 昨日排行榜
func yesterday() { 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") yd := time.Now().Local().AddDate(0, 0, -1).Format("20060102")
// 发送词云 // 发送词云
fileName := fmt.Sprintf("%s_%s.png", yd, id) fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
utils.SendImage(id, "D:\\Share\\wordcloud\\"+fileName, 0) utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
} }
} }
@ -60,23 +66,14 @@ func dealYesterday(gid string) {
} }
var records []record var records []record
tx := client.MySQL.Table("t_message AS tm"). 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"). 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`"). Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("tm.from_user = ?", gid). Where("tm.from_user = ?", gid).
Where("tm.type < 10000"). Where("tm.type < 10000").
Where("DATEDIFF(tm.create_at,NOW()) = -1"). Where("DATEDIFF(tm.create_at,NOW()) = -1").
Group("tm.group_user, tgu.nickname").Order("`count` DESC"). Group("tm.group_user, tgu.nickname").Order("`count` DESC").
Limit(10) Limit(10).Find(&records).Error
// 黑名单
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 { if err != nil {
log.Printf("获取昨日消息失败, 错误信息: %v", err) log.Printf("获取昨日消息失败, 错误信息: %v", err)

View File

@ -3,8 +3,8 @@ package tasks
import ( import (
"fmt" "fmt"
"go-wechat/client" "go-wechat/client"
"go-wechat/config"
"go-wechat/entity" "go-wechat/entity"
"go-wechat/service"
"go-wechat/utils" "go-wechat/utils"
"log" "log"
"strings" "strings"
@ -14,14 +14,20 @@ import (
// week // week
// @description: 周排行榜 // @description: 周排行榜
func week() { 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() year, weekNo := time.Now().Local().AddDate(0, 0, -1).ISOWeek()
// 发送词云 // 发送词云
fileName := fmt.Sprintf("%d%d_%s.png", year, weekNo, id) fileName := fmt.Sprintf("%d%d_%s.png", year, weekNo, group.Wxid)
utils.SendImage(id, "D:\\Share\\wordcloud\\"+fileName, 0) utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
} }
} }
@ -58,23 +64,14 @@ func dealWeek(gid string) {
} }
var records []record var records []record
tx := client.MySQL.Table("t_message AS tm"). 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"). 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`"). Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("tm.from_user = ?", gid). Where("tm.from_user = ?", gid).
Where("tm.type < 10000"). Where("tm.type < 10000").
Where("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(now()) - 1"). Where("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(now()) - 1").
Group("tm.group_user, tgu.nickname").Order("`count` DESC"). Group("tm.group_user, tgu.nickname").Order("`count` DESC").
Limit(10) Limit(10).Find(&records).Error
// 黑名单
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 { if err != nil {
log.Printf("获取上周消息失败, 错误信息: %v", err) log.Printf("获取上周消息失败, 错误信息: %v", err)