From c56cd650e0a3391831e211449b2105f2eb10215d Mon Sep 17 00:00:00 2001 From: Tim Zabel Date: Sat, 28 Mar 2020 21:44:59 -0400 Subject: [PATCH] Create Telegram update handler. (#285) --- internal/handlers/telegram/handler.go | 29 +++++++++++++++++++------- internal/handlers/telegram/telegram.go | 20 +++--------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/internal/handlers/telegram/handler.go b/internal/handlers/telegram/handler.go index 5edde511..cd82c47c 100644 --- a/internal/handlers/telegram/handler.go +++ b/internal/handlers/telegram/handler.go @@ -4,14 +4,6 @@ import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) -// TODO: These format strings are currently unused, so commenting out for now -/* -const ( - joinFmt = "%s (@%s) has joined the Telegram Group!" - partFmt = "%s (@%s) has left the Telegram Group." -) -*/ - /* Handler specifies a function that handles a Telegram update. In this case, we take a Telegram client and update object, @@ -19,6 +11,27 @@ where the specific Handler will "handle" the given event. */ type Handler = func(tg *Client, u tgbotapi.Update) +/* +updateHandler takes in a Telegram Update channel, and determines +which handler to fire off +*/ +func updateHandler(tg *Client, updates tgbotapi.UpdatesChannel) { + for u := range updates { + switch { + case u.Message == nil: + continue + case u.Message.Text != "": + messageHandler(tg, u) + case u.Message.Sticker != nil: + stickerHandler(tg, u) + case u.Message.Document != nil: + documentHandler(tg, u.Message) + default: + continue + } + } +} + /* messageHandler handles the Message Telegram Object, which formats the Telegram update into a simple string for IRC. diff --git a/internal/handlers/telegram/telegram.go b/internal/handlers/telegram/telegram.go index d9f81599..53efd3fa 100644 --- a/internal/handlers/telegram/telegram.go +++ b/internal/handlers/telegram/telegram.go @@ -2,7 +2,6 @@ package telegram import ( - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/ritlug/teleirc/internal" ) @@ -56,7 +55,7 @@ func (tg *Client) StartBot(errChan chan<- error, sendMessage func(string)) { tg.logger.LogError(err) errChan <- err } - tg.logger.LogDebug("Authorized on account",tg.api.Self.UserName) + tg.logger.LogInfo("Authorized on account", tg.api.Self.UserName) tg.sendToIrc = sendMessage u := tgbotapi.NewUpdate(0) @@ -68,20 +67,7 @@ func (tg *Client) StartBot(errChan chan<- error, sendMessage func(string)) { tg.logger.LogError(err) } - // TODO: Move these lines into the updateHandler when available - for update := range updates { - switch { - case update.Message == nil: - continue - case update.Message.Text != "": - messageHandler(tg, update) - case update.Message.Sticker != nil: - stickerHandler(tg, update) - case update.Message.Document != nil: - documentHandler(tg, update.Message) - default: - continue - } - } + updateHandler(tg, updates) + errChan <- nil }