Skip to content

Instantly share code, notes, and snippets.

@fpaupier
Created May 21, 2020 14:53
Show Gist options
  • Save fpaupier/7f0626461459e2fe1a24e161b22a9cd9 to your computer and use it in GitHub Desktop.
Save fpaupier/7f0626461459e2fe1a24e161b22a9cd9 to your computer and use it in GitHub Desktop.
Handler for the Telegram bot @RapGeniusBot
// HandleTelegramWebHook sends a message back to the chat with a punchline starting by the message provided by the user.
func HandleTelegramWebHook(w http.ResponseWriter, r *http.Request) {
// Parse incoming request
var update, err = parseTelegramRequest(r)
if err != nil {
log.Printf("error parsing update, %s", err.Error())
return
}
// Sanitize input
var sanitizedSeed = sanitize(update.Message.Text)
// Call RapLyrics to get a punchline
var lyric, errRapLyrics = getPunchline(sanitizedSeed)
if errRapLyrics != nil {
log.Printf("got error when calling RapLyrics API %s", errRapLyrics.Error())
return
}
// Send the punchline back to Telegram
var telegramResponseBody, errTelegram = sendTextToTelegramChat(update.Message.Chat.Id, lyric)
if errTelegram != nil {
log.Printf("got error %s from telegram, reponse body is %s", errTelegram.Error(), telegramResponseBody)
} else {
log.Printf("punchline %s successfuly distributed to chat id %d", lyric, update.Message.Chat.Id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment