mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
interact: fix telegram session restore
This commit is contained in:
parent
fb37bce4bf
commit
01afe9c14e
|
@ -713,24 +713,24 @@ func (environ *Environment) setupSlack(userConfig *Config, slackToken string, pe
|
||||||
var sessions = interact.SlackSessionMap{}
|
var sessions = interact.SlackSessionMap{}
|
||||||
var sessionStore = persistence.NewStore("bbgo", "slack")
|
var sessionStore = persistence.NewStore("bbgo", "slack")
|
||||||
if err := sessionStore.Load(&sessions); err != nil {
|
if err := sessionStore.Load(&sessions); err != nil {
|
||||||
log.WithError(err).Errorf("sessions load error")
|
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: this is not necessary for slack, but we should find a way to restore the sessions
|
||||||
|
/*
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
if session.IsAuthorized() {
|
if session.IsAuthorized() {
|
||||||
// notifier.AddChat(session.Chat)
|
// notifier.AddChat(session.Chat)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
messenger.RestoreSessions(sessions)
|
||||||
// you must restore the session after the notifier updates
|
|
||||||
// messenger.RestoreSessions(sessions)
|
|
||||||
}
|
|
||||||
|
|
||||||
messenger.OnAuthorized(func(userSession *interact.SlackSession) {
|
messenger.OnAuthorized(func(userSession *interact.SlackSession) {
|
||||||
if userSession.IsAuthorized() {
|
if userSession.IsAuthorized() {
|
||||||
// notifier.AddChat(userSession.Chat)
|
// notifier.AddChat(userSession.Chat)
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
interact.AddMessenger(messenger)
|
interact.AddMessenger(messenger)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -264,7 +264,7 @@ func (it *Interact) Start(ctx context.Context) error {
|
||||||
|
|
||||||
// TODO: use go routine and context
|
// TODO: use go routine and context
|
||||||
for _, m := range it.messengers {
|
for _, m := range it.messengers {
|
||||||
m.Start(ctx)
|
go m.Start(ctx)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,14 +70,6 @@ func (r *TelegramReply) Message(message string) {
|
||||||
r.set = true
|
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() {
|
func (r *TelegramReply) RemoveKeyboard() {
|
||||||
r.menu.ReplyKeyboardRemove = true
|
r.menu.ReplyKeyboardRemove = true
|
||||||
r.set = true
|
r.set = true
|
||||||
|
@ -139,11 +131,6 @@ func (tm *Telegram) SetTextMessageResponder(responder Responder) {
|
||||||
func (tm *Telegram) Start(context.Context) {
|
func (tm *Telegram) Start(context.Context) {
|
||||||
tm.Bot.Handle(telebot.OnCallback, func(c *telebot.Callback) {
|
tm.Bot.Handle(telebot.OnCallback, func(c *telebot.Callback) {
|
||||||
log.Infof("[telegram] onCallback: %+v", c)
|
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) {
|
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")
|
log.WithError(err).Errorf("[telegram] set commands error")
|
||||||
}
|
}
|
||||||
|
|
||||||
go tm.Bot.Start()
|
tm.Bot.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkSendErr(m *telebot.Message, err error) {
|
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]
|
session, ok := tm.sessions[m.Chat.ID]
|
||||||
if ok {
|
if ok {
|
||||||
|
log.Infof("[telegram] loaded existing session: %+v", session)
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
session = NewTelegramSession(tm, m)
|
session = NewTelegramSession(tm, m)
|
||||||
tm.sessions[m.Chat.ID] = session
|
tm.sessions[m.Chat.ID] = session
|
||||||
|
|
||||||
|
log.Infof("[telegram] allocated a new session: %+v", session)
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,7 +231,7 @@ func (tm *Telegram) Sessions() TelegramSessionMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tm *Telegram) RestoreSessions(sessions TelegramSessionMap) {
|
func (tm *Telegram) RestoreSessions(sessions TelegramSessionMap) {
|
||||||
if len(sessions) == 0 {
|
if sessions == nil || len(sessions) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,6 +242,9 @@ func (tm *Telegram) RestoreSessions(sessions TelegramSessionMap) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update telegram context reference
|
||||||
|
session.telegram = tm
|
||||||
|
|
||||||
if session.IsAuthorized() {
|
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 {
|
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")
|
log.WithError(err).Error("[telegram] can not send telegram message")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user