Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/notif/telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ Multiple chat IDs can be provided in order to deliver notifications to multiple
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
```

!!! example "File with templateFile"
```yaml
notif:
telegram:
token: aabbccdd:11223344
chatIDs:
- "123456789"
- "987654321"
templateFile: /path/to/telegram-template.txt
```

| Name | Default | Description |
|-----------------------|------------------------------------|---------------------------------------------------------------------------|
| `token` | | Telegram bot token |
| `tokenFile` | | Use content of secret file as Telegram bot token if `token` not defined |
| `chatIDs` | | List of [chat IDs](#chatids-format) to send notifications to |
| `chatIDsFile` | | Use content of secret file as chat IDs if `chatIDs` not defined |
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body |
| `templateFile` | | Use content of file as template body if `templateBody` not defined |
| `disableNotification` | `false` | Send silent message with no sound |

!!! abstract "Environment variables"
Expand All @@ -38,6 +50,7 @@ Multiple chat IDs can be provided in order to deliver notifications to multiple
* `DIUN_NOTIF_TELEGRAM_CHATIDS` (comma separated)
* `DIUN_NOTIF_TELEGRAM_CHATIDSFILE`
* `DIUN_NOTIF_TELEGRAM_TEMPLATEBODY`
* `DIUN_NOTIF_TELEGRAM_TEMPLATEFILE`
* `DIUN_NOTIF_TELEGRAM_DISABLENOTIFICATION`

!!! example "chat IDs secret file"
Expand Down
4 changes: 2 additions & 2 deletions internal/app/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ func (di *Diun) createJob(job model.Job) {
reg = (&model.RegOpt{}).GetDefaults()
}

regUser, err := utl.GetSecret(reg.Username, reg.UsernameFile)
regUser, err := utl.GetValueOrFileContents(reg.Username, reg.UsernameFile)
if err != nil {
log.Warn().Err(err).Msgf("Cannot retrieve username secret for regopts %s", reg.Name)
}
regPassword, err := utl.GetSecret(reg.Password, reg.PasswordFile)
regPassword, err := utl.GetValueOrFileContents(reg.Password, reg.PasswordFile)
if err != nil {
log.Warn().Err(err).Msgf("Cannot retrieve password secret for regopts %s", reg.Name)
}
Expand Down
1 change: 1 addition & 0 deletions internal/model/notif_telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type NotifTelegram struct {
ChatIDs []string `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"`
ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
TemplateFile string `yaml:"templateFile,omitempty" json:"templateFile,omitempty" validate:"omitempty,file"`
DisableNotification *bool `yaml:"disableNotification,omitempty" json:"disableNotification,omitempty" validate:"omitempty"`
}

Expand Down
4 changes: 2 additions & 2 deletions internal/notif/amqp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func (c *Client) Name() string {

// Send creates and sends a amqp notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
username, err := utl.GetSecret(c.cfg.Username, c.cfg.UsernameFile)
username, err := utl.GetValueOrFileContents(c.cfg.Username, c.cfg.UsernameFile)
if err != nil {
return err
}

password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile)
password, err := utl.GetValueOrFileContents(c.cfg.Password, c.cfg.PasswordFile)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notif/apprise/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
u.Path = path.Join(u.Path, "notify")

if c.cfg.Token != "" || c.cfg.TokenFile != "" {
token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile)
token, err := utl.GetValueOrFileContents(c.cfg.Token, c.cfg.TokenFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve token secret for Apprise notifier")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notif/discord/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *Client) Name() string {
func (c *Client) Send(entry model.NotifEntry) error {
var content bytes.Buffer

webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
webhookURL, err := utl.GetValueOrFileContents(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve webhook URL for Discord notifier")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/notif/elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func (c *Client) Name() string {

// Send creates and sends an elasticsearch notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
username, err := utl.GetSecret(c.cfg.Username, c.cfg.UsernameFile)
username, err := utl.GetValueOrFileContents(c.cfg.Username, c.cfg.UsernameFile)
if err != nil {
return err
}

password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile)
password, err := utl.GetValueOrFileContents(c.cfg.Password, c.cfg.PasswordFile)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notif/gotify/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *Client) Name() string {

// Send creates and sends a gotify notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile)
token, err := utl.GetValueOrFileContents(c.cfg.Token, c.cfg.TokenFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve token secret for Gotify notifier")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/notif/mail/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ func (c *Client) Send(entry model.NotifEntry) error {
}
}

username, err := utl.GetSecret(c.cfg.Username, c.cfg.UsernameFile)
username, err := utl.GetValueOrFileContents(c.cfg.Username, c.cfg.UsernameFile)
if err != nil {
log.Warn().Err(err).Msg("Cannot retrieve username secret for mail notifier")
}
password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile)
password, err := utl.GetValueOrFileContents(c.cfg.Password, c.cfg.PasswordFile)
if err != nil {
log.Warn().Err(err).Msg("Cannot retrieve password secret for mail notifier")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/notif/matrix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (c *Client) Send(entry model.NotifEntry) error {
}
}()

