downloader/telegram.go

49 lines
1008 B
Go

package main
import (
"strings"
"text/template"
"git.milar.in/milarin/adverr"
"git.milar.in/nyaanime/model"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
var TelegramBot *tgbotapi.BotAPI
func InitTelegramBot() error {
if TelegramBotToken != "" && TelegramChatID != 0 {
bot, err := tgbotapi.NewBotAPI(TelegramBotToken)
if err != nil {
return err
}
TelegramBot = bot
}
return nil
}
func SendTelegramMessage(text string) {
if TelegramBot == nil {
return
}
msg := tgbotapi.NewMessage(TelegramChatID, text)
_, err := TelegramBot.Send(msg)
if err != nil {
adverr.Println(adverr.Wrap("could not send telegram message", err))
}
}
func SendTelegramAnimeEpMessage(messagePattern *template.Template, animeEps []model.AnimeEpisode) {
if len(animeEps) == 0 {
return
}
b := new(strings.Builder)
if err := messagePattern.Execute(b, animeEps); err != nil {
adverr.Println(adverr.Wrap("could not send telegram message", err))
}
SendTelegramMessage(b.String())
}