Merge pull request #51 from li-xunhuan/master

🎨 增加表情包和Media消息的处理接口、变量名优化
This commit is contained in:
Ivy 2021-08-01 09:57:48 +08:00 committed by GitHub
commit 1d0d2a1e2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 48 deletions

View File

@ -67,22 +67,22 @@ type MessageType int
// From: varcaser.ScreamingSnakeCase, To: varcaser.UpperCamelCaseKeepCaps} // From: varcaser.ScreamingSnakeCase, To: varcaser.UpperCamelCaseKeepCaps}
const ( const (
MsgtypeText MessageType = 1 // 文本消息 MsgTypeText MessageType = 1 // 文本消息
MsgtypeImage MessageType = 3 // 图片消息 MsgTypeImage MessageType = 3 // 图片消息
MsgtypeVoice MessageType = 34 // 语音消息 MsgTypeVoice MessageType = 34 // 语音消息
MsgtypeVerifymsg MessageType = 37 // 认证消息 MsgTypeVerify MessageType = 37 // 认证消息
MsgtypePossiblefriendMsg MessageType = 40 // 好友推荐消息 MsgTypePossibleFriend MessageType = 40 // 好友推荐消息
MsgtypeSharecard MessageType = 42 // 名片消息 MsgTypeShareCard MessageType = 42 // 名片消息
MsgtypeVideo MessageType = 43 // 视频消息 MsgTypeVideo MessageType = 43 // 视频消息
MsgtypeEmoticon MessageType = 47 // 表情消息 MsgTypeEmoticon MessageType = 47 // 表情消息
MsgtypeLocation MessageType = 48 // 地理位置消息 MsgTypeLocation MessageType = 48 // 地理位置消息
MsgtypeApp MessageType = 49 // APP消息 MsgTypeApp MessageType = 49 // APP消息
MsgtypeVoipmsg MessageType = 50 // VOIP消息 MsgTypeVoip MessageType = 50 // VOIP消息
MsgtypeVoipnotify MessageType = 52 // VOIP结束消息 MsgTypeVoipNotify MessageType = 52 // VOIP结束消息
MsgtypeVoipinvite MessageType = 53 // VOIP邀请 MsgTypeVoipInvite MessageType = 53 // VOIP邀请
MsgtypeMicrovideo MessageType = 62 // 小视频消息 MsgTypeMicroVideo MessageType = 62 // 小视频消息
MsgtypeSys MessageType = 10000 // 系统消息 MsgTypeSys MessageType = 10000 // 系统消息
MsgtypeRecalled MessageType = 10002 // 消息撤回 MsgTypeRecalled MessageType = 10002 // 消息撤回
) )
// 登录状态 // 登录状态

View File

