Merge pull request 'hotfix' (#102) from hotfix into main
All checks were successful
BuildImage / build-image (push) Successful in 1m46s

Reviewed-on: #102
This commit is contained in:
李寻欢 2024-08-21 15:53:30 +08:00
commit 0dc79274c3
9 changed files with 411 additions and 24 deletions

View File

@ -61,9 +61,9 @@ task:
hotTop:
enable: true
cron: '0 */1 * * *' # 每小时一次
syncFriends:
syncFriends: # 逻辑已修改,每天大半夜同步一次即可(主要是用以修正数据),平时会根据收到的消息针对性同步
enable: false
cron: '*/5 * * * *' # 五分钟一次
cron: '0 4 * * *' # 每天4:00
groupSummary:
enable: false
cron: '30 0 * * *' # 每天0:30

View File

@ -6,7 +6,6 @@ import (
plugin "go-wechat/plugin"
"go-wechat/plugin/plugins"
"go-wechat/service"
"go-wechat/types"
)
// Plugin
@ -31,7 +30,7 @@ func Plugin() {
}, plugins.NotifyInvitationJoinGroup)
// 被移除群聊通知到配置用户
dispatcher.RegisterHandler(func(m *dto.Message) bool {
return m.Type == types.MsgTypeSys
return m.IsRemoveFromChatroom()
}, plugins.NotifyRemoveFromChatroom)
// 响应好友添加成功消息
dispatcher.RegisterHandler(func(m *dto.Message) bool {

View File

@ -252,3 +252,14 @@ func (m Message) IsJoinToGroup() (flag bool) {
}
return strings.Contains(m.Content, "\"邀请你加入了群聊,群聊参与人还有:")
}
// IsRemoveFromChatroom
// @description: 是否是被移出群聊消息
// @receiver m
// @return flag
func (m Message) IsRemoveFromChatroom() (flag bool) {
if m.Type != types.MsgTypeSys {
return
}
return strings.HasPrefix(m.Content, "你被\"") && strings.HasSuffix(m.Content, "\"移出群聊")
}

View File

@ -43,7 +43,7 @@ func AI(m *plugin.MessageContext) {
if !friendInfo.EnableAi {
return
}
if friendInfo.AiUsedToday > 0 && friendInfo.AiUsedToday >= friendInfo.AiFreeLimit {
if friendInfo.AiFreeLimit > 0 && friendInfo.AiUsedToday >= friendInfo.AiFreeLimit {
if notifyMap[m.FromUser] {
return
}

View File

@ -2,11 +2,12 @@ package plugins
import (
"fmt"
"go-wechat/client"
"go-wechat/config"
"go-wechat/model/entity"
"go-wechat/plugin"
"go-wechat/service"
"go-wechat/utils"
"strings"
)
// NotifyInvitationJoinGroup
@ -14,7 +15,7 @@ import (
// @param m
func NotifyInvitationJoinGroup(m *plugin.MessageContext) {
// 先回复一条固定句子
utils.SendMessage(m.FromUser, m.GroupUser, "您的邀请消息已收到啦,正在通知我的主人来同意请求。在我加群之后将会进行初始化操作,直到收到我主动发送的消息就是初始化完成咯,在那之前请耐心等待喔~", 0)
_ = utils.SendMessage(m.FromUser, m.GroupUser, "您的邀请消息已收到啦,正在通知我的主人来同意请求。在我加群之后将会进行初始化操作,直到收到我主动发送的消息就是初始化完成咯,在那之前请耐心等待喔~", 0)
// 如果是邀请进群,推送到配置的用户
if flag, dec := m.IsInvitationJoinGroup(); flag {
@ -32,8 +33,6 @@ func NotifyInvitationJoinGroup(m *plugin.MessageContext) {
// @description: 被移除群聊通知到配置用户
// @param m
func NotifyRemoveFromChatroom(m *plugin.MessageContext) {
// 如果是被移出群聊,推送到配置的用户
if strings.HasPrefix(m.Content, "你被\"") && strings.HasSuffix(m.Content, "\"移出群聊") {
// 调用一下退出群聊接口,防止被移出后还能从好友列表接口拉到相关信息
utils.QuitChatroom(m.FromUser, 0)
@ -48,8 +47,10 @@ func NotifyRemoveFromChatroom(m *plugin.MessageContext) {
for _, user := range config.Conf.System.NewFriendNotify.ToUser {
if user != "" {
// 发送一条新消息
utils.SendMessage(user, "", msg, 0)
}
_ = utils.SendMessage(user, "", msg, 0)
}
}
// 修改is_ok状态
client.MySQL.Model(&entity.Friend{}).Where("wxid = ?", m.FromUser).Update("is_ok", false)
}

View File

@ -2,6 +2,7 @@ package plugins
import (
"go-wechat/plugin"
"go-wechat/service"
"go-wechat/utils"
)
@ -15,4 +16,13 @@ func ReplyNewFriend(m *plugin.MessageContext) {
if m.IsOldFriendBack() {
_ = utils.SendMessage(m.FromUser, m.GroupUser, "嘿,我的朋友,你为何要离我而去?又为何去而复返?", 0)
}
go func() {
// 刷新好友列表
service.SyncFriend()
// 如果是加入群,刷新群成员列表
if m.IsJoinToGroup() {
service.SyncGroupMembers(m.FromUser)
}
}()
}

View File

@ -5,6 +5,7 @@ import (
"go-wechat/config"
"go-wechat/model/entity"
"go-wechat/plugin"
"go-wechat/service"
"go-wechat/utils"
)
@ -28,7 +29,7 @@ func WelcomeNew(m *plugin.MessageContext) {
switch conf.Type {
case "text":
// 文字类型
utils.SendMessage(m.FromUser, "", conf.Path, 0)
_ = utils.SendMessage(m.FromUser, "", conf.Path, 0)
case "image":
// 图片类型
utils.SendImage(m.FromUser, conf.Path, 0)
@ -36,4 +37,7 @@ func WelcomeNew(m *plugin.MessageContext) {
// 表情类型
utils.SendEmotion(m.FromUser, conf.Path, 0)
}
// 刷新群成员列表
go service.SyncGroupMembers(m.FromUser)
}

257
service/friendsync.go Normal file
View File

@ -0,0 +1,257 @@
package service
import (
"go-wechat/client"
"go-wechat/common/types"
"go-wechat/config"
"go-wechat/model/entity"
"go-wechat/utils"
"log"
"strings"
"time"
)
// SyncFriend
// @description: 同步好友列表
func SyncFriend() {
// 获取好友列表
friends, err := utils.GetFriendList()
if err != nil {
log.Printf("获取好友列表失败: %s", err.Error())
return
}
// 当前获取到的成员Id用于后续设置is_ok状态
nowIds := make([]string, 0)
// 取出已存在的成员信息
var oldData []entity.Friend
err = client.MySQL.Find(&oldData).Error
if err != nil {
log.Printf("查询好友列表失败: %s", err.Error())
return
}
// 将历史数据整理成map
oldMap := make(map[string]bool)
for _, item := range oldData {
oldMap[item.Wxid] = item.IsOk
}
// 新增的成员,用于通知给指定的人
var notifyMap = make(map[string]string)
// 开启事务
tx := client.MySQL.Begin()
defer tx.Commit()
// 循环获取到的好友列表
for _, item := range friends {
// 填充当前存在的账号
nowIds = append(nowIds, item.Wxid)
// 判断是否已经存在
if _, ok := oldMap[item.Wxid]; ok {
// 已存在,修改
pm := map[string]any{
"nickname": item.Nickname,
"custom_account": item.CustomAccount,
"pinyin": item.Pinyin,
"pinyin_all": item.PinyinAll,
"is_ok": true,
}
err = tx.Model(&entity.Friend{}).Where("wxid = ?", item.Wxid).Updates(pm).Error
if err != nil {
log.Printf("修改好友失败: %s", err.Error())
continue
}
// 如果已存在但是是已退出的群,也通知一下
if !oldMap[item.Wxid] {
notifyMap[item.Wxid] = item.Nickname + " #秽土转生"
// 通知一下,初始化完成
if conf, e := config.Conf.Resource["introduce"]; e {
// 发送一条新消息
switch conf.Type {
case "text":
// 文字类型
_ = utils.SendMessage(item.Wxid, "", conf.Path, 0)
case "image":
// 图片类型
utils.SendImage(item.Wxid, conf.Path, 0)
case "emotion":
// 表情类型
utils.SendEmotion(item.Wxid, conf.Path, 0)
}
}
// 发送配置网页
if config.Conf.System.Domain != "" {
title := "欢迎使用微信机器人(切勿转发)"
desc := "点我可以配置功能喔,提示非微信官方网页,点击继续访问即可"
url := utils.GetManagerUrl(item.Wxid)
utils.SendPublicMsg(item.Wxid, title, desc, url, 0)
}
}
} else {
// 新增
err = tx.Create(&entity.Friend{
CustomAccount: item.CustomAccount,
Nickname: item.Nickname,
Pinyin: item.Pinyin,
PinyinAll: item.PinyinAll,
Wxid: item.Wxid,
IsOk: true,
EnableAi: config.Conf.System.DefaultRule.Ai,
EnableChatRank: config.Conf.System.DefaultRule.ChatRank,
EnableSummary: config.Conf.System.DefaultRule.Summary,
EnableWelcome: config.Conf.System.DefaultRule.Welcome,
EnableNews: config.Conf.System.DefaultRule.News,
EnableHotTop: config.Conf.System.DefaultRule.HotTop,
AiFreeLimit: config.Conf.System.DefaultRule.AiFreeLimit,
ClearMember: 0,
LastActive: types.DateTime(time.Now().Local()),
}).Error
if err != nil {
log.Printf("新增好友失败: %s", err.Error())
continue
}
notifyMap[item.Wxid] = item.Nickname
if conf, e := config.Conf.Resource["introduce"]; e {
// 发送一条新消息
switch conf.Type {
case "text":
// 文字类型
_ = utils.SendMessage(item.Wxid, "", conf.Path, 0)
case "image":
// 图片类型
utils.SendImage(item.Wxid, conf.Path, 0)
case "emotion":
// 表情类型
utils.SendEmotion(item.Wxid, conf.Path, 0)
}
}
// 发送配置网页
if config.Conf.System.Domain != "" {
title := "欢迎使用微信机器人(切勿转发)"
desc := "点我可以配置功能喔,提示非微信官方网页,点击继续访问即可"
url := utils.GetManagerUrl(item.Wxid)
utils.SendPublicMsg(item.Wxid, title, desc, url, 0)
}
}
}
// 通知有新成员
if len(notifyMap) > 0 && config.Conf.System.NewFriendNotify.Enable {
// 组装成一句话
msg := []string{"#新好友通知\n"}
for wxId, nickname := range notifyMap {
msg = append(msg, "微信Id: "+wxId+"\n昵称: "+nickname)
}
for _, user := range config.Conf.System.NewFriendNotify.ToUser {
if user != "" {
// 发送一条新消息
_ = utils.SendMessage(user, "", strings.Join(msg, "\n-------\n"), 0)
}
}
}
// 清理不在列表中的好友
clearPm := map[string]any{
"is_ok": false,
}
err = tx.Model(&entity.Friend{}).Where("wxid NOT IN (?)", nowIds).Updates(clearPm).Error
if err != nil {
log.Printf("清理好友失败: %s", err.Error())
}
log.Println("同步好友列表完成")
}
// SyncGroupMembers
// @description: 同步群成员
// @param wxId
func SyncGroupMembers(wxId string) {
membersIds, admin, err := utils.GetGroupMembers(wxId)
if err != nil {
return
}
// 修改不在数组的群成员状态为不在
pm := map[string]any{
"is_member": false,
"leave_time": time.Now().Local(),
}
err = client.MySQL.Model(&entity.GroupUser{}).
Where("group_id = ?", wxId).
Where("is_member IS TRUE").
Where("wxid NOT IN (?)", membersIds).
Updates(pm).Error
if err != nil {
log.Printf("修改群成员状态失败: %s", err.Error())
return
}
// 取出当前数据库存在的成员信息
var oldData []entity.GroupUser
err = client.MySQL.Model(&entity.GroupUser{}).
Where("group_id = ?", wxId).
Find(&oldData).Error
if err != nil {
log.Printf("查询群成员失败: %s", err.Error())
return
}
// 将历史数据整理成map
oldMap := make(map[string]bool)
for _, item := range oldData {
oldMap[item.Wxid] = item.IsMember
}
// 循环获取到的群成员列表
for _, wxid := range membersIds {
// 如果历史数据中存在,且是成员,跳过
if isMember, ok := oldMap[wxid]; ok && isMember {
continue
}
// 获取成员信息
cp, e := utils.GetContactProfile(wxid)
if e != nil {
log.Printf("获取成员信息失败: %s", e.Error())
continue
}
if cp.Wxid != "" {
if _, ok := oldMap[wxid]; ok {
// 历史数据中存在,修改
// 修改
gupm := map[string]any{
"account": cp.Account,
"head_image": cp.HeadImage,
"nickname": cp.Nickname,
"is_member": true,
"is_admin": cp.Wxid == admin,
"leave_time": nil,
}
err = client.MySQL.Model(&entity.GroupUser{}).Where("group_id = ?", wxId).Where("wxid = ?", wxid).Updates(gupm).Error
if err != nil {
log.Printf("修改群成员失败: %s", err.Error())
continue
}
} else {
// 新增的
// 新增
err = client.MySQL.Create(&entity.GroupUser{
GroupId: wxid,
Account: cp.Account,
HeadImage: cp.HeadImage,
Nickname: cp.Nickname,
Wxid: cp.Wxid,
IsMember: true,
IsAdmin: cp.Wxid == admin,
JoinTime: time.Now().Local(),
LastActive: time.Now().Local(),
}).Error
if err != nil {
log.Printf("新增群成员失败: %s", err.Error())
continue
}
}
}
}
}

105
utils/friend.go Normal file
View File

@ -0,0 +1,105 @@
package utils
import (
"encoding/json"
"github.com/go-resty/resty/v2"
"go-wechat/common/constant"
"go-wechat/config"
"go-wechat/model/dto"
"log"
"slices"
"strings"
)
// http客户端
var hc = resty.New()
// GetFriendList
// @description: 获取好友列表
// @return friends
// @return err
func GetFriendList() (friends []dto.FriendItem, err error) {
var base dto.Response[[]dto.FriendItem]
resp, err := hc.R().
SetHeader("Content-Type", "application/json;chartset=utf-8").
SetResult(&base).
Post(config.Conf.Wechat.GetURL("/api/getContactList"))
if err != nil {
log.Printf("获取好友列表失败: %s", err.Error())
return
}
log.Printf("获取好友列表结果: %s", resp.String())
// 循环获取到的好友列表
for _, item := range base.Data {
// 跳过特殊账号
// 跳过公众号和企业微信好友
if strings.Contains(item.Wxid, "gh_") || strings.Contains(item.Wxid, "@openim") {
continue
}
// 特殊Id跳过
if slices.Contains(constant.SpecialId, item.Wxid) {
continue
}
// 添加到待返回列表
friends = append(friends, item)
}
return
}
// GetGroupMembers
// @description: 获取指定群成员
// @param wxId 群Id
// @return ids 群成员id数组
// @return members 群成员信息
// @return err 错误信息
func GetGroupMembers(wxId string) (ids []string, admin string, err error) {
var base dto.Response[dto.GroupUser]
// 组装参数
param := map[string]any{
"chatRoomId": wxId, // 群Id
}
pbs, _ := json.Marshal(param)
_, err = hc.R().
SetHeader("Content-Type", "application/json;chartset=utf-8").
SetBody(string(pbs)).
SetResult(&base).
Post(config.Conf.Wechat.GetURL("/api/getMemberFromChatRoom"))
if err != nil {
log.Printf("获取群成员信息失败: %s", err.Error())
return
}
// 昵称Id
ids = strings.Split(base.Data.Members, "^G")
admin = base.Data.Admin
return
}
// GetContactProfile
// @description: 获取联系人信息
// @param wxId 微信Id
// @return info 联系人信息
// @return err 错误信息
func GetContactProfile(wxId string) (info dto.ContactProfile, err error) {
var baseResp dto.Response[dto.ContactProfile]
// 组装参数
param := map[string]any{
"wxid": wxId, // 群Id
}
pbs, _ := json.Marshal(param)
_, err = hc.R().
SetHeader("Content-Type", "application/json;chartset=utf-8").
SetBody(string(pbs)).
SetResult(&baseResp).
Post(config.Conf.Wechat.GetURL("/api/getContactProfile"))
if err != nil {
log.Printf("获取成员详情失败: %s", err.Error())
return
}
info = baseResp.Data
return
}