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

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 {
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)}
}