1
0
Fork 0
hswro-alarm-bot/telegram_utils.go

36 lines
697 B
Go
Raw Permalink Normal View History

2024-03-10 22:30:15 +00:00
package main
import (
"html/template"
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type TelegramBotSender interface {
Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
}
type TgSender struct {
bot TelegramBotSender
s SatelNameGetter
logger *log.Logger
chatIds []int64
}
func (self TgSender) Send(msg GenericMessage, tpl *template.Template) error {
if len(self.chatIds) == 0 {
return nil
}
message := msg.Format(tpl, self.s, self.logger)
for _, chatId := range self.chatIds {
toSend := tgbotapi.NewMessage(chatId, message)
toSend.ParseMode = "HTML"
_, err := self.bot.Send(toSend)
if err != nil {
return err
}
}
return nil
}