add message.receiver support

This commit is contained in:
ivy1996-encode 2021-03-13 23:00:29 +08:00
parent 485cd56cdc
commit d4b23ea156
2 changed files with 51 additions and 37 deletions

2
bot.go
View File

@ -164,7 +164,7 @@ func (b *Bot) getMessage() error {
// 遍历所有的新的消息,依次处理 // 遍历所有的新的消息,依次处理
for _, message := range resp.AddMsgList { for _, message := range resp.AddMsgList {
// 根据不同的消息类型来进行处理,方便后续统一调用 // 根据不同的消息类型来进行处理,方便后续统一调用
processMessage(message, b) message.init(b)
// 调用自定义的处理方法 // 调用自定义的处理方法
b.messageHandlerGroups.ProcessMessage(message) b.messageHandlerGroups.ProcessMessage(message)
} }

View File

@ -14,33 +14,35 @@ type Message struct {
AppID string AppID string
Type int Type int
} }
AppMsgType int AppMsgType int
Content string Content string
CreateTime int64 CreateTime int64
EncryFileName string EncryFileName string
FileName string FileName string
FileSize string FileSize string
ForwardFlag int ForwardFlag int
FromUserName string FromUserName string
HasProductId int HasProductId int
ImgHeight int ImgHeight int
ImgStatus int ImgStatus int
ImgWidth int ImgWidth int
MediaId string MediaId string
MsgId string MsgId string
MsgType int MsgType int
NewMsgId int64 NewMsgId int64
OriContent string OriContent string
PlayLength int64 PlayLength int64
RecommendInfo RecommendInfo RecommendInfo RecommendInfo
Status int Status int
StatusNotifyCode int StatusNotifyCode int
StatusNotifyUserName string StatusNotifyUserName string
SubMsgType int SubMsgType int
Ticket string Ticket string
ToUserName string ToUserName string
Url string Url string
VoiceLength int VoiceLength int
IsAt bool
Bot *Bot Bot *Bot
senderInGroupUserName string senderInGroupUserName string
mu sync.RWMutex mu sync.RWMutex
@ -80,6 +82,18 @@ func (m *Message) SenderInGroup() (*User, error) {
return group.MemberList.SearchByUserName(m.senderInGroupUserName) 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 { func (m *Message) IsSendBySelf() bool {
return m.FromUserName == m.Bot.self.User.UserName return m.FromUserName == m.Bot.self.User.UserName
@ -172,6 +186,15 @@ func (m *Message) Get(key string) (value interface{}, exist bool) {
return return
} }
func (m *Message) init(bot *Bot) {
m.Bot = bot
if m.IsSendByGroup() {
data := strings.Split(m.Content, ":<br/>")
m.Content = strings.Join(data[1:], "")
m.senderInGroupUserName = data[0]
}
}
//func (m *Message) Agree() error { //func (m *Message) Agree() error {
// if !m.IsFriendAdd() { // if !m.IsFriendAdd() {
// return fmt.Errorf("the excepted message type is 37, but got %d", m.MsgType) // return fmt.Errorf("the excepted message type is 37, but got %d", m.MsgType)
@ -230,12 +253,3 @@ type RecommendInfo struct {
UserName string UserName string
VerifyFlag int VerifyFlag int
} }
func processMessage(message *Message, bot *Bot) {
message.Bot = bot
if message.IsSendByGroup() {
data := strings.Split(message.Content, ":<br/>")
message.Content = strings.Join(data[1:], "")
message.senderInGroupUserName = data[0]
}
}