设置bot的默认行为

This commit is contained in:
eatmoreapple 2021-09-29 11:19:34 +08:00
parent e7f21d702c
commit 6cd4fccba0

28
bot.go
View File

@ -10,13 +10,14 @@ import (
) )
type Bot struct { type Bot struct {
ScanCallBack func(body []byte) // 扫码回调,可获取扫码用户的头像 ScanCallBack func(body []byte) // 扫码回调,可获取扫码用户的头像
LoginCallBack func(body []byte) // 登陆回调 LoginCallBack func(body []byte) // 登陆回调
LogoutCallBack func(bot *Bot) // 退出回调 LogoutCallBack func(bot *Bot) // 退出回调
UUIDCallback func(uuid string) // 获取UUID的回调函数 UUIDCallback func(uuid string) // 获取UUID的回调函数
MessageHandler MessageHandler // 获取消息成功的handle SyncCheckCallback func(resp SyncCheckResponse) // 心跳回调
GetMessageErrorHandler func(err error) // 获取消息发生错误的handle MessageHandler MessageHandler // 获取消息成功的handle
IsHot bool // 是否为热登录模式 GetMessageErrorHandler func(err error) // 获取消息发生错误的handle
IsHot bool // 是否为热登录模式
once sync.Once once sync.Once
err error err error
context context.Context context context.Context
@ -234,6 +235,10 @@ func (b *Bot) asyncCall() error {
if err != nil { if err != nil {
return err return err
} }
// 执行心跳回调
if b.SyncCheckCallback != nil {
b.SyncCheckCallback(*resp)
}
// 如果不是正常的状态码返回,发生了错误,直接退出 // 如果不是正常的状态码返回,发生了错误,直接退出
if !resp.Success() { if !resp.Success() {
return resp return resp
@ -357,6 +362,15 @@ func DefaultBot(modes ...mode) *Bot {
caller.Client.mode = m caller.Client.mode = m
bot := NewBot(caller) bot := NewBot(caller)
bot.UUIDCallback = PrintlnQrcodeUrl bot.UUIDCallback = PrintlnQrcodeUrl
bot.ScanCallBack = func(body []byte) {
log.Println("扫码成功,请在手机上确认登录")
}
bot.LoginCallBack = func(body []byte) {
log.Println("登录成功")
}
bot.SyncCheckCallback = func(resp SyncCheckResponse) {
log.Printf("RetCode:%s Selector:%s", resp.RetCode, resp.Selector)
}
return bot return bot
} }