diff --git a/config.yaml b/config.yaml index 47e62746..9134afcf 100644 --- a/config.yaml +++ b/config.yaml @@ -4,7 +4,7 @@ system: alApiToken: xxx # urlc.cn的Token,用来生成短链接 urlcApiToken: xxx - # 系统访问域名 + # 系统访问域名,必须是包括 http[s]:// 的完整域名 domain: https://wechat.abc.com # 添加新好友或群之后通知给指定的人 newFriendNotify: diff --git a/initialization/plugin.go b/initialization/plugin.go index ec036545..68926135 100644 --- a/initialization/plugin.go +++ b/initialization/plugin.go @@ -35,7 +35,7 @@ func Plugin() { }, plugins.NotifyRemoveFromChatroom) // 响应好友添加成功消息 dispatcher.RegisterHandler(func(m *dto.Message) bool { - return m.Type == types.MsgTypeSys + return m.IsNewFriendAdd() || m.IsJoinToGroup() || m.IsOldFriendBack() }, plugins.ReplyNewFriend) // 私聊指令消息 diff --git a/model/dto/message.go b/model/dto/message.go index 110d0c78..5ccf4b40 100644 --- a/model/dto/message.go +++ b/model/dto/message.go @@ -219,3 +219,36 @@ func (m Message) IsInvitationJoinGroup() (flag bool, str string) { } return } + +// IsNewFriendAdd +// @description: 是否是新好友添加消息 +// @receiver m +// @return flag +func (m Message) IsNewFriendAdd() (flag bool) { + if m.Type != types.MsgTypeSys { + return + } + return strings.HasPrefix(m.Content, "你已添加了") && strings.HasSuffix(m.Content, ",现在可以开始聊天了。") +} + +// IsOldFriendBack +// @description: 是否是老好友回归消息 +// @receiver m +// @return flag +func (m Message) IsOldFriendBack() (flag bool) { + if m.Type != types.MsgTypeSys { + return + } + return m.Content == "以上是打招呼的内容" +} + +// IsJoinToGroup +// @description: 是否是加入群聊消息 +// @receiver m +// @return flag +func (m Message) IsJoinToGroup() (flag bool) { + if m.Type != types.MsgTypeSys { + return + } + return strings.Contains(m.Content, "\"邀请你加入了群聊,群聊参与人还有:") +} diff --git a/plugin/plugins/ai.go b/plugin/plugins/ai.go index 814a3074..7d7410e1 100644 --- a/plugin/plugins/ai.go +++ b/plugin/plugins/ai.go @@ -19,8 +19,12 @@ import ( "time" ) +// 已经通知过的群组或者好友map var notifyMap = make(map[string]bool) +// 拉取最近消息条数 +const fetchMessageCount = 20 + // AI // @description: AI消息 // @param m @@ -198,7 +202,7 @@ func getGroupUserMessages(msgId int64, groupId, groupUserId string) (records []e Where("create_at >= DATE_SUB(NOW(),INTERVAL 30 MINUTE)"). Where(subQuery). Order("create_at desc"). - Limit(4).Find(&records) + Limit(fetchMessageCount).Find(&records) return } @@ -214,6 +218,6 @@ func getUserPrivateMessages(userId string) (records []entity.Message) { Where("create_at >= DATE_SUB(NOW(),INTERVAL 30 MINUTE)"). Where(subQuery). Order("create_at desc"). - Limit(4).Find(&records) + Limit(fetchMessageCount).Find(&records) return } diff --git a/plugin/plugins/systemmessgae.go b/plugin/plugins/systemmessgae.go index 0f8d620e..c1c99d4b 100644 --- a/plugin/plugins/systemmessgae.go +++ b/plugin/plugins/systemmessgae.go @@ -3,16 +3,16 @@ package plugins import ( "go-wechat/plugin" "go-wechat/utils" - "strings" ) // ReplyNewFriend // @description: 响应好友添加成功消息 // @param m func ReplyNewFriend(m *plugin.MessageContext) { - isNewFriend := strings.HasPrefix(m.Content, "你已添加了") && strings.HasSuffix(m.Content, ",现在可以开始聊天了。") - isNewChatroom := strings.Contains(m.Content, "\"邀请你加入了群聊,群聊参与人还有:") - if isNewFriend || isNewChatroom { - utils.SendMessage(m.FromUser, m.GroupUser, "AI正在初始化,请稍等几分钟,初始化完成之后我将主动告知您。", 0) + if m.IsNewFriendAdd() || m.IsJoinToGroup() { + _ = utils.SendMessage(m.FromUser, m.GroupUser, "AI正在初始化,请稍等几分钟,初始化完成之后我将主动告知您。", 0) + } + if m.IsOldFriendBack() { + _ = utils.SendMessage(m.FromUser, m.GroupUser, "嘿,我的朋友,你为何要离我而去?又为何去而复返?", 0) } } diff --git a/tasks/friends/friends.go b/tasks/friends/friends.go index 5db7af05..d75c44da 100644 --- a/tasks/friends/friends.go +++ b/tasks/friends/friends.go @@ -110,9 +110,9 @@ func Sync() { } // 发送配置网页 if config.Conf.System.Domain != "" { - title := "欢迎使用微信机器人" + title := "欢迎使用微信机器人(切勿转发)" desc := "点我可以配置功能喔,提示非微信官方网页,点击继续访问即可" - url := config.Conf.System.Domain + "/manager.html?id=" + friend.Wxid + url := utils.GetManagerUrl(friend.Wxid) utils.SendPublicMsg(friend.Wxid, title, desc, url, 0) } @@ -150,9 +150,9 @@ func Sync() { } // 发送配置网页 if config.Conf.System.Domain != "" { - title := "欢迎使用微信机器人" + title := "欢迎使用微信机器人(切勿转发)" desc := "点我可以配置功能喔,提示非微信官方网页,点击继续访问即可" - url := config.Conf.System.Domain + "/manager.html?id=" + friend.Wxid + url := utils.GetManagerUrl(friend.Wxid) utils.SendPublicMsg(friend.Wxid, title, desc, url, 0) } } diff --git a/utils/url.go b/utils/url.go new file mode 100644 index 00000000..a8c9e621 --- /dev/null +++ b/utils/url.go @@ -0,0 +1,21 @@ +package utils + +import ( + "encoding/base64" + "fmt" + "go-wechat/config" +) + +// GetManagerUrl +// @description: 获取管理页面链接 +// @param wxId +// @return newUrl +func GetManagerUrl(wxId string) (url string) { + // 生成管理页面链接 + url = fmt.Sprintf("%s/manager.html?id=%s", config.Conf.System.Domain, wxId) + // base64一下 + encodeString := base64.StdEncoding.EncodeToString([]byte(url)) + // 拼接新链接(这个是一个已备案的域名) + url = "https://redirect.wjg95.cn/?s=" + encodeString + return +} diff --git a/views/manager-one.html b/views/manager-one.html index f934a7b3..eb3cab53 100644 --- a/views/manager-one.html +++ b/views/manager-one.html @@ -83,7 +83,12 @@
需要扩容请添加微信: _Elixir
+