From 2e5a46947aa930afc98f4d16541e5ff59be30489 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 10:46:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:sparkles:=20=E5=BC=80=E6=94=BE=E5=87=A0?= =?UTF-8?q?=E4=B8=AA=E5=8F=98=E9=87=8F=E5=92=8C=E5=87=BD=E6=95=B0=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=96=E9=83=A8=E8=AE=BF=E9=97=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 46 +++++++++++++++++++++++----------------------- global.go | 8 ++++---- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/bot.go b/bot.go index d3eb0bc..96463ad 100644 --- a/bot.go +++ b/bot.go @@ -15,7 +15,7 @@ type Bot struct { UUIDCallback func(uuid string) // 获取UUID的回调函数 MessageHandler MessageHandler // 获取消息成功的handle GetMessageErrorHandler func(err error) // 获取消息发生错误的handle - isHot bool // 是否为热登录模式 + IsHot bool // 是否为热登录模式 once sync.Once err error context context.Context @@ -23,7 +23,7 @@ type Bot struct { Caller *Caller self *Self storage *Storage - hotReloadStorage HotReloadStorage + HotReloadStorage HotReloadStorage } // Alive 判断当前用户是否正常在线 @@ -58,8 +58,8 @@ func (b *Bot) GetCurrentUser() (*Self, error) { // err := bot.HotLogin(storage, true) // fmt.Println(err) func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error { - b.isHot = true - b.hotReloadStorage = storage + b.IsHot = true + b.HotReloadStorage = storage var err error @@ -69,13 +69,13 @@ func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error { return b.Login() } - if err = b.hotLoginInit(); err != nil { + if err = b.HotLoginInit(); err != nil { return err } // 如果webInit出错,则说明可能身份信息已经失效 // 如果retry为True的话,则进行正常登陆 - if err = b.webInit(); err != nil { + if err = b.WebInit(); err != nil { if len(retry) > 0 && retry[0] { return b.Login() } @@ -83,9 +83,9 @@ func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error { return err } -// 热登陆初始化 -func (b *Bot) hotLoginInit() error { - item := b.hotReloadStorage.GetHotReloadStorageItem() +// HotLoginInit 热登陆初始化 +func (b *Bot) HotLoginInit() error { + item := b.HotReloadStorage.GetHotReloadStorageItem() cookies := item.Cookies for u, ck := range cookies { path, err := url.Parse(u) @@ -118,20 +118,20 @@ func (b *Bot) Login() error { return err } switch resp.Code { - case statusSuccess: + case StatusSuccess: // 判断是否有登录回调,如果有执行它 if b.LoginCallBack != nil { b.LoginCallBack(resp.Raw) } - return b.handleLogin(resp.Raw) - case statusScanned: + return b.HandleLogin(resp.Raw) + case StatusScanned: // 执行扫码回调 if b.ScanCallBack != nil { b.ScanCallBack(resp.Raw) } - case statusTimeout: + case StatusTimeout: return errors.New("login time out") - case statusWait: + case StatusWait: continue } } @@ -153,8 +153,8 @@ func (b *Bot) Logout() error { return errors.New("user not login") } -// 登录逻辑 -func (b *Bot) handleLogin(data []byte) error { +// HandleLogin 登录逻辑 +func (b *Bot) HandleLogin(data []byte) error { // 获取登录的一些基本的信息 info, err := b.Caller.GetLoginInfo(data) if err != nil { @@ -175,17 +175,17 @@ func (b *Bot) handleLogin(data []byte) error { b.storage.Request = request // 如果是热登陆,则将当前的重要信息写入hotReloadStorage - if b.isHot { + if b.IsHot { if err = b.DumpHotReloadStorage(); err != nil { return err } } - return b.webInit() + return b.WebInit() } -// 根据有效凭证获取和初始化用户信息 -func (b *Bot) webInit() error { +// WebInit 根据有效凭证获取和初始化用户信息 +func (b *Bot) WebInit() error { req := b.storage.Request info := b.storage.LoginInfo // 获取初始化的用户信息和一些必要的参数 @@ -297,8 +297,8 @@ func (b *Bot) MessageOnError(h func(err error)) { // DumpHotReloadStorage 写入HotReloadStorage func (b *Bot) DumpHotReloadStorage() error { - if b.hotReloadStorage == nil { - return errors.New("hotReloadStorage can be nil") + if b.HotReloadStorage == nil { + return errors.New("HotReloadStorage can be nil") } cookies := b.Caller.Client.GetCookieMap() item := HotReloadStorageItem{ @@ -307,7 +307,7 @@ func (b *Bot) DumpHotReloadStorage() error { LoginInfo: b.storage.LoginInfo, WechatDomain: b.Caller.Client.domain, } - return b.hotReloadStorage.Dump(item) + return b.HotReloadStorage.Dump(item) } // OnLogin is a setter for LoginCallBack diff --git a/global.go b/global.go index 600b451..7e9745e 100644 --- a/global.go +++ b/global.go @@ -87,10 +87,10 @@ const ( // 登录状态 const ( - statusSuccess = "200" - statusScanned = "201" - statusTimeout = "400" - statusWait = "408" + StatusSuccess = "200" + StatusScanned = "201" + StatusTimeout = "400" + StatusWait = "408" ) // errors From a132af0075ac5b36ff857faaae1a3267603f84fd Mon Sep 17 00:00:00 2001 From: Tong Sun Date: Sat, 31 Jul 2021 22:55:53 -0400 Subject: [PATCH 2/2] - [+] add AppMsgTypes --- appmessagetype_string.go | 62 ++++++++++++++++++++++++++++++++++++++++ global.go | 27 +++++++++++++++-- 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 appmessagetype_string.go diff --git a/appmessagetype_string.go b/appmessagetype_string.go new file mode 100644 index 0000000..9883419 --- /dev/null +++ b/appmessagetype_string.go @@ -0,0 +1,62 @@ +// Code generated by "stringer -type=AppMessageType -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[AppMsgTypeText-1] + _ = x[AppMsgTypeImg-2] + _ = x[AppMsgTypeAudio-3] + _ = x[AppMsgTypeVideo-4] + _ = x[AppMsgTypeUrl-5] + _ = x[AppMsgTypeAttach-6] + _ = x[AppMsgTypeOpen-7] + _ = x[AppMsgTypeEmoji-8] + _ = x[AppMsgTypeVoiceRemind-9] + _ = x[AppMsgTypeScanGood-10] + _ = x[AppMsgTypeGood-13] + _ = x[AppMsgTypeEmotion-15] + _ = x[AppMsgTypeCardTicket-16] + _ = x[AppMsgTypeRealtimeShareLocation-17] + _ = x[AppMsgTypeTransfers-2000] + _ = x[AppMsgTypeRedEnvelopes-2001] + _ = x[AppMsgTypeReaderType-100001] +} + +const ( + _AppMessageType_name_0 = "文本消息图片消息语音消息视频消息文章消息附件消息Open表情消息VoiceRemindScanGood" + _AppMessageType_name_1 = "Good" + _AppMessageType_name_2 = "Emotion名片消息地理位置消息" + _AppMessageType_name_3 = "转账消息红包消息" + _AppMessageType_name_4 = "自定义的消息" +) + +var ( + _AppMessageType_index_0 = [...]uint8{0, 12, 24, 36, 48, 60, 72, 76, 88, 99, 107} + _AppMessageType_index_2 = [...]uint8{0, 7, 19, 37} + _AppMessageType_index_3 = [...]uint8{0, 12, 24} +) + +func (i AppMessageType) String() string { + switch { + case 1 <= i && i <= 10: + i -= 1 + return _AppMessageType_name_0[_AppMessageType_index_0[i]:_AppMessageType_index_0[i+1]] + case i == 13: + return _AppMessageType_name_1 + case 15 <= i && i <= 17: + i -= 15 + return _AppMessageType_name_2[_AppMessageType_index_2[i]:_AppMessageType_index_2[i+1]] + case 2000 <= i && i <= 2001: + i -= 2000 + return _AppMessageType_name_3[_AppMessageType_index_3[i]:_AppMessageType_index_3[i+1]] + case i == 100001: + return _AppMessageType_name_4 + default: + return "AppMessageType(" + strconv.FormatInt(int64(i), 10) + ")" + } +} diff --git a/global.go b/global.go index 600b451..12eb908 100644 --- a/global.go +++ b/global.go @@ -44,7 +44,11 @@ const ( // 详见 message_test.go type MessageType int +// AppMessageType以Go惯用形式定义了PC微信所有的官方App消息类型。 +type AppMessageType int + //go:generate stringer -type=MessageType -linecomment=true +//go:generate stringer -type=AppMessageType -linecomment=true // https://res.wx.qq.com/a/wx_fed/webwx/res/static/js/index_c7d281c.js // MSGTYPE_TEXT @@ -63,8 +67,6 @@ type MessageType int // MSGTYPE_MICROVIDEO // MSGTYPE_SYS // MSGTYPE_RECALLED -// varcaser.Caser{ -// From: varcaser.ScreamingSnakeCase, To: varcaser.UpperCamelCaseKeepCaps} const ( MsgTypeText MessageType = 1 // 文本消息 @@ -85,6 +87,27 @@ const ( MsgTypeRecalled MessageType = 10002 // 消息撤回 ) +const ( + AppMsgTypeText AppMessageType = 1 // 文本消息 + AppMsgTypeImg AppMessageType = 2 // 图片消息 + AppMsgTypeAudio AppMessageType = 3 // 语音消息 + AppMsgTypeVideo AppMessageType = 4 // 视频消息 + AppMsgTypeUrl AppMessageType = 5 // 文章消息 + AppMsgTypeAttach AppMessageType = 6 // 附件消息 + AppMsgTypeOpen AppMessageType = 7 // Open + AppMsgTypeEmoji AppMessageType = 8 // 表情消息 + AppMsgTypeVoiceRemind AppMessageType = 9 // VoiceRemind + AppMsgTypeScanGood AppMessageType = 10 // ScanGood + AppMsgTypeGood AppMessageType = 13 // Good + AppMsgTypeEmotion AppMessageType = 15 // Emotion + AppMsgTypeCardTicket AppMessageType = 16 // 名片消息 + AppMsgTypeRealtimeShareLocation AppMessageType = 17 // 地理位置消息 + AppMsgTypeTransfers AppMessageType = 2000 // 转账消息 + AppMsgTypeRedEnvelopes AppMessageType = 2001 // 红包消息 + AppMsgTypeReaderType AppMessageType = 100001 //自定义的消息 + +) + // 登录状态 const ( statusSuccess = "200"