From 71172e5aff52a802172fa7cc1bf7a88e567ad09b Mon Sep 17 00:00:00 2001 From: eatMoreApple <15055461510@163.com> Date: Wed, 28 Apr 2021 11:17:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E5=A4=B1=E8=B4=A5=E7=9A=84=E5=A4=84=E7=90=86=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/bot.go b/bot.go index 5e78bcf..6d2af6c 100644 --- a/bot.go +++ b/bot.go @@ -7,17 +7,18 @@ import ( ) type Bot struct { - ScanCallBack func(body []byte) - LoginCallBack func(body []byte) - UUIDCallback func(uuid string) - MessageHandler func(msg *Message) - isHot bool - err error - exit chan bool - Caller *Caller - self *Self - storage *Storage - hotReloadStorage HotReloadStorage + ScanCallBack func(body []byte) // 扫码回调,可获取扫码用户的头像 + LoginCallBack func(body []byte) // 登陆回调 + UUIDCallback func(uuid string) // 获取UUID的回调函数 + MessageHandler func(msg *Message) // 获取消息成功的handle + GetMessageErrorHandler func(err error) // 获取消息发生错误的handle + isHot bool + err error + exit chan bool + Caller *Caller + self *Self + storage *Storage + hotReloadStorage HotReloadStorage } // 判断当前用户是否正常在线 @@ -181,7 +182,12 @@ func (b *Bot) webInit() error { } // 开启协程,轮训获取是否有新的消息返回 go func() { - b.stopAsyncCALL(b.asyncCall()) + if b.GetMessageErrorHandler == nil { + b.GetMessageErrorHandler = b.stopAsyncCALL + } + if err := b.asyncCall(); err != nil { + b.GetMessageErrorHandler(err) + } }() return nil } @@ -213,6 +219,7 @@ func (b *Bot) asyncCall() error { return err } +// 当获取消息发生错误时, 默认的错误处理行为 func (b *Bot) stopAsyncCALL(err error) { b.exit <- true b.err = err @@ -257,6 +264,16 @@ func (b *Bot) CrashReason() error { return b.err } +// setter for Bot.MessageHandler +func (b *Bot) MessageOnSuccess(h func(msg *Message)) { + b.MessageHandler = h +} + +// setter for Bot.GetMessageErrorHandler +func (b *Bot) MessageOnError(h func(err error)) { + b.GetMessageErrorHandler = h +} + func NewBot(caller *Caller) *Bot { return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)} }