From 803a174ace115df26b28b4b83c5320df4d71cefd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Rudowicz?= Date: Fri, 3 Jan 2025 21:47:07 +0100 Subject: [PATCH] Omit telegram if no bot api key is provided --- README.md | 4 ---- main.go | 14 ++++++++++---- telegram_utils.go | 6 ++++++ 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 41a6c1b..5cd7b69 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,3 @@ MemoryMax=50M WantedBy=multi-user.target ``` - -## Debugging - -Set the `OMIT_TG` environment variable to, well, omit sending anything over to Telegram and just see the logs instead. diff --git a/main.go b/main.go index bd32655..9c7a4be 100644 --- a/main.go +++ b/main.go @@ -61,11 +61,17 @@ func main() { s := makeSatel(config.SatelAddr, config.PoolInterval.GetDuration()) logger.Printf("Connected to Satel: %s", config.SatelAddr) - bot, err := tgbotapi.NewBotAPI(config.TelegramApiKey) - if err != nil { - panic(err) + var bot TelegramBotSender = nil + if len(config.TelegramApiKey) != 0 { + b, err := tgbotapi.NewBotAPI(config.TelegramApiKey) + if err != nil { + panic(err) + } + logger.Print("Created Telegram Bot API client") + bot = b + } else { + bot = EmptySender{} } - logger.Print("Created Telegram Bot API client") tgSender := TgSender{bot, s, log.New(os.Stderr, "TgFormatter", log.Lmicroseconds), config.ChatIds} diff --git a/telegram_utils.go b/telegram_utils.go index 0b48efe..32832e7 100644 --- a/telegram_utils.go +++ b/telegram_utils.go @@ -11,6 +11,12 @@ type TelegramBotSender interface { Send(c tgbotapi.Chattable) (tgbotapi.Message, error) } +type EmptySender struct{} + +func (self EmptySender) Send(_ tgbotapi.Chattable) (tgbotapi.Message, error) { + return tgbotapi.Message{}, nil +} + type TgSender struct { bot TelegramBotSender s SatelNameGetter