🎨 增加表情包和Media消息的处理接口、变量名优化

This commit is contained in:
李寻欢 2021-08-01 09:52:42 +08:00
parent 8f09d7a509
commit b21c0ecf8f
5 changed files with 63 additions and 48 deletions

View File

@ -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 // 消息撤回
)
// 登录状态

View File

@ -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() {

View File

@ -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 {

View File

@ -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("=======")

View File

@ -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邀请小视频消息系统消息消息撤回"