添加 bot option (#176)

This commit is contained in:
多吃点苹果 2023-01-05 18:24:48 +08:00 committed by GitHub
parent e171c1fefa
commit 46e6fb1afb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 8 deletions

12
bot.go
View File

@ -387,8 +387,8 @@ func (b *Bot) OnLogout(f func(bot *Bot)) {
// 接收外部的 context.Context用于控制Bot的存活
func NewBot(c context.Context) *Bot {
caller := DefaultCaller()
// 默认行为为桌面模式
caller.Client.SetMode(Normal)
// 默认行为为网页版微信模式
caller.Client.SetMode(normal)
ctx, cancel := context.WithCancel(c)
return &Bot{Caller: caller, Storage: &Storage{}, context: ctx, cancel: cancel}
}
@ -397,11 +397,8 @@ func NewBot(c context.Context) *Bot {
// mode不传入默认为 openwechat.Desktop,详情见mode
//
// bot := openwechat.DefaultBot(openwechat.Desktop)
func DefaultBot(modes ...Mode) *Bot {
func DefaultBot(opts ...BotOptionFunc) *Bot {
bot := NewBot(context.Background())
if len(modes) > 0 {
bot.Caller.Client.SetMode(modes[0])
}
// 获取二维码回调
bot.UUIDCallback = PrintlnQrcodeUrl
// 扫码回调
@ -417,6 +414,9 @@ func DefaultBot(modes ...Mode) *Bot {
bot.SyncCheckCallback = func(resp SyncCheckResponse) {
log.Printf("RetCode:%s Selector:%s", resp.RetCode, resp.Selector)
}
for _, opt := range opts {
opt(bot)
}
return bot
}

30
bot_option.go Normal file
View File

@ -0,0 +1,30 @@
package openwechat
import (
"context"
)
// BotOptionFunc 用于设置Bot的选项
type BotOptionFunc func(*Bot)
// Normal 网页版微信
func Normal(b *Bot) {
b.Caller.Client.SetMode(normal)
}
// Desktop 模式
func Desktop(b *Bot) {
b.Caller.Client.SetMode(desktop)
}
func WithContext(ctx context.Context) BotOptionFunc {
return func(b *Bot) {
b.context = ctx
}
}
func WithDeviceID(deviceID string) BotOptionFunc {
return func(b *Bot) {
b.SetDeviceId(deviceID)
}
}

View File

@ -13,9 +13,9 @@ type Mode interface {
}
var (
Normal Mode = normalMode{}
normal Mode = normalMode{}
Desktop Mode = desktopMode{}
desktop Mode = desktopMode{}
)
type normalMode struct{}