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

52 lines
1.1 KiB
Go
Raw 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"
)
2025-01-13 19:37:50 +00:00
type TelegramBot interface {
2024-03-10 22:30:15 +00:00
Send(c tgbotapi.Chattable) (tgbotapi.Message, error)
2025-01-13 19:37:50 +00:00
GetUpdatesChan(u tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel
StopReceivingUpdates()
2024-03-10 22:30:15 +00:00
}
2025-01-13 19:37:50 +00:00
type EmptyBot struct{}
2025-01-13 19:37:50 +00:00
func (_ EmptyBot) Send(_ tgbotapi.Chattable) (tgbotapi.Message, error) {
return tgbotapi.Message{}, nil
}
2025-01-13 19:37:50 +00:00
func (_ EmptyBot) GetUpdatesChan(u tgbotapi.UpdateConfig) tgbotapi.UpdatesChannel {
ch := make(chan tgbotapi.Update)
close(ch)
return ch
}
func (_ EmptyBot) StopReceivingUpdates() {}
2024-03-10 22:30:15 +00:00
type TgSender struct {
2025-01-13 19:37:50 +00:00
bot TelegramBot
2024-03-10 22:30:15 +00:00
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
}