diff --git a/global.go b/global.go index 800f310..498bb74 100644 --- a/global.go +++ b/global.go @@ -40,26 +40,49 @@ const ( AppMessage = 6 ) +// MessageType以Go惯用形式定义了PC微信所有的官方消息类型。 +// 详见 message_test.go +type MessageType int + +//go:generate stringer -type=MessageType -linecomment=true + // https://res.wx.qq.com/a/wx_fed/webwx/res/static/js/index_c7d281c.js +// MSGTYPE_TEXT +// MSGTYPE_IMAGE +// MSGTYPE_VOICE +// MSGTYPE_VERIFYMSG +// MSGTYPE_POSSIBLEFRIEND_MSG +// MSGTYPE_SHARECARD +// MSGTYPE_VIDEO +// MSGTYPE_EMOTICON +// MSGTYPE_LOCATION +// MSGTYPE_APP +// MSGTYPE_VOIPMSG +// MSGTYPE_VOIPNOTIFY +// MSGTYPE_VOIPINVITE +// MSGTYPE_MICROVIDEO +// MSGTYPE_SYS +// MSGTYPE_RECALLED // varcaser.Caser{ // From: varcaser.ScreamingSnakeCase, To: varcaser.UpperCamelCaseKeepCaps} + const ( - MsgtypeText = 1 // 文本消息 - MsgtypeImage = 3 // 图片消息 - MsgtypeVoice = 34 // 语音消息 - MsgtypeVerifymsg = 37 // 认证消息 - MsgtypePossiblefriendMsg = 40 // 好友推荐消息 - MsgtypeSharecard = 42 // 名片消息 - MsgtypeVideo = 43 // 视频消息 - MsgtypeEmoticon = 47 // 表情消息 - MsgtypeLocation = 48 // 地理位置消息 - MsgtypeApp = 49 // APP消息 - MsgtypeVoipmsg = 50 // voip msg //VOIP消息 - MsgtypeVoipnotify = 52 // voip 结束消息 - MsgtypeVoipinvite = 53 // voip 邀请 - MsgtypeMicrovideo = 62 // 小视频消息 - MsgtypeSys = 10000 // 系统消息 - MsgtypeRecalled = 10002 // 消息撤回 + 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 // 消息撤回 ) // 登录状态 diff --git a/message_test.go b/message_test.go new file mode 100644 index 0000000..f01b91e --- /dev/null +++ b/message_test.go @@ -0,0 +1,45 @@ +package openwechat + +import ( + "fmt" + "regexp" +) + +func ExampleMessageType_output() { + for _, wxt := range []MessageType{ + MsgtypeText, MsgtypeImage, MsgtypeVoice, MsgtypeVerifymsg, + MsgtypePossiblefriendMsg, MsgtypeSharecard, MsgtypeVideo, MsgtypeEmoticon, + MsgtypeLocation, MsgtypeApp, MsgtypeVoipmsg, MsgtypeVoipnotify, + MsgtypeVoipinvite, MsgtypeMicrovideo, MsgtypeSys, MsgtypeRecalled} { + fmt.Printf("收到一条%s(type %d)\n", wxt, wxt) + } + fmt.Println("=======") + for _, wxt := range []MessageType{10000, 6, 51} { + wxtstr := wxt.String() + if regexp.MustCompile(`^M`).MatchString(wxtstr) { + wxtstr = "未知消息" + } + fmt.Printf("收到一条%s(type %d): %s\n", wxtstr, wxt, wxt) + } + // Output: + // 收到一条文本消息(type 1) + // 收到一条图片消息(type 3) + // 收到一条语音消息(type 34) + // 收到一条认证消息(type 37) + // 收到一条好友推荐消息(type 40) + // 收到一条名片消息(type 42) + // 收到一条视频消息(type 43) + // 收到一条表情消息(type 47) + // 收到一条地理位置消息(type 48) + // 收到一条APP消息(type 49) + // 收到一条VOIP消息(type 50) + // 收到一条VOIP结束消息(type 52) + // 收到一条VOIP邀请(type 53) + // 收到一条小视频消息(type 62) + // 收到一条系统消息(type 10000) + // 收到一条消息撤回(type 10002) + // ======= + // 收到一条系统消息(type 10000): 系统消息 + // 收到一条未知消息(type 6): MessageType(6) + // 收到一条未知消息(type 51): MessageType(51) +} diff --git a/messagetype_string.go b/messagetype_string.go new file mode 100644 index 0000000..c33c7cb --- /dev/null +++ b/messagetype_string.go @@ -0,0 +1,55 @@ +// Code generated by "stringer -type=MessageType -linecomment=true"; DO NOT EDIT. + +package openwechat + +import "strconv" + +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] +} + +const _MessageType_name = "文本消息图片消息语音消息认证消息好友推荐消息名片消息视频消息表情消息地理位置消息APP消息VOIP消息VOIP结束消息VOIP邀请小视频消息系统消息消息撤回" + +var _MessageType_map = map[MessageType]string{ + 1: _MessageType_name[0:12], + 3: _MessageType_name[12:24], + 34: _MessageType_name[24:36], + 37: _MessageType_name[36:48], + 40: _MessageType_name[48:66], + 42: _MessageType_name[66:78], + 43: _MessageType_name[78:90], + 47: _MessageType_name[90:102], + 48: _MessageType_name[102:120], + 49: _MessageType_name[120:129], + 50: _MessageType_name[129:139], + 52: _MessageType_name[139:155], + 53: _MessageType_name[155:165], + 62: _MessageType_name[165:180], + 10000: _MessageType_name[180:192], + 10002: _MessageType_name[192:204], +} + +func (i MessageType) String() string { + if str, ok := _MessageType_map[i]; ok { + return str + } + return "MessageType(" + strconv.FormatInt(int64(i), 10) + ")" +}