44 lines
965 B
Go
44 lines
965 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, animeEp model.AnimeEpisode) {
|
||
|
b := new(strings.Builder)
|
||
|
if err := messagePattern.Execute(b, animeEp); err != nil {
|
||
|
adverr.Println(adverr.Wrap("could not send telegram message", err))
|
||
|
}
|
||
|
SendTelegramMessage(b.String())
|
||
|
}
|