From b49fc182dc368980c36af3edba04a1355cfce827 Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 16 Jan 2022 00:39:24 +0800 Subject: [PATCH] fix telegram session persistence --- pkg/bbgo/environment.go | 6 +++--- pkg/interact/auth.go | 1 - pkg/interact/telegram.go | 26 +++++++++++++------------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index c7411262b..6ec79e1fa 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -719,8 +719,8 @@ func (environ *Environment) setupTelegram(userConfig *Config, telegramBotToken s var sessions = interact.TelegramSessionMap{} var sessionStore = persistence.NewStore("bbgo", "telegram", telegramID) - if err := sessionStore.Load(sessions); err != nil { - + if err := sessionStore.Load(&sessions); err != nil { + log.WithError(err).Errorf("sessions load error") } else { for _, session := range sessions { if session.IsAuthorized() { @@ -734,7 +734,7 @@ func (environ *Environment) setupTelegram(userConfig *Config, telegramBotToken s } messenger.OnAuthorized(func(userSession *interact.TelegramSession) { - log.Infof("saving telegram sessions...") + log.Infof("user session %d got authorized, saving telegram sessions...", userSession.User.ID) if err := sessionStore.Save(messenger.Sessions()); err != nil { log.WithError(err).Errorf("telegram session save error") } diff --git a/pkg/interact/auth.go b/pkg/interact/auth.go index f97592c9c..b62e7eb91 100644 --- a/pkg/interact/auth.go +++ b/pkg/interact/auth.go @@ -101,7 +101,6 @@ func (it *AuthInteract) Commands(interact *Interact) { reply.Message("Great! You're authenticated!") session.SetOriginState(StateAuthenticated) session.SetAuthorized() - session.SetAuthorized() return nil } } diff --git a/pkg/interact/telegram.go b/pkg/interact/telegram.go index 4dc1bb0bf..4d14ead7a 100644 --- a/pkg/interact/telegram.go +++ b/pkg/interact/telegram.go @@ -10,11 +10,7 @@ import ( "gopkg.in/tucnak/telebot.v2" ) -type TelegramSessionKey struct { - UserID, ChatID int64 -} - -type TelegramSessionMap map[TelegramSessionKey]*TelegramSession +type TelegramSessionMap map[int64]*TelegramSession type TelegramSession struct { BaseSession @@ -29,6 +25,11 @@ func (s *TelegramSession) ID() string { return fmt.Sprintf("telegram-%d-%d", s.User.ID, s.Chat.ID) } +func (s *TelegramSession) SetAuthorized() { + s.BaseSession.SetAuthorized() + s.telegram.EmitAuthorized(s) +} + func NewTelegramSession(telegram *Telegram, message *telebot.Message) *TelegramSession { return &TelegramSession{ BaseSession: BaseSession{ @@ -95,7 +96,7 @@ type Telegram struct { authorizing bool - sessions map[TelegramSessionKey]*TelegramSession + sessions map[int64]*TelegramSession // textMessageResponder is used for interact to register its message handler textMessageResponder Responder @@ -130,7 +131,7 @@ func (tm *Telegram) Start(context.Context) { if reply.set { reply.build() - checkSendErr(tm.Bot.Send(m.Sender, reply.message, reply.menu)) + checkSendErr(tm.Bot.Send(m.Chat, reply.message, reply.menu)) } }) @@ -160,17 +161,16 @@ func checkSendErr(m *telebot.Message, err error) { func (tm *Telegram) loadSession(m *telebot.Message) *TelegramSession { if tm.sessions == nil { - tm.sessions = make(map[TelegramSessionKey]*TelegramSession) + tm.sessions = make(map[int64]*TelegramSession) } - key := TelegramSessionKey{UserID: m.Sender.ID, ChatID: m.Chat.ID} - session, ok := tm.sessions[key] + session, ok := tm.sessions[m.Chat.ID] if ok { return session } session = NewTelegramSession(tm, m) - tm.sessions[key] = session + tm.sessions[m.Chat.ID] = session return session } @@ -181,14 +181,14 @@ func (tm *Telegram) AddCommand(cmd *Command, responder Responder) { reply := tm.newReply(session) if err := responder(session, m.Payload, reply); err != nil { log.WithError(err).Errorf("[telegram] responder error") - checkSendErr(tm.Bot.Send(m.Sender, fmt.Sprintf("error: %v", err))) + checkSendErr(tm.Bot.Send(m.Chat, fmt.Sprintf("error: %v", err))) return } // build up the response objects if reply.set { reply.build() - checkSendErr(tm.Bot.Send(m.Sender, reply.message, reply.menu)) + checkSendErr(tm.Bot.Send(m.Chat, reply.message, reply.menu)) } }) }