🎨 逻辑优化,启用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
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

View File

@ -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"` // 人设
}

View File

@ -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

View File

@ -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 {

View File

@ -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
}

View File

@ -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
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 (
"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)

View File

@ -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)

View File

@ -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)