From 9785d3cc4ced2b9e99b49c93129b5ee406e885fd Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sun, 14 Mar 2021 15:32:58 +0100 Subject: [PATCH] Allow telegram chat IDs as file (#301) Co-authored-by: CrazyMax --- docs/notif/telegram.md | 7 ++++++- internal/model/notif_telegram.go | 7 ++++--- internal/notif/telegram/client.go | 14 +++++++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/docs/notif/telegram.md b/docs/notif/telegram.md index a8cba254..46f49e56 100644 --- a/docs/notif/telegram.md +++ b/docs/notif/telegram.md @@ -23,12 +23,17 @@ Multiple chat IDs can be provided in order to deliver notifications to multiple |--------------------|---------------|---------------| | `token` | | Telegram bot token | | `tokenFile` | | Use content of secret file as Telegram bot token if `token` not defined | -| `chatIDs`[^1] | | List of chat IDs to send notifications to | +| `chatIDs` | | List of chat IDs to send notifications to | +| `chatIDsFile` | | Use content of secret file as chat IDs if `chatIDs` not defined | !!! abstract "Environment variables" * `DIUN_NOTIF_TELEGRAM_TOKEN` * `DIUN_NOTIF_TELEGRAM_TOKENFILE` * `DIUN_NOTIF_TELEGRAM_CHATIDS` (comma separated) + * `DIUN_NOTIF_TELEGRAM_CHATIDSFILE` + +!!! example "chat IDs secret file" + Chat IDs secret file must be a valid JSON array like: `[123456789,987654321]` ## Sample diff --git a/internal/model/notif_telegram.go b/internal/model/notif_telegram.go index 1fa82e53..42fc7bf4 100644 --- a/internal/model/notif_telegram.go +++ b/internal/model/notif_telegram.go @@ -2,9 +2,10 @@ package model // NotifTelegram holds Telegram notification configuration details type NotifTelegram struct { - Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"` - TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"` - ChatIDs []int64 `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"required"` + Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"` + TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"` + ChatIDs []int64 `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"` + ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"` } // GetDefaults gets the default values diff --git a/internal/notif/telegram/client.go b/internal/notif/telegram/client.go index 035c5327..f342cad0 100644 --- a/internal/notif/telegram/client.go +++ b/internal/notif/telegram/client.go @@ -1,6 +1,7 @@ package telegram import ( + "encoding/json" "strings" "text/template" @@ -45,6 +46,17 @@ func (c *Client) Send(entry model.NotifEntry) error { return errors.New("Cannot retrieve token secret for Telegram notifier") } + chatIDs := c.cfg.ChatIDs + chatIDsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile) + if err != nil { + return errors.New("Cannot retrieve chat IDs secret for Telegram notifier") + } + if len(chatIDsRaw) > 0 { + if err = json.Unmarshal([]byte(chatIDsRaw), &chatIDs); err != nil { + return errors.New("Cannot unmarshal chat IDs secret for Telegram notifier") + } + } + bot, err := tgbotapi.NewBotAPI(token) if err != nil { return err @@ -72,7 +84,7 @@ func (c *Client) Send(entry model.NotifEntry) error { return err } - for _, chatID := range c.cfg.ChatIDs { + for _, chatID := range chatIDs { _, err := bot.Send(tgbotapi.MessageConfig{ BaseChat: tgbotapi.BaseChat{ ChatID: chatID,