From d4b23ea156113ce95fcfdbd36310df4b2cfe688e Mon Sep 17 00:00:00 2001
From: ivy1996-encode <15055461510@163.com>
Date: Sat, 13 Mar 2021 23:00:29 +0800
Subject: [PATCH] add message.receiver support
---
bot.go | 2 +-
message.go | 86 +++++++++++++++++++++++++++++++-----------------------
2 files changed, 51 insertions(+), 37 deletions(-)
diff --git a/bot.go b/bot.go
index 3739f0d..90be98e 100644
--- a/bot.go
+++ b/bot.go
@@ -164,7 +164,7 @@ func (b *Bot) getMessage() error {
// 遍历所有的新的消息,依次处理
for _, message := range resp.AddMsgList {
// 根据不同的消息类型来进行处理,方便后续统一调用
- processMessage(message, b)
+ message.init(b)
// 调用自定义的处理方法
b.messageHandlerGroups.ProcessMessage(message)
}
diff --git a/message.go b/message.go
index a9eb741..9d8a6dc 100644
--- a/message.go
+++ b/message.go
@@ -14,33 +14,35 @@ type Message struct {
AppID string
Type int
}
- AppMsgType int
- Content string
- CreateTime int64
- EncryFileName string
- FileName string
- FileSize string
- ForwardFlag int
- FromUserName string
- HasProductId int
- ImgHeight int
- ImgStatus int
- ImgWidth int
- MediaId string
- MsgId string
- MsgType int
- NewMsgId int64
- OriContent string
- PlayLength int64
- RecommendInfo RecommendInfo
- Status int
- StatusNotifyCode int
- StatusNotifyUserName string
- SubMsgType int
- Ticket string
- ToUserName string
- Url string
- VoiceLength int
+ AppMsgType int
+ Content string
+ CreateTime int64
+ EncryFileName string
+ FileName string
+ FileSize string
+ ForwardFlag int
+ FromUserName string
+ HasProductId int
+ ImgHeight int
+ ImgStatus int
+ ImgWidth int
+ MediaId string
+ MsgId string
+ MsgType int
+ NewMsgId int64
+ OriContent string
+ PlayLength int64
+ RecommendInfo RecommendInfo
+ Status int
+ StatusNotifyCode int
+ StatusNotifyUserName string
+ SubMsgType int
+ Ticket string
+ ToUserName string
+ Url string
+ VoiceLength int
+
+ IsAt bool
Bot *Bot
senderInGroupUserName string
mu sync.RWMutex
@@ -80,6 +82,18 @@ func (m *Message) SenderInGroup() (*User, error) {
return group.MemberList.SearchByUserName(m.senderInGroupUserName)
}
+func (m *Message) Receiver() (*User, error) {
+ if m.IsSendByGroup() {
+ if sender, err := m.Sender(); err != nil {
+ return nil, err
+ } else {
+ return sender.MemberList.SearchByUserName(m.ToUserName)
+ }
+ } else {
+ return m.Bot.self.MemberList.SearchByUserName(m.ToUserName)
+ }
+}
+
// 判断消息是否由自己发送
func (m *Message) IsSendBySelf() bool {
return m.FromUserName == m.Bot.self.User.UserName
@@ -172,6 +186,15 @@ func (m *Message) Get(key string) (value interface{}, exist bool) {
return
}
+func (m *Message) init(bot *Bot) {
+ m.Bot = bot
+ if m.IsSendByGroup() {
+ data := strings.Split(m.Content, ":
")
+ m.Content = strings.Join(data[1:], "")
+ m.senderInGroupUserName = data[0]
+ }
+}
+
//func (m *Message) Agree() error {
// if !m.IsFriendAdd() {
// return fmt.Errorf("the excepted message type is 37, but got %d", m.MsgType)
@@ -230,12 +253,3 @@ type RecommendInfo struct {
UserName string
VerifyFlag int
}
-
-func processMessage(message *Message, bot *Bot) {
- message.Bot = bot
- if message.IsSendByGroup() {
- data := strings.Split(message.Content, ":
")
- message.Content = strings.Join(data[1:], "")
- message.senderInGroupUserName = data[0]
- }
-}