interact: fix telegram session restore

This commit is contained in:
c9s 2022-01-23 01:50:27 +08:00
parent fb37bce4bf
commit 01afe9c14e
3 changed files with 24 additions and 31 deletions

View File

@ -713,24 +713,24 @@ func (environ *Environment) setupSlack(userConfig *Config, slackToken string, pe
var sessions = interact.SlackSessionMap{}
var sessionStore = persistence.NewStore("bbgo", "slack")
if err := sessionStore.Load(&sessions); err != nil {
log.WithError(err).Errorf("sessions load error")
} else {
// TODO: this is not necessary for slack, but we should find a way to restore the sessions
/*
for _, session := range sessions {
if session.IsAuthorized() {
// notifier.AddChat(session.Chat)
}
}
// you must restore the session after the notifier updates
// messenger.RestoreSessions(sessions)
}
messenger.RestoreSessions(sessions)
messenger.OnAuthorized(func(userSession *interact.SlackSession) {
if userSession.IsAuthorized() {
// notifier.AddChat(userSession.Chat)
}
})
*/
}
interact.AddMessenger(messenger)
}

View File

@ -264,7 +264,7 @@ func (it *Interact) Start(ctx context.Context) error {
// TODO: use go routine and context
for _, m := range it.messengers {
m.Start(ctx)
go m.Start(ctx)
}
return nil
}

View File

@ -70,14 +70,6 @@ func (r *TelegramReply) Message(message string) {
r.set = true
}
func (r *TelegramReply) Choose(prompt string, options ...Option) {
}
func (r *TelegramReply) InputText(prompt string, textFields ...TextField) {
r.message = prompt
}
func (r *TelegramReply) RemoveKeyboard() {
r.menu.ReplyKeyboardRemove = true
r.set = true
@ -139,11 +131,6 @@ func (tm *Telegram) SetTextMessageResponder(responder Responder) {
func (tm *Telegram) Start(context.Context) {
tm.Bot.Handle(telebot.OnCallback, func(c *telebot.Callback) {
log.Infof("[telegram] onCallback: %+v", c)
if c.Message != nil {
session := tm.loadSession(c.Message)
_ = session
}
// c.Sender
})
tm.Bot.Handle(telebot.OnText, func(m *telebot.Message) {
@ -185,7 +172,7 @@ func (tm *Telegram) Start(context.Context) {
log.WithError(err).Errorf("[telegram] set commands error")
}
go tm.Bot.Start()
tm.Bot.Start()
}
func checkSendErr(m *telebot.Message, err error) {
@ -201,11 +188,14 @@ func (tm *Telegram) loadSession(m *telebot.Message) *TelegramSession {
session, ok := tm.sessions[m.Chat.ID]
if ok {
log.Infof("[telegram] loaded existing session: %+v", session)
return session
}
session = NewTelegramSession(tm, m)
tm.sessions[m.Chat.ID] = session
log.Infof("[telegram] allocated a new session: %+v", session)
return session
}
@ -241,7 +231,7 @@ func (tm *Telegram) Sessions() TelegramSessionMap {
}
func (tm *Telegram) RestoreSessions(sessions TelegramSessionMap) {
if len(sessions) == 0 {
if sessions == nil || len(sessions) == 0 {
return
}
@ -252,6 +242,9 @@ func (tm *Telegram) RestoreSessions(sessions TelegramSessionMap) {
continue
}
// update telegram context reference
session.telegram = tm
if session.IsAuthorized() {
if _, err := tm.Bot.Send(session.Chat, fmt.Sprintf("Hi %s, I'm back. Your telegram session is restored.", session.User.Username)); err != nil {
log.WithError(err).Error("[telegram] can not send telegram message")