fix telegram session persistence

This commit is contained in:
c9s 2022-01-16 00:39:24 +08:00
parent 2088234b44
commit b49fc182dc
3 changed files with 16 additions and 17 deletions

View File

@ -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")
}

View File

@ -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
}
}

View File

@ -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))
}
})
}