From bc3622464f46e5ad85fe3f747706d9139c7c5900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Mon, 19 Aug 2024 11:44:59 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 2 +- initialization/plugin.go | 2 +- model/dto/message.go | 33 +++++++++++++++++++++++++++++++++ plugin/plugins/systemmessgae.go | 10 +++++----- tasks/friends/friends.go | 8 ++++---- utils/url.go | 21 +++++++++++++++++++++ 6 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 utils/url.go diff --git a/config.yaml b/config.yaml index 47e6274..9134afc 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 ec03654..6892613 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 110d0c7..5ccf4b4 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/systemmessgae.go b/plugin/plugins/systemmessgae.go index 0f8d620..c1c99d4 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 5db7af0..d75c44d 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 0000000..a8c9e62 --- /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 +}