修改同步消息错误默认行为 (#190)
This commit is contained in:
parent
2681b51702
commit
200c5be4ff
81
bot.go
81
bot.go
@ -107,7 +107,7 @@ func (b *Bot) Logout() error {
|
|||||||
if err := b.Caller.Logout(info); err != nil {
|
if err := b.Caller.Logout(info); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.stopSyncCheck(errors.New("logout"))
|
b.Exit()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return errors.New("user not login")
|
return errors.New("user not login")
|
||||||
@ -173,7 +173,7 @@ func (b *Bot) WebInit() error {
|
|||||||
// FIX: 当bot在线的情况下执行热登录,会开启多次事件监听
|
// FIX: 当bot在线的情况下执行热登录,会开启多次事件监听
|
||||||
go b.once.Do(func() {
|
go b.once.Do(func() {
|
||||||
if b.MessageErrorHandler == nil {
|
if b.MessageErrorHandler == nil {
|
||||||
b.MessageErrorHandler = b.stopSyncCheck
|
b.MessageErrorHandler = defaultSyncCheckErrHandler(b)
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
err := b.syncCheck()
|
err := b.syncCheck()
|
||||||
@ -182,6 +182,7 @@ func (b *Bot) WebInit() error {
|
|||||||
}
|
}
|
||||||
// 判断是否继续, 如果不继续则退出
|
// 判断是否继续, 如果不继续则退出
|
||||||
if goon := b.MessageErrorHandler(err); !goon {
|
if goon := b.MessageErrorHandler(err); !goon {
|
||||||
|
b.err = err
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -231,13 +232,6 @@ func (b *Bot) syncCheck() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 当获取消息发生错误时, 默认的错误处理行为
|
|
||||||
func (b *Bot) stopSyncCheck(err error) bool {
|
|
||||||
b.err = err
|
|
||||||
b.Exit()
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取新的消息
|
// 获取新的消息
|
||||||
func (b *Bot) syncMessage() ([]*Message, error) {
|
func (b *Bot) syncMessage() ([]*Message, error) {
|
||||||
resp, err := b.Caller.WebWxSync(b.Storage.Request, b.Storage.Response, b.Storage.LoginInfo)
|
resp, err := b.Caller.WebWxSync(b.Storage.Request, b.Storage.Response, b.Storage.LoginInfo)
|
||||||
@ -294,6 +288,33 @@ func (b *Bot) DumpTo(writer io.Writer) error {
|
|||||||
return json.NewEncoder(writer).Encode(item)
|
return json.NewEncoder(writer).Encode(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsHot returns true if is hot login otherwise false
|
||||||
|
func (b *Bot) IsHot() bool {
|
||||||
|
return b.hotReloadStorage != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UUID returns current uuid of bot
|
||||||
|
func (b *Bot) UUID() string {
|
||||||
|
return b.uuid
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) reload() error {
|
||||||
|
if b.hotReloadStorage == nil {
|
||||||
|
return errors.New("hotReloadStorage is nil")
|
||||||
|
}
|
||||||
|
var item HotReloadStorageItem
|
||||||
|
err := json.NewDecoder(b.hotReloadStorage).Decode(&item)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.Caller.Client.Jar = item.Jar.AsCookieJar()
|
||||||
|
b.Storage.LoginInfo = item.LoginInfo
|
||||||
|
b.Storage.Request = item.BaseRequest
|
||||||
|
b.Caller.Client.Domain = item.WechatDomain
|
||||||
|
b.uuid = item.UUID
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// NewBot Bot的构造方法
|
// NewBot Bot的构造方法
|
||||||
// 接收外部的 context.Context,用于控制Bot的存活
|
// 接收外部的 context.Context,用于控制Bot的存活
|
||||||
func NewBot(c context.Context) *Bot {
|
func NewBot(c context.Context) *Bot {
|
||||||
@ -331,6 +352,21 @@ func DefaultBot(opts ...BotOptionFunc) *Bot {
|
|||||||
return bot
|
return bot
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// defaultSyncCheckErrHandler 默认的SyncCheck错误处理函数
|
||||||
|
func defaultSyncCheckErrHandler(bot *Bot) func(error) bool {
|
||||||
|
return func(err error) bool {
|
||||||
|
var ret Ret
|
||||||
|
if errors.As(err, &ret) {
|
||||||
|
switch ret {
|
||||||
|
case failedLoginCheck, cookieInvalid, failedLoginWarn:
|
||||||
|
_ = bot.Logout()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// GetQrcodeUrl 通过uuid获取登录二维码的url
|
// GetQrcodeUrl 通过uuid获取登录二维码的url
|
||||||
func GetQrcodeUrl(uuid string) string {
|
func GetQrcodeUrl(uuid string) string {
|
||||||
return qrcode + uuid
|
return qrcode + uuid
|
||||||
@ -365,30 +401,3 @@ func open(url string) error {
|
|||||||
args = append(args, url)
|
args = append(args, url)
|
||||||
return exec.Command(cmd, args...).Start()
|
return exec.Command(cmd, args...).Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsHot returns true if is hot login otherwise false
|
|
||||||
func (b *Bot) IsHot() bool {
|
|
||||||
return b.hotReloadStorage != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UUID returns current uuid of bot
|
|
||||||
func (b *Bot) UUID() string {
|
|
||||||
return b.uuid
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) reload() error {
|
|
||||||
if b.hotReloadStorage == nil {
|
|
||||||
return errors.New("hotReloadStorage is nil")
|
|
||||||
}
|
|
||||||
var item HotReloadStorageItem
|
|
||||||
err := json.NewDecoder(b.hotReloadStorage).Decode(&item)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
b.Caller.Client.Jar = item.Jar.AsCookieJar()
|
|
||||||
b.Storage.LoginInfo = item.LoginInfo
|
|
||||||
b.Storage.Request = item.BaseRequest
|
|
||||||
b.Caller.Client.Domain = item.WechatDomain
|
|
||||||
b.uuid = item.UUID
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
@ -147,7 +147,7 @@ func (c *Caller) SyncCheck(request *BaseRequest, info *LoginInfo, response *WebI
|
|||||||
if len(results) != 3 {
|
if len(results) != 3 {
|
||||||
return nil, errors.New("parse sync key failed")
|
return nil, errors.New("parse sync key failed")
|
||||||
}
|
}
|
||||||
retCode, selector := string(results[1]), string(results[2])
|
retCode, selector := string(results[1]), Selector(results[2])
|
||||||
syncCheckResponse := &SyncCheckResponse{RetCode: retCode, Selector: selector}
|
syncCheckResponse := &SyncCheckResponse{RetCode: retCode, Selector: selector}
|
||||||
return syncCheckResponse, nil
|
return syncCheckResponse, nil
|
||||||
}
|
}
|
||||||
|
25
items.go
25
items.go
@ -2,7 +2,6 @@ package openwechat
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -91,30 +90,6 @@ func NewUserDetailItemList(members Members) UserDetailItemList {
|
|||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
type SyncCheckResponse struct {
|
|
||||||
RetCode string
|
|
||||||
Selector string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SyncCheckResponse) Success() bool {
|
|
||||||
return s.RetCode == "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SyncCheckResponse) NorMal() bool {
|
|
||||||
return s.Success() && s.Selector == "0"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s SyncCheckResponse) Err() error {
|
|
||||||
if s.Success() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
i, err := strconv.ParseInt(s.RetCode, 16, 10)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("sync check unknown error")
|
|
||||||
}
|
|
||||||
return errors.New(Ret(i).String())
|
|
||||||
}
|
|
||||||
|
|
||||||
type WebWxSyncResponse struct {
|
type WebWxSyncResponse struct {
|
||||||
AddMsgCount int
|
AddMsgCount int
|
||||||
ContinueFlag int
|
ContinueFlag int
|
||||||
|
40
sync_check.go
Normal file
40
sync_check.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package openwechat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Selector string
|
||||||
|
|
||||||
|
const (
|
||||||
|
SelectorNormal Selector = "0" // 正常
|
||||||
|
SelectorNewMsg Selector = "2" // 有新消息
|
||||||
|
SelectorModContact Selector = "4" // 联系人信息变更
|
||||||
|
SelectorAddOrDelContact Selector = "6" // 添加或删除联系人
|
||||||
|
SelectorModChatRoom Selector = "7" // 进入或退出聊天室
|
||||||
|
)
|
||||||
|
|
||||||
|
type SyncCheckResponse struct {
|
||||||
|
RetCode string
|
||||||
|
Selector Selector
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s SyncCheckResponse) Success() bool {
|
||||||
|
return s.RetCode == "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s SyncCheckResponse) NorMal() bool {
|
||||||
|
return s.Success() && s.Selector == "0"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s SyncCheckResponse) Err() error {
|
||||||
|
if s.Success() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
i, err := strconv.Atoi(s.RetCode)
|
||||||
|
if err != nil {
|
||||||
|
return errors.New("sync check unknown error")
|
||||||
|
}
|
||||||
|
return Ret(i)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user