diff --git a/internal/handlers/telegram/handler.go b/internal/handlers/telegram/handler.go index 021f49d..2db3a17 100644 --- a/internal/handlers/telegram/handler.go +++ b/internal/handlers/telegram/handler.go @@ -46,6 +46,12 @@ func updateHandler(tg *Client, updates tgbotapi.UpdatesChannel) { case u.Message.Location != nil: tg.logger.LogDebug("locationHandler triggered") locationHandler(tg, u.Message) + case u.Message.Video != nil: + tg.logger.LogDebug("videoHandler triggered") + videoHandler(tg, u.Message) + case u.Message.Voice != nil: + tg.logger.LogDebug("voiceHandler triggered") + voiceHandler(tg, u.Message) default: tg.logger.LogWarning("Triggered, but message type is currently unsupported") tg.logger.LogWarning("Unhandled Update:", u) @@ -217,3 +223,35 @@ func locationHandler(tg *Client, u *tgbotapi.Message) { tg.sendToIrc(formatted) } + +/* +videoHandler receives a video object from Telegram, and sends +a notification to IRC. +*/ +func videoHandler(tg *Client, u *tgbotapi.Message) { + username := GetUsername(tg.IRCSettings.ShowZWSP, u.From) + formatted := username + " shared a video" + if u.Caption != "" { + formatted += " on Telegram with caption: '" + u.Caption + "'." + } else { + formatted += " on Telegram." + } + + tg.sendToIrc(formatted) +} + +/* +voiceHandler receives a voice message object from Telegram, and sends +a notification to IRC. +*/ +func voiceHandler(tg *Client, u *tgbotapi.Message) { + username := GetUsername(tg.IRCSettings.ShowZWSP, u.From) + formatted := username + " shared a voice message" + if u.Caption != "" { + formatted += " on Telegram with caption: '" + u.Caption + "'." + } else { + formatted += " on Telegram." + } + + tg.sendToIrc(formatted) +} diff --git a/internal/handlers/telegram/handler_test.go b/internal/handlers/telegram/handler_test.go index e356c5b..4b3ecbb 100644 --- a/internal/handlers/telegram/handler_test.go +++ b/internal/handlers/telegram/handler_test.go @@ -1012,3 +1012,139 @@ func TestLocationHandlerWithLocationDisabled(t *testing.T) { locationHandler(clientObj, &messageObj) } + +/* +TestVideoHandlerWithCaption tests video handler with a caption +*/ +func TestVideoHandlerWithCaption(t *testing.T) { + testUser := &tgbotapi.User{ + ID: 1, + UserName: "test", + FirstName: "testing", + LastName: "123", + } + + correct := "test shared a video on Telegram with caption: 'Check this out!'." + + messageObj := tgbotapi.Message{ + From: testUser, + Video: &tgbotapi.Video{ + FileID: "AgADBAAD...", + Width: 1080, + Height: 1920, + Duration: 60, + }, + Caption: "Check this out!", + } + clientObj := &Client{ + IRCSettings: &internal.IRCSettings{ + ShowZWSP: false, + }, + sendToIrc: func(s string) { + assert.Equal(t, correct, s) + }, + } + + videoHandler(clientObj, &messageObj) +} + +/* +TestVideoHandlerWithoutCaption tests video handler without a caption +*/ +func TestVideoHandlerWithoutCaption(t *testing.T) { + testUser := &tgbotapi.User{ + ID: 1, + UserName: "test", + FirstName: "testing", + LastName: "123", + } + + correct := "test shared a video on Telegram." + + messageObj := tgbotapi.Message{ + From: testUser, + Video: &tgbotapi.Video{ + FileID: "AgADBAAD...", + Width: 1080, + Height: 1920, + Duration: 60, + }, + Caption: "", + } + clientObj := &Client{ + IRCSettings: &internal.IRCSettings{ + ShowZWSP: false, + }, + sendToIrc: func(s string) { + assert.Equal(t, correct, s) + }, + } + + videoHandler(clientObj, &messageObj) +} + +/* +TestVoiceHandlerWithCaption tests voice handler with a caption +*/ +func TestVoiceHandlerWithCaption(t *testing.T) { + testUser := &tgbotapi.User{ + ID: 1, + UserName: "test", + FirstName: "testing", + LastName: "123", + } + + correct := "test shared a voice message on Telegram with caption: 'Listen to this!'." + + messageObj := tgbotapi.Message{ + From: testUser, + Voice: &tgbotapi.Voice{ + FileID: "AwADBAAD...", + Duration: 30, + }, + Caption: "Listen to this!", + } + clientObj := &Client{ + IRCSettings: &internal.IRCSettings{ + ShowZWSP: false, + }, + sendToIrc: func(s string) { + assert.Equal(t, correct, s) + }, + } + + voiceHandler(clientObj, &messageObj) +} + +/* +TestVoiceHandlerWithoutCaption tests voice handler without a caption +*/ +func TestVoiceHandlerWithoutCaption(t *testing.T) { + testUser := &tgbotapi.User{ + ID: 1, + UserName: "test", + FirstName: "testing", + LastName: "123", + } + + correct := "test shared a voice message on Telegram." + + messageObj := tgbotapi.Message{ + From: testUser, + Voice: &tgbotapi.Voice{ + FileID: "AwADBAAD...", + Duration: 30, + }, + Caption: "", + } + clientObj := &Client{ + IRCSettings: &internal.IRCSettings{ + ShowZWSP: false, + }, + sendToIrc: func(s string) { + assert.Equal(t, correct, s) + }, + } + + voiceHandler(clientObj, &messageObj) +}