添加Bot行为设置函数

This commit is contained in:
eatMoreApple 2021-07-26 12:47:03 +08:00
parent 5259114400
commit 5120404ba7
3 changed files with 23 additions and 10 deletions

View File

@ -24,7 +24,7 @@
`go get` `go get`
```shell ```shell
go get github.com/eatMoreApple/openwechat go get github.com/eatmoreapple/openwechat
``` ```
@ -36,7 +36,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/eatMoreApple/openwechat" "github.com/eatmoreapple/openwechat"
) )
func main() { func main() {
@ -95,7 +95,7 @@ func main() {
### 项目主页 ### 项目主页
[https://github.com/eatMoreApple/openwechat](https://github.com/eatMoreApple/openwechat) [https://github.com/eatmoreapple/openwechat](https://github.com/eatmoreapple/openwechat)

25
bot.go
View File

@ -74,10 +74,8 @@ func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error {
// 如果webInit出错,则说明可能身份信息已经失效 // 如果webInit出错,则说明可能身份信息已经失效
// 如果retry为True的话,则进行正常登陆 // 如果retry为True的话,则进行正常登陆
if err = b.webInit(); err != nil { if err = b.webInit(); err != nil {
if len(retry) > 0 { if len(retry) > 0 && retry[0] {
if retry[0] { return b.Login()
return b.Login()
}
} }
} }
return err return err
@ -176,7 +174,7 @@ func (b *Bot) handleLogin(data []byte) error {
// 如果是热登陆,则将当前的重要信息写入hotReloadStorage // 如果是热登陆,则将当前的重要信息写入hotReloadStorage
if b.isHot { if b.isHot {
if err := b.DumpHotReloadStorage(); err != nil { if err = b.DumpHotReloadStorage(); err != nil {
return err return err
} }
} }
@ -207,7 +205,7 @@ func (b *Bot) webInit() error {
if b.GetMessageErrorHandler == nil { if b.GetMessageErrorHandler == nil {
b.GetMessageErrorHandler = b.stopAsyncCALL b.GetMessageErrorHandler = b.stopAsyncCALL
} }
if err := b.asyncCall(); err != nil { if err = b.asyncCall(); err != nil {
b.GetMessageErrorHandler(err) b.GetMessageErrorHandler(err)
} }
}() }()
@ -308,6 +306,21 @@ func (b *Bot) DumpHotReloadStorage() error {
return b.hotReloadStorage.Dump(item) return b.hotReloadStorage.Dump(item)
} }
// OnLogin is a setter for LoginCallBack
func (b *Bot) OnLogin(f func(body []byte)) {
b.LoginCallBack = f
}
// OnScanned is a setter for ScanCallBack
func (b *Bot) OnScanned(f func(body []byte)) {
b.ScanCallBack = f
}
// OnLogout is a setter for LogoutCallBack
func (b *Bot) OnLogout(f func(bot *Bot)) {
b.LogoutCallBack = f
}
// Bot的构造方法需要自己传入Caller // Bot的构造方法需要自己传入Caller
func NewBot(caller *Caller) *Bot { func NewBot(caller *Caller) *Bot {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())

2
go.mod
View File

@ -1,4 +1,4 @@
module github.com/eatMoreApple/openwechat module github.com/eatmoreapple/openwechat
go 1.15 go 1.15