增加获取消息失败的处理函数

This commit is contained in:
eatMoreApple 2021-04-28 11:17:20 +08:00
parent 57c397810d
commit 71172e5aff

41
bot.go
View File

@ -7,17 +7,18 @@ import (
) )
type Bot struct { type Bot struct {
ScanCallBack func(body []byte) ScanCallBack func(body []byte) // 扫码回调,可获取扫码用户的头像
LoginCallBack func(body []byte) LoginCallBack func(body []byte) // 登陆回调
UUIDCallback func(uuid string) UUIDCallback func(uuid string) // 获取UUID的回调函数
MessageHandler func(msg *Message) MessageHandler func(msg *Message) // 获取消息成功的handle
isHot bool GetMessageErrorHandler func(err error) // 获取消息发生错误的handle
err error isHot bool
exit chan bool err error
Caller *Caller exit chan bool
self *Self Caller *Caller
storage *Storage self *Self
hotReloadStorage HotReloadStorage storage *Storage
hotReloadStorage HotReloadStorage
} }
// 判断当前用户是否正常在线 // 判断当前用户是否正常在线
@ -181,7 +182,12 @@ func (b *Bot) webInit() error {
} }
// 开启协程,轮训获取是否有新的消息返回 // 开启协程,轮训获取是否有新的消息返回
go func() { 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 return nil
} }
@ -213,6 +219,7 @@ func (b *Bot) asyncCall() error {
return err return err
} }
// 当获取消息发生错误时, 默认的错误处理行为
func (b *Bot) stopAsyncCALL(err error) { func (b *Bot) stopAsyncCALL(err error) {
b.exit <- true b.exit <- true
b.err = err b.err = err
@ -257,6 +264,16 @@ func (b *Bot) CrashReason() error {
return b.err 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 { func NewBot(caller *Caller) *Bot {
return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)} return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)}
} }