From b21c0ecf8fd7ca8912c343f567b372fa0fc4da0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Sun, 1 Aug 2021 09:52:42 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=A2=9E=E5=8A=A0=E8=A1=A8=E6=83=85?= =?UTF-8?q?=E5=8C=85=E5=92=8CMedia=E6=B6=88=E6=81=AF=E7=9A=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=8E=A5=E5=8F=A3=E3=80=81=E5=8F=98=E9=87=8F=E5=90=8D?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global.go | 32 ++++++++++++++++---------------- message.go | 29 +++++++++++++++++------------ message_handle.go | 10 ++++++++++ message_test.go | 8 ++++---- messagetype_string.go | 32 ++++++++++++++++---------------- 5 files changed, 63 insertions(+), 48 deletions(-) diff --git a/global.go b/global.go index 498bb74..600b451 100644 --- a/global.go +++ b/global.go @@ -67,22 +67,22 @@ type MessageType int // From: varcaser.ScreamingSnakeCase, To: varcaser.UpperCamelCaseKeepCaps} const ( - MsgtypeText MessageType = 1 // 文本消息 - MsgtypeImage MessageType = 3 // 图片消息 - MsgtypeVoice MessageType = 34 // 语音消息 - MsgtypeVerifymsg MessageType = 37 // 认证消息 - MsgtypePossiblefriendMsg MessageType = 40 // 好友推荐消息 - MsgtypeSharecard MessageType = 42 // 名片消息 - MsgtypeVideo MessageType = 43 // 视频消息 - MsgtypeEmoticon MessageType = 47 // 表情消息 - MsgtypeLocation MessageType = 48 // 地理位置消息 - MsgtypeApp MessageType = 49 // APP消息 - MsgtypeVoipmsg MessageType = 50 // VOIP消息 - MsgtypeVoipnotify MessageType = 52 // VOIP结束消息 - MsgtypeVoipinvite MessageType = 53 // VOIP邀请 - MsgtypeMicrovideo MessageType = 62 // 小视频消息 - MsgtypeSys MessageType = 10000 // 系统消息 - MsgtypeRecalled MessageType = 10002 // 消息撤回 + MsgTypeText MessageType = 1 // 文本消息 + MsgTypeImage MessageType = 3 // 图片消息 + MsgTypeVoice MessageType = 34 // 语音消息 + MsgTypeVerify MessageType = 37 // 认证消息 + MsgTypePossibleFriend MessageType = 40 // 好友推荐消息 + MsgTypeShareCard MessageType = 42 // 名片消息 + MsgTypeVideo MessageType = 43 // 视频消息 + MsgTypeEmoticon MessageType = 47 // 表情消息 + MsgTypeLocation MessageType = 48 // 地理位置消息 + MsgTypeApp MessageType = 49 // APP消息 + MsgTypeVoip MessageType = 50 // VOIP消息 + MsgTypeVoipNotify MessageType = 52 // VOIP结束消息 + MsgTypeVoipInvite MessageType = 53 // VOIP邀请 + MsgTypeMicroVideo MessageType = 62 // 小视频消息 + MsgTypeSys MessageType = 10000 // 系统消息 + MsgTypeRecalled MessageType = 10002 // 消息撤回 ) // 登录状态 diff --git a/message.go b/message.go index c5a5e2f..6fa61f5 100644 --- a/message.go +++ b/message.go @@ -150,44 +150,49 @@ func (m *Message) ReplyFile(file *os.File) (*SentMessage, error) { } func (m *Message) IsText() bool { - return m.MsgType == MsgtypeText && m.Url == "" + return m.MsgType == MsgTypeText && m.Url == "" } func (m *Message) IsMap() bool { - return m.MsgType == MsgtypeText && m.Url != "" + return m.MsgType == MsgTypeText && m.Url != "" } 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 { - return m.MsgType == MsgtypeVoice + return m.MsgType == MsgTypeVoice } func (m *Message) IsFriendAdd() bool { - return m.MsgType == MsgtypeVerifymsg && m.FromUserName == "fmessage" + return m.MsgType == MsgTypeVerify && m.FromUserName == "fmessage" } func (m *Message) IsCard() bool { - return m.MsgType == MsgtypeSharecard + return m.MsgType == MsgTypeShareCard } func (m *Message) IsVideo() bool { - return m.MsgType == MsgtypeVideo || m.MsgType == MsgtypeMicrovideo + return m.MsgType == MsgTypeVideo || m.MsgType == MsgTypeMicroVideo } func (m *Message) IsMedia() bool { - return m.MsgType == MsgtypeApp + return m.MsgType == MsgTypeApp } // IsRecalled 判断是否撤回 func (m *Message) IsRecalled() bool { - return m.MsgType == MsgtypeRecalled + return m.MsgType == MsgTypeRecalled } func (m *Message) IsSystem() bool { - return m.MsgType == MsgtypeSys + return m.MsgType == MsgTypeSys } func (m *Message) IsNotify() bool { @@ -220,7 +225,7 @@ func (m *Message) StatusNotify() bool { // HasFile 判断消息是否为文件类型的消息 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 获取文件消息的文件 @@ -228,7 +233,7 @@ func (m *Message) GetFile() (*http.Response, error) { if !m.HasFile() { 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) } if m.IsVoice() { diff --git a/message_handle.go b/message_handle.go index d3ebb09..fd787fa 100644 --- a/message_handle.go +++ b/message_handle.go @@ -113,6 +113,11 @@ func (m *MessageMatchDispatcher) OnImage(handlers ...MessageContextHandler) { 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的处理函数 func (m *MessageMatchDispatcher) OnVoice(handlers ...MessageContextHandler) { 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...) } +// OnMedia 注册处理消息类型为Media(多媒体消息,包括但不限于APP分享、文件分享)的处理函数 +func (m *MessageMatchDispatcher) OnMedia(handlers ...MessageContextHandler) { + m.RegisterHandler(func(message *Message) bool { return message.IsMedia() }, handlers...) +} + // OnFriendByNickName 注册根据好友昵称是否匹配的消息处理函数 func (m *MessageMatchDispatcher) OnFriendByNickName(nickName string, handlers ...MessageContextHandler) { matchFunc := func(message *Message) bool { diff --git a/message_test.go b/message_test.go index f01b91e..3e9e763 100644 --- a/message_test.go +++ b/message_test.go @@ -7,10 +7,10 @@ import ( func ExampleMessageType_output() { for _, wxt := range []MessageType{ - MsgtypeText, MsgtypeImage, MsgtypeVoice, MsgtypeVerifymsg, - MsgtypePossiblefriendMsg, MsgtypeSharecard, MsgtypeVideo, MsgtypeEmoticon, - MsgtypeLocation, MsgtypeApp, MsgtypeVoipmsg, MsgtypeVoipnotify, - MsgtypeVoipinvite, MsgtypeMicrovideo, MsgtypeSys, MsgtypeRecalled} { + MsgTypeText, MsgTypeImage, MsgTypeVoice, MsgTypeVerify, + MsgTypePossibleFriend, MsgTypeShareCard, MsgTypeVideo, MsgTypeEmoticon, + MsgTypeLocation, MsgTypeApp, MsgTypeVoip, MsgTypeVoipNotify, + MsgTypeVoipInvite, MsgTypeMicroVideo, MsgTypeSys, MsgTypeRecalled} { fmt.Printf("收到一条%s(type %d)\n", wxt, wxt) } fmt.Println("=======") diff --git a/messagetype_string.go b/messagetype_string.go index c33c7cb..0d44c33 100644 --- a/messagetype_string.go +++ b/messagetype_string.go @@ -8,22 +8,22 @@ func _() { // An "invalid array index" compiler error signifies that the constant values have changed. // Re-run the stringer command to generate them again. var x [1]struct{} - _ = x[MsgtypeText-1] - _ = x[MsgtypeImage-3] - _ = x[MsgtypeVoice-34] - _ = x[MsgtypeVerifymsg-37] - _ = x[MsgtypePossiblefriendMsg-40] - _ = x[MsgtypeSharecard-42] - _ = x[MsgtypeVideo-43] - _ = x[MsgtypeEmoticon-47] - _ = x[MsgtypeLocation-48] - _ = x[MsgtypeApp-49] - _ = x[MsgtypeVoipmsg-50] - _ = x[MsgtypeVoipnotify-52] - _ = x[MsgtypeVoipinvite-53] - _ = x[MsgtypeMicrovideo-62] - _ = x[MsgtypeSys-10000] - _ = x[MsgtypeRecalled-10002] + _ = x[MsgTypeText-1] + _ = x[MsgTypeImage-3] + _ = x[MsgTypeVoice-34] + _ = x[MsgTypeVerify-37] + _ = x[MsgTypePossibleFriend-40] + _ = x[MsgTypeShareCard-42] + _ = x[MsgTypeVideo-43] + _ = x[MsgTypeEmoticon-47] + _ = x[MsgTypeLocation-48] + _ = x[MsgTypeApp-49] + _ = x[MsgTypeVoip-50] + _ = x[MsgTypeVoipNotify-52] + _ = x[MsgTypeVoipInvite-53] + _ = x[MsgTypeMicroVideo-62] + _ = x[MsgTypeSys-10000] + _ = x[MsgTypeRecalled-10002] } const _MessageType_name = "文本消息图片消息语音消息认证消息好友推荐消息名片消息视频消息表情消息地理位置消息APP消息VOIP消息VOIP结束消息VOIP邀请小视频消息系统消息消息撤回"