Skip to content
Merged
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
16 changes: 9 additions & 7 deletions docs/notif/telegram.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ 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.
```

| 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 |
| 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 |
| `disableNotification` | `false` | Send silent message with no sound |

!!! abstract "Environment variables"
* `DIUN_NOTIF_TELEGRAM_TOKEN`
* `DIUN_NOTIF_TELEGRAM_TOKENFILE`
* `DIUN_NOTIF_TELEGRAM_CHATIDS` (comma separated)
* `DIUN_NOTIF_TELEGRAM_CHATIDSFILE`
* `DIUN_NOTIF_TELEGRAM_TEMPLATEBODY`
* `DIUN_NOTIF_TELEGRAM_DISABLENOTIFICATION`

!!! example "chat IDs secret file"
Chat IDs secret file must be a valid JSON array like: `["123456789","987654321","567891234:25","891256734:25;12"]`
Expand Down
6 changes: 4 additions & 2 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
"567891234:25",
"891256734:25;12",
},
TemplateBody: model.NotifTelegramDefaultTemplateBody,
TemplateBody: model.NotifTelegramDefaultTemplateBody,
DisableNotification: utl.NewFalse(),
},
Webhook: &model.NotifWebhook{
Endpoint: "http://webhook.foo.com/sd54qad89azd5a",
Expand Down Expand Up @@ -351,7 +352,8 @@ func TestLoadEnv(t *testing.T) {
"8547439",
"1234567",
},
TemplateBody: model.NotifTelegramDefaultTemplateBody,
TemplateBody: model.NotifTelegramDefaultTemplateBody,
DisableNotification: utl.NewFalse(),
},
},
Providers: &model.Providers{
Expand Down
14 changes: 9 additions & 5 deletions internal/model/notif_telegram.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package model

import "github.com/crazy-max/diun/v4/pkg/utl"

// NotifTelegramDefaultTemplateBody ...
const NotifTelegramDefaultTemplateBody = `Docker tag {{ if .Entry.Image.HubLink }}[{{ .Entry.Image }}]({{ .Entry.Image.HubLink }}){{ else }}{{ .Entry.Image }}{{ end }} which you subscribed to through {{ .Entry.Provider }} provider {{ if (eq .Entry.Status "new") }}is available{{ else }}has been updated{{ end }} on {{ .Entry.Image.Domain }} registry (triggered by {{ escapeMarkdown .Meta.Hostname }} host).`

// 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 []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"`
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
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"`
DisableNotification *bool `yaml:"disableNotification,omitempty" json:"disableNotification,omitempty" validate:"omitempty"`
}

// GetDefaults gets the default values
Expand All @@ -22,4 +25,5 @@ func (s *NotifTelegram) GetDefaults() *NotifTelegram {
// SetDefaults sets the default values
func (s *NotifTelegram) SetDefaults() {
s.TemplateBody = NotifTelegramDefaultTemplateBody
s.DisableNotification = utl.NewFalse()
}
13 changes: 7 additions & 6 deletions internal/notif/telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func (c *Client) Send(entry model.NotifEntry) error {
for _, cid := range parsedChatIDs {
if len(cid.topics) > 0 {
for _, topic := range cid.topics {
if err = sendTelegramMessage(bot, cid.id, topic, string(body)); err != nil {
if err = sendTelegramMessage(bot, cid.id, topic, string(body), *c.cfg.DisableNotification); err != nil {
return err
}
}
} else {
if err = sendTelegramMessage(bot, cid.id, 0, string(body)); err != nil {
if err = sendTelegramMessage(bot, cid.id, 0, string(body), *c.cfg.DisableNotification); err != nil {
return err
}
}
Expand Down Expand Up @@ -151,11 +151,12 @@ func parseChatIDs(entries []string) ([]chatID, error) {
return chatIDs, nil
}

func sendTelegramMessage(bot *gotgbot.Bot, chatID int64, threadID int64, message string) error {
func sendTelegramMessage(bot *gotgbot.Bot, chatID int64, threadID int64, message string, disableNotification bool) error {
_, err := bot.SendMessage(chatID, message, &gotgbot.SendMessageOpts{
MessageThreadId: threadID,
ParseMode: gotgbot.ParseModeMarkdown,
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
MessageThreadId: threadID,
ParseMode: gotgbot.ParseModeMarkdown,
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
DisableNotification: disableNotification,
})
return err
}