1
0
forked from lxh/go-wxhelper
go-wxhelper/model/message.go

102 lines
2.6 KiB
Go
Raw Permalink Normal View History

2023-09-21 17:33:59 +08:00
package model
2023-12-04 14:17:32 +08:00
import (
"encoding/xml"
"go-wechat/types"
"strings"
)
2023-09-21 17:33:59 +08:00
// Message
// @description: 消息
type Message struct {
2023-12-04 14:17:32 +08:00
MsgId int64 `json:"msgId"`
2023-09-21 17:33:59 +08:00
CreateTime int `json:"createTime"`
Content string `json:"content"`
2023-12-04 14:17:32 +08:00
DisplayFullContent string `json:"displayFullContent"`
2023-09-21 17:33:59 +08:00
FromUser string `json:"fromUser"`
2023-12-04 14:17:32 +08:00
GroupUser string `json:"-"`
2023-09-21 17:33:59 +08:00
MsgSequence int `json:"msgSequence"`
Pid int `json:"pid"`
Signature string `json:"signature"`
ToUser string `json:"toUser"`
Type types.MessageType `json:"type"`
2023-12-11 10:53:21 +08:00
Raw string `json:"raw"`
2023-09-21 17:33:59 +08:00
}
2023-12-04 14:17:32 +08:00
// systemMsgDataXml
// @description: 微信系统消息的xml结构
type systemMsgDataXml struct {
SysMsg sysMsg `xml:"sysmsg"`
Type string `xml:"type,attr"`
}
// sysMsg
// @description: 消息主体
type sysMsg struct{}
2023-12-07 22:38:49 +08:00
func (m Message) IsGroup() bool {
return strings.HasSuffix(m.FromUser, "@chatroom")
}
2023-12-04 14:17:32 +08:00
// IsPat
// @description: 是否是拍一拍消息
// @receiver m
// @return bool
func (m Message) IsPat() bool {
// 解析xml
var d systemMsgDataXml
if err := xml.Unmarshal([]byte(m.Content), &d); err != nil {
return false
}
return m.Type == types.MsgTypeRecalled && d.Type == "pat"
}
// IsRevokeMsg
// @description: 是否是撤回消息
// @receiver m
// @return bool
func (m Message) IsRevokeMsg() bool {
// 解析xml
var d systemMsgDataXml
if err := xml.Unmarshal([]byte(m.Content), &d); err != nil {
return false
}
return m.Type == types.MsgTypeRecalled && d.Type == "revokemsg"
}
// IsNewUserJoin
// @description: 是否是新人入群
// @receiver m
// @return bool
func (m Message) IsNewUserJoin() bool {
sysFlag := m.Type == types.MsgTypeSys && strings.Contains(m.Content, "\"邀请\"") && strings.Contains(m.Content, "\"加入了群聊")
if sysFlag {
return true
}
// 解析另一种情况
var d systemMsgDataXml
if err := xml.Unmarshal([]byte(m.Content), &d); err != nil {
return false
}
return m.Type == types.MsgTypeSys && d.Type == "delchatroommember"
}
// IsAt
// @description: 是否是At机器人的消息
// @receiver m
// @return bool
func (m Message) IsAt() bool {
return strings.HasSuffix(m.DisplayFullContent, "在群聊中@了你")
}
2023-12-11 10:53:21 +08:00
// IsPrivateText
// @description: 是否是私聊消息
// @receiver m
// @return bool
func (m Message) IsPrivateText() bool {
// 发信人不以@chatroom结尾且消息类型为文本
return !strings.HasSuffix(m.FromUser, "chatroom") && m.Type == types.MsgTypeText
}