interact: use interaction singleton

This commit is contained in:
c9s 2022-01-14 11:57:01 +08:00
parent 0114d92f2f
commit 5bef7d8a1e
4 changed files with 42 additions and 10 deletions

View File

@ -128,21 +128,19 @@ func main() {
}
ctx := context.Background()
globalInteraction := interact.New()
globalInteraction.SetMessenger(&interact.Telegram{
interact.SetMessenger(&interact.Telegram{
Private: true,
Bot: b,
})
globalInteraction.AddCustomInteraction(&interact.AuthInteract{
interact.AddCustomInteraction(&interact.AuthInteract{
Strict: true,
Mode: interact.AuthModeToken,
Token: "123",
})
globalInteraction.AddCustomInteraction(&PositionInteraction{})
if err := globalInteraction.Start(ctx); err != nil {
interact.AddCustomInteraction(&PositionInteraction{})
if err := interact.Start(ctx); err != nil {
log.Fatal(err)
}
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)

View File

@ -20,6 +20,7 @@ import (
"gopkg.in/tucnak/telebot.v2"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/interact"
"github.com/c9s/bbgo/pkg/notifier/slacknotifier"
"github.com/c9s/bbgo/pkg/notifier/telegramnotifier"
"github.com/c9s/bbgo/pkg/service"
@ -581,14 +582,27 @@ func (environ *Environment) ConfigureNotificationSystem(userConfig *Config) erro
// allocate a store, so that we can save the chatID for the owner
var sessionStore = persistence.NewStore("bbgo", "telegram", telegramID)
var interaction = telegramnotifier.NewInteraction(bot, sessionStore)
interact.SetMessenger(&interact.Telegram{
Private: true,
Bot: bot,
})
// TODO: replace this background context later
if err := interact.Start(context.Background()); err != nil {
return err
}
authToken := viper.GetString("telegram-bot-auth-token")
if len(authToken) > 0 {
interaction.SetAuthToken(authToken)
interact.AddCustomInteraction(&interact.AuthInteract{
Strict: true,
Mode: interact.AuthModeToken,
Token: authToken,
})
log.Debugf("telegram bot auth token is set, using fixed token for authorization...")
printTelegramAuthTokenGuide(authToken)
}

21
pkg/interact/default.go Normal file
View File

@ -0,0 +1,21 @@
package interact
import "context"
var defaultInteraction = New()
func Default() *Interact {
return defaultInteraction
}
func SetMessenger(messenger Messenger) {
defaultInteraction.SetMessenger(messenger)
}
func AddCustomInteraction(custom CustomInteraction) {
custom.Commands(defaultInteraction)
}
func Start(ctx context.Context) error {
return defaultInteraction.Start(ctx)
}

View File

@ -222,4 +222,3 @@ func (it *Interact) Start(ctx context.Context) error {
it.messenger.Start(ctx)
return nil
}