mirror of
https://github.com/crazy-max/diun.git
synced 2026-01-05 04:15:37 +01:00
Allow telegram chat IDs as file (#301)
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user