downloader/telegram.go

49 lines
1.0 KiB
Go
Raw Normal View History

2023-01-15 20:59:54 +01:00
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) {
2023-01-20 14:31:27 +01:00
if TelegramBot == nil || strings.TrimSpace(text) == "" {
2023-01-15 20:59:54 +01:00
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
}
2023-01-15 20:59:54 +01:00
b := new(strings.Builder)
if err := messagePattern.Execute(b, animeEps); err != nil {
2023-01-15 20:59:54 +01:00
adverr.Println(adverr.Wrap("could not send telegram message", err))
}
2023-01-15 20:59:54 +01:00
SendTelegramMessage(b.String())
}