@ -150,44 +150,49 @@ func (m *Message) ReplyFile(file *os.File) (*SentMessage, error) {
} }
func (m *Message) IsText() bool { func (m *Message) IsText() bool {
return m.MsgType == MsgtypeText && m.Url == "" return m.MsgType == MsgTypeText && m.Url == ""
} }
func (m *Message) IsMap() bool { func (m *Message) IsMap() bool {
return m.MsgType == MsgtypeText && m.Url != "" return m.MsgType == MsgTypeText && m.Url != ""
} }
func (m *Message) IsPicture() bool { func (m *Message) IsPicture() bool {
return m.MsgType == MsgtypeImage || m.MsgType == MsgtypeEmoticon return m.MsgType == MsgTypeImage
}
// IsEmoticon 是否为表情包消息
func (m *Message) IsEmoticon() bool {
return m.MsgType == MsgTypeEmoticon
} }
func (m *Message) IsVoice() bool { func (m *Message) IsVoice() bool {
return m.MsgType == MsgtypeVoice return m.MsgType == MsgTypeVoice
} }
func (m *Message) IsFriendAdd() bool { func (m *Message) IsFriendAdd() bool {
return m.MsgType == MsgtypeVerifymsg && m.FromUserName == "fmessage" return m.MsgType == MsgTypeVerify && m.FromUserName == "fmessage"
} }
func (m *Message) IsCard() bool { func (m *Message) IsCard() bool {
return m.MsgType == MsgtypeSharecard return m.MsgType == MsgTypeShareCard
} }
func (m *Message) IsVideo() bool { func (m *Message) IsVideo() bool {
return m.MsgType == MsgtypeVideo || m.MsgType == MsgtypeMicrovideo return m.MsgType == MsgTypeVideo || m.MsgType == MsgTypeMicroVideo
} }
func (m *Message) IsMedia() bool { func (m *Message) IsMedia() bool {
return m.MsgType == MsgtypeApp return m.MsgType == MsgTypeApp
} }
// IsRecalled 判断是否撤回 // IsRecalled 判断是否撤回
func (m *Message) IsRecalled() bool { func (m *Message) IsRecalled() bool {
return m.MsgType == MsgtypeRecalled return m.MsgType == MsgTypeRecalled
} }
func (m *Message) IsSystem() bool { func (m *Message) IsSystem() bool {
return m.MsgType == MsgtypeSys return m.MsgType == MsgTypeSys
} }
func (m *Message) IsNotify() bool { func (m *Message) IsNotify() bool {
@ -220,7 +225,7 @@ func (m *Message) StatusNotify() bool {
// HasFile 判断消息是否为文件类型的消息 // HasFile 判断消息是否为文件类型的消息
func (m *Message) HasFile() bool { func (m *Message) HasFile() bool {
return m.IsPicture() || m.IsVoice() || m.IsVideo() || m.IsMedia() return m.IsPicture() || m.IsVoice() || m.IsVideo() || m.IsMedia() || m.IsEmoticon()
} }
// GetFile 获取文件消息的文件 // GetFile 获取文件消息的文件
@ -228,7 +233,7 @@ func (m *Message) GetFile() (*http.Response, error) {
if !m.HasFile() { if !m.HasFile() {
return nil, errors.New("invalid message type") return nil, errors.New("invalid message type")
} }
if m.IsPicture() { if m.IsPicture() || m.IsEmoticon() {
return m.Bot.Caller.Client.WebWxGetMsgImg(m, m.Bot.storage.LoginInfo) return m.Bot.Caller.Client.WebWxGetMsgImg(m, m.Bot.storage.LoginInfo)
} }
if m.IsVoice() { if m.IsVoice() {

View File

@ -113,6 +113,11 @@ func (m *MessageMatchDispatcher) OnImage(handlers ...MessageContextHandler) {
m.RegisterHandler(func(message *Message) bool { return message.IsPicture() }, handlers...) m.RegisterHandler(func(message *Message) bool { return message.IsPicture() }, handlers...)
} }
// OnEmoticon 注册处理消息类型为Emoticon的处理函数(表情包)
func (m *MessageMatchDispatcher) OnEmoticon(handlers ...MessageContextHandler) {
m.RegisterHandler(func(message *Message) bool { return message.IsEmoticon() }, handlers...)
}
// OnVoice 注册处理消息类型为Voice的处理函数 // OnVoice 注册处理消息类型为Voice的处理函数
func (m *MessageMatchDispatcher) OnVoice(handlers ...MessageContextHandler) { func (m *MessageMatchDispatcher) OnVoice(handlers ...MessageContextHandler) {
m.RegisterHandler(func(message *Message) bool { return message.IsVoice() }, handlers...) m.RegisterHandler(func(message *Message) bool { return message.IsVoice() }, handlers...)
@ -128,6 +133,11 @@ func (m *MessageMatchDispatcher) OnCard(handlers ...MessageContextHandler) {
m.RegisterHandler(func(message *Message) bool { return message.IsCard() }, handlers...) m.RegisterHandler(func(message *Message) bool { return message.IsCard() }, handlers...)
} }
// OnMedia 注册处理消息类型为Media(多媒体消息包括但不限于APP分享、文件分享)的处理函数
func (m *MessageMatchDispatcher) OnMedia(handlers ...MessageContextHandler) {
m.RegisterHandler(func(message *Message) bool { return message.IsMedia() }, handlers...)
}
// OnFriendByNickName 注册根据好友昵称是否匹配的消息处理函数 // OnFriendByNickName 注册根据好友昵称是否匹配的消息处理函数
func (m *MessageMatchDispatcher) OnFriendByNickName(nickName string, handlers ...MessageContextHandler) { func (m *MessageMatchDispatcher) OnFriendByNickName(nickName string, handlers ...MessageContextHandler) {
matchFunc := func(message *Message) bool { matchFunc := func(message *Message) bool {

View File

@ -7,10 +7,10 @@ import (
func ExampleMessageType_output() { func ExampleMessageType_output() {
for _, wxt := range []MessageType{ for _, wxt := range []MessageType{
MsgtypeText, MsgtypeImage, MsgtypeVoice, MsgtypeVerifymsg, MsgTypeText, MsgTypeImage, MsgTypeVoice, MsgTypeVerify,
MsgtypePossiblefriendMsg, MsgtypeSharecard, MsgtypeVideo, MsgtypeEmoticon, MsgTypePossibleFriend, MsgTypeShareCard, MsgTypeVideo, MsgTypeEmoticon,
MsgtypeLocation, MsgtypeApp, MsgtypeVoipmsg, MsgtypeVoipnotify, MsgTypeLocation, MsgTypeApp, MsgTypeVoip, MsgTypeVoipNotify,
MsgtypeVoipinvite, MsgtypeMicrovideo, MsgtypeSys, MsgtypeRecalled} { MsgTypeVoipInvite, MsgTypeMicroVideo, MsgTypeSys, MsgTypeRecalled} {
fmt.Printf("收到一条%s(type %d)\n", wxt, wxt) fmt.Printf("收到一条%s(type %d)\n", wxt, wxt)
} }
fmt.Println("=======") fmt.Println("=======")

View File

@ -8,22 +8,22 @@ func _() {
// An "invalid array index" compiler error signifies that the constant values have changed. // An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again. // Re-run the stringer command to generate them again.
var x [1]struct{} var x [1]struct{}
_ = x[MsgtypeText-1] _ = x[MsgTypeText-1]
_ = x[MsgtypeImage-3] _ = x[MsgTypeImage-3]
_ = x[MsgtypeVoice-34] _ = x[MsgTypeVoice-34]
_ = x[MsgtypeVerifymsg-37] _ = x[MsgTypeVerify-37]
_ = x[MsgtypePossiblefriendMsg-40] _ = x[MsgTypePossibleFriend-40]
_ = x[MsgtypeSharecard-42] _ = x[MsgTypeShareCard-42]
_ = x[MsgtypeVideo-43] _ = x[MsgTypeVideo-43]
_ = x[MsgtypeEmoticon-47] _ = x[MsgTypeEmoticon-47]
_ = x[MsgtypeLocation-48] _ = x[MsgTypeLocation-48]
_ = x[MsgtypeApp-49] _ = x[MsgTypeApp-49]
_ = x[MsgtypeVoipmsg-50] _ = x[MsgTypeVoip-50]
_ = x[MsgtypeVoipnotify-52] _ = x[MsgTypeVoipNotify-52]
_ = x[MsgtypeVoipinvite-53] _ = x[MsgTypeVoipInvite-53]
_ = x[MsgtypeMicrovideo-62] _ = x[MsgTypeMicroVideo-62]
_ = x[MsgtypeSys-10000] _ = x[MsgTypeSys-10000]
_ = x[MsgtypeRecalled-10002] _ = x[MsgTypeRecalled-10002]
} }
const _MessageType_name = "文本消息图片消息语音消息认证消息好友推荐消息名片消息视频消息表情消息地理位置消息APP消息VOIP消息VOIP结束消息VOIP邀请小视频消息系统消息消息撤回" const _MessageType_name = "文本消息图片消息语音消息认证消息好友推荐消息名片消息视频消息表情消息地理位置消息APP消息VOIP消息VOIP结束消息VOIP邀请小视频消息系统消息消息撤回"