From 5bef7d8a1ed523f1f30b4ee16fc9e3cc5ca39fb5 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 14 Jan 2022 11:57:01 +0800 Subject: [PATCH] interact: use interaction singleton --- examples/interact/main.go | 10 ++++------ pkg/bbgo/environment.go | 20 +++++++++++++++++--- pkg/interact/default.go | 21 +++++++++++++++++++++ pkg/interact/interact.go | 1 - 4 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 pkg/interact/default.go diff --git a/examples/interact/main.go b/examples/interact/main.go index ce696bd31..248b8ff19 100644 --- a/examples/interact/main.go +++ b/examples/interact/main.go @@ -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) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index b927de631..3024df137 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -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) } diff --git a/pkg/interact/default.go b/pkg/interact/default.go new file mode 100644 index 000000000..09ef93b49 --- /dev/null +++ b/pkg/interact/default.go @@ -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) +} diff --git a/pkg/interact/interact.go b/pkg/interact/interact.go index a756e074f..62062c617 100644 --- a/pkg/interact/interact.go +++ b/pkg/interact/interact.go @@ -222,4 +222,3 @@ func (it *Interact) Start(ctx context.Context) error { it.messenger.Start(ctx) return nil } -