initial commit
This commit is contained in:
48
telegram.go
Normal file
48
telegram.go
Normal file
@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"git.tordarus.net/nyaanime/model"
|
||||
"git.tordarus.net/tordarus/adverr/v2"
|
||||
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 || strings.TrimSpace(text) == "" {
|
||||
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())
|
||||
}
|
Reference in New Issue
Block a user