Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions internal/handlers/telegram/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
136 changes: 136 additions & 0 deletions internal/handlers/telegram/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}