Skip to content

Commit

Permalink
Create Telegram update handler. (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjzabel committed Mar 29, 2020
1 parent 10109fb commit c56cd65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
29 changes: 21 additions & 8 deletions internal/handlers/telegram/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@ 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,
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.
Expand Down
20 changes: 3 additions & 17 deletions internal/handlers/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package telegram

import (

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/ritlug/teleirc/internal"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

0 comments on commit c56cd65

Please sign in to comment.