From 2918924107235e260b8af34c6edf46eed8fe7f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Tue, 9 Jan 2024 11:11:09 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8DAI=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E4=BC=9A=E5=93=8D=E5=BA=94`@=E6=89=80=E6=9C=89=E4=BA=BA`?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- initialization/plugin.go | 4 ++-- model/message.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/initialization/plugin.go b/initialization/plugin.go index b65e21e..269dc1f 100644 --- a/initialization/plugin.go +++ b/initialization/plugin.go @@ -26,13 +26,13 @@ func Plugin() { // 私聊指令消息 dispatcher.RegisterHandler(func(m *model.Message) bool { // 私聊消息 或 群聊艾特机器人并且以/开头的消息 - return (m.IsPrivateText() || (m.IsAt() && m.CleanContentStartWith("/"))) && service.CheckIsEnableCommand(m.FromUser) + return (m.IsPrivateText() || (m.IsAt() && !m.IsAtAll() && m.CleanContentStartWith("/"))) && service.CheckIsEnableCommand(m.FromUser) }, plugins.Command) // AI消息插件 dispatcher.RegisterHandler(func(m *model.Message) bool { // 群内@或者私聊文字消息 - return m.IsAt() || m.IsPrivateText() + return m.IsAt() && !m.IsAtAll() || m.IsPrivateText() }, plugins.AI) // 欢迎新成员 diff --git a/model/message.go b/model/message.go index 5e3a96f..5f4f437 100644 --- a/model/message.go +++ b/model/message.go @@ -2,6 +2,7 @@ package model import ( "encoding/xml" + "github.com/duke-git/lancet/v2/slice" "go-wechat/types" "regexp" "strings" @@ -31,6 +32,21 @@ type systemMsgDataXml struct { Type string `xml:"type,attr"` } +// atMsgDataXml +// @description: 微信@消息的xml结构 +type atMsgDataXml struct { + XMLName xml.Name `xml:"msgsource"` + Text string `xml:",chardata"` + AtUserList string `xml:"atuserlist"` + Silence string `xml:"silence"` + MemberCount string `xml:"membercount"` + Signature string `xml:"signature"` + TmpNode struct { + Text string `xml:",chardata"` + PublisherID string `xml:"publisher-id"` + } `xml:"tmp_node"` +} + // sysMsg // @description: 消息主体 type sysMsg struct{} @@ -92,6 +108,22 @@ func (m Message) IsAt() bool { return strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你") } +// IsAtAll +// @description: 是否是At所有人的消息 +// @receiver m +// @return bool +func (m Message) IsAtAll() bool { + // 解析raw里面的xml + var d atMsgDataXml + if err := xml.Unmarshal([]byte(m.Raw), &d); err != nil { + return false + } + // 转换@用户列表为数组 + atUserList := strings.Split(d.AtUserList, ",") + // 判断是否包含@所有人 + return slice.Contain(atUserList, "notify@all") +} + // IsPrivateText // @description: 是否是私聊消息 // @receiver m -- 2.45.2