user, err := utl.GetSecret(c.cfg.User, c.cfg.UserFile)
user, err := utl.GetValueOrFileContents(c.cfg.User, c.cfg.UserFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve username secret for Matrix notifier")
}
password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile)
password, err := utl.GetValueOrFileContents(c.cfg.Password, c.cfg.PasswordFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve password secret for Matrix notifier")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/notif/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ func (c *Client) Name() string {

// Send creates and sends a mqtt notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
username, err := utl.GetSecret(c.cfg.Username, c.cfg.UsernameFile)
username, err := utl.GetValueOrFileContents(c.cfg.Username, c.cfg.UsernameFile)
if err != nil {
return err
}

password, err := utl.GetSecret(c.cfg.Password, c.cfg.PasswordFile)
password, err := utl.GetValueOrFileContents(c.cfg.Password, c.cfg.PasswordFile)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notif/ntfy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
return err
}

if token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile); err == nil && token != "" {
if token, err := utl.GetValueOrFileContents(c.cfg.Token, c.cfg.TokenFile); err == nil && token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
req.Header.Set("Content-Type", "application/json")
Expand Down
4 changes: 2 additions & 2 deletions internal/notif/pushover/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ func (c *Client) Name() string {

// Send creates and sends a Pushover notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile)
token, err := utl.GetValueOrFileContents(c.cfg.Token, c.cfg.TokenFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve token secret for Pushover notifier")
} else if token == "" {
return errors.New("Pushover API token cannot be empty")
}

recipient, err := utl.GetSecret(c.cfg.Recipient, c.cfg.RecipientFile)
recipient, err := utl.GetValueOrFileContents(c.cfg.Recipient, c.cfg.RecipientFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve recipient secret for Pushover notifier")
} else if recipient == "" {
Expand Down
2 changes: 1 addition & 1 deletion internal/notif/rocketchat/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (c *Client) Name() string {
// Send creates and sends a rocketchat notification with an entry
// https://rocket.chat/docs/developer-guides/rest-api/chat/postmessage/
func (c *Client) Send(entry model.NotifEntry) error {
token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile)
token, err := utl.GetValueOrFileContents(c.cfg.Token, c.cfg.TokenFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve token secret for RocketChat notifier")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notif/slack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *Client) Name() string {

// Send creates and sends a slack notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
webhookURL, err := utl.GetValueOrFileContents(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve webhook URL for Slack notifier")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notif/teams/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Fact struct {

// Send creates and sends a webhook notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
webhookURL, err := utl.GetSecret(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
webhookURL, err := utl.GetValueOrFileContents(c.cfg.WebhookURL, c.cfg.WebhookURLFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve webhook URL for Teams notifier")
}
Expand Down
11 changes: 8 additions & 3 deletions internal/notif/telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func (c *Client) Name() string {

// Send creates and sends a Telegram notification with an entry
func (c *Client) Send(entry model.NotifEntry) error {
token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile)
token, err := utl.GetValueOrFileContents(c.cfg.Token, c.cfg.TokenFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve token secret for Telegram notifier")
}

cids := c.cfg.ChatIDs
cidsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile)
cidsRaw, err := utl.GetValueOrFileContents("", c.cfg.ChatIDsFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve chat IDs secret for Telegram notifier")
}
Expand Down Expand Up @@ -81,10 +81,15 @@ func (c *Client) Send(entry model.NotifEntry) error {
return errors.Wrap(err, "failed to create telegram bot client")
}

templateBody, err := utl.GetValueOrFileContents(c.cfg.TemplateBody, c.cfg.TemplateFile)
if err != nil {
return errors.Wrap(err, "cannot get template for Telegram notifier")
}

message, err := msg.New(msg.Options{
Meta: c.meta,
Entry: entry,
TemplateBody: c.cfg.TemplateBody,
TemplateBody: templateBody,
TemplateFuncs: template.FuncMap{
"escapeMarkdown": func(text string) string {
text = strings.ReplaceAll(text, "_", "\\_")
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func newExternalClusterClient(opts Options) (*kubernetes.Clientset, error) {
return nil, errors.New("endpoint missing for external cluster client")
}

opts.Token, err = utl.GetSecret(opts.Token, opts.TokenFile)
opts.Token, err = utl.GetValueOrFileContents(opts.Token, opts.TokenFile)
if err != nil {
return nil, errors.Wrap(err, "cannot retrieve bearer token")
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/utl/utl.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ func GetEnv(key, fallback string) string {
return fallback
}

// GetSecret retrieves secret's value from plaintext or filename if defined
func GetSecret(plaintext, filename string) (string, error) {
// GetValueOrFileContents retrieves a value from plaintext or filename if defined.
// This function can be used for secrets, templates, or any content that needs
// to be loaded either from a direct value or from a file.
func GetValueOrFileContents(plaintext, filename string) (string, error) {
if plaintext != "" {
return plaintext, nil
} else if filename != "" {
Expand Down