From: Javier Sancho Date: Thu, 23 Nov 2017 16:18:50 +0000 (+0100) Subject: Sessions working X-Git-Url: https://git.jsancho.org/?p=midgaard_bot.git;a=commitdiff_plain;h=54d3318115d6fd32364f5993ff67d0461c162aef Sessions working --- diff --git a/midgaard_bot.go b/midgaard_bot.go index d4c3410..a311c97 100644 --- a/midgaard_bot.go +++ b/midgaard_bot.go @@ -4,7 +4,6 @@ import ( "context" "log" - "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/jessevdk/go-flags" ) @@ -12,55 +11,29 @@ var config struct { Token string `short:"t" long:"token" description:"Telegram API Token" required:"true"` } -func recv_task(bot *tgbotapi.BotAPI, sendChannel chan tgbotapi.Chattable, ctx context.Context) { - u := tgbotapi.NewUpdate(0) - u.Timeout = 60 - - updates, _ := bot.GetUpdatesChan(u) - - for { - select { - case update := <-updates: - log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) - - msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text) - msg.ReplyToMessageID = update.Message.MessageID - sendChannel <- msg - case <-ctx.Done(): - return - } - } -} - -func send_task(bot *tgbotapi.BotAPI, sendChannel chan tgbotapi.Chattable, ctx context.Context) { - for { - select { - case msg := <-sendChannel: - bot.Send(msg) - case <-ctx.Done(): - return - } - } -} - func main() { _, err := flags.Parse(&config) if err != nil { log.Panic(err) } - bot, err := tgbotapi.NewBotAPI(config.Token) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + err = initSessions(ctx) if err != nil { log.Panic(err) } - bot.Debug = true - - log.Printf("Authorized on account %s", bot.Self.UserName) + err = initTelegramWorkers(config.Token, ctx) + if err != nil { + log.Panic(err) + } - ctx, cancel := context.WithCancel(context.Background()) - sendChannel := make(chan tgbotapi.Chattable) - go send_task(bot, sendChannel, ctx) - recv_task(bot, sendChannel, ctx) - cancel() + for { + select { + case <-ctx.Done(): + break + } + } } diff --git a/sessions.go b/sessions.go index a0ff861..7dd2df3 100644 --- a/sessions.go +++ b/sessions.go @@ -1,18 +1,24 @@ package main import ( + "context" + "log" + "github.com/go-telegram-bot-api/telegram-bot-api" ) type Session struct { Chat *tgbotapi.Chat - Msgbox chan tgbotapi.Message + Input chan *tgbotapi.Message } var sessions map[int64]*Session +var sessionsCtx context.Context -func initSessions() { +func initSessions(ctx context.Context) error { sessions = make(map[int64]*Session) + sessionsCtx = ctx + return nil } func getSession(chat *tgbotapi.Chat) *Session { @@ -24,7 +30,30 @@ func getSession(chat *tgbotapi.Chat) *Session { } func newSession(chat *tgbotapi.Chat) *Session { - session := Session{chat, make(chan tgbotapi.Message)} + session := Session{chat, make(chan *tgbotapi.Message)} sessions[chat.ID] = &session + startSession(&session) return &session } + +func startSession(session *Session) { + ctx, _ := context.WithCancel(sessionsCtx) + go func() { + for { + select { + case msg := <-session.Input: + log.Printf("[%s] %s", msg.From.UserName, msg.Text) + + newMsg := tgbotapi.NewMessage(msg.Chat.ID, msg.Text) + newMsg.ReplyToMessageID = msg.MessageID + sendToTelegram(newMsg) + case <-ctx.Done(): + return + } + } + }() +} + +func sendToSession(session *Session, message *tgbotapi.Message) { + session.Input <- message +} diff --git a/telegram.go b/telegram.go new file mode 100644 index 0000000..3ae6a3d --- /dev/null +++ b/telegram.go @@ -0,0 +1,59 @@ +package main + +import ( + "context" + "log" + + "github.com/go-telegram-bot-api/telegram-bot-api" +) + +var sendChannel chan tgbotapi.Chattable + +func initTelegramWorkers(token string, ctx context.Context) error { + bot, err := tgbotapi.NewBotAPI(token) + if err != nil { + return err + } + + bot.Debug = true + + log.Printf("Authorized on account %s", bot.Self.UserName) + + sendChannel = make(chan tgbotapi.Chattable) + go sendWorker(bot, sendChannel, ctx) + go recvWorker(bot, ctx) + + return nil +} + +func recvWorker(bot *tgbotapi.BotAPI, ctx context.Context) { + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + + updates, _ := bot.GetUpdatesChan(u) + + for { + select { + case update := <-updates: + session := getSession(update.Message.Chat) + sendToSession(session, update.Message) + case <-ctx.Done(): + return + } + } +} + +func sendWorker(bot *tgbotapi.BotAPI, sendChannel chan tgbotapi.Chattable, ctx context.Context) { + for { + select { + case msg := <-sendChannel: + bot.Send(msg) + case <-ctx.Done(): + return + } + } +} + +func sendToTelegram(message tgbotapi.Chattable) { + sendChannel <- message +}