Skip to content

Commit 13a391a

Browse files
committed
Add disable_notification support for telegram.
1 parent 4c5e8ac commit 13a391a

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

docs/notif/telegram.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ Multiple chat IDs can be provided in order to deliver notifications to multiple
2323
Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
2424
```
2525

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

3435
!!! abstract "Environment variables"
3536
* `DIUN_NOTIF_TELEGRAM_TOKEN`
3637
* `DIUN_NOTIF_TELEGRAM_TOKENFILE`
3738
* `DIUN_NOTIF_TELEGRAM_CHATIDS` (comma separated)
3839
* `DIUN_NOTIF_TELEGRAM_CHATIDSFILE`
3940
* `DIUN_NOTIF_TELEGRAM_TEMPLATEBODY`
41+
* `DIUN_NOTIF_TELEGRAM_DISABLENOTIFICATION`
4042

4143
!!! example "chat IDs secret file"
4244
Chat IDs secret file must be a valid JSON array like: `["123456789","987654321","567891234:25","891256734:25;12"]`

internal/config/config_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
189189
"567891234:25",
190190
"891256734:25;12",
191191
},
192-
TemplateBody: model.NotifTelegramDefaultTemplateBody,
192+
TemplateBody: model.NotifTelegramDefaultTemplateBody,
193+
DisableNotification: utl.NewFalse(),
193194
},
194195
Webhook: &model.NotifWebhook{
195196
Endpoint: "http://webhook.foo.com/sd54qad89azd5a",
@@ -351,7 +352,8 @@ func TestLoadEnv(t *testing.T) {
351352
"8547439",
352353
"1234567",
353354
},
354-
TemplateBody: model.NotifTelegramDefaultTemplateBody,
355+
TemplateBody: model.NotifTelegramDefaultTemplateBody,
356+
DisableNotification: utl.NewFalse(),
355357
},
356358
},
357359
Providers: &model.Providers{

internal/model/notif_telegram.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package model
22

3+
import "github.com/crazy-max/diun/v4/pkg/utl"
4+
35
// NotifTelegramDefaultTemplateBody ...
46
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).`
57

68
// NotifTelegram holds Telegram notification configuration details
79
type NotifTelegram struct {
8-
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
9-
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
10-
ChatIDs []string `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"`
11-
ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"`
12-
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
10+
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
11+
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
12+
ChatIDs []string `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"`
13+
ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"`
14+
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"`
15+
DisableNotification *bool `yaml:"disableNotification,omitempty" json:"disableNotification,omitempty" validate:"omitempty"`
1316
}
1417

1518
// GetDefaults gets the default values
@@ -22,4 +25,5 @@ func (s *NotifTelegram) GetDefaults() *NotifTelegram {
2225
// SetDefaults sets the default values
2326
func (s *NotifTelegram) SetDefaults() {
2427
s.TemplateBody = NotifTelegramDefaultTemplateBody
28+
s.DisableNotification = utl.NewFalse()
2529
}

internal/notif/telegram/client.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ func (c *Client) Send(entry model.NotifEntry) error {
107107
for _, cid := range parsedChatIDs {
108108
if len(cid.topics) > 0 {
109109
for _, topic := range cid.topics {
110-
if err = sendTelegramMessage(bot, cid.id, topic, string(body)); err != nil {
110+
if err = sendTelegramMessage(bot, cid.id, topic, string(body), *c.cfg.DisableNotification); err != nil {
111111
return err
112112
}
113113
}
114114
} else {
115-
if err = sendTelegramMessage(bot, cid.id, 0, string(body)); err != nil {
115+
if err = sendTelegramMessage(bot, cid.id, 0, string(body), *c.cfg.DisableNotification); err != nil {
116116
return err
117117
}
118118
}
@@ -151,11 +151,12 @@ func parseChatIDs(entries []string) ([]chatID, error) {
151151
return chatIDs, nil
152152
}
153153

154-
func sendTelegramMessage(bot *gotgbot.Bot, chatID int64, threadID int64, message string) error {
154+
func sendTelegramMessage(bot *gotgbot.Bot, chatID int64, threadID int64, message string, disableNotification bool) error {
155155
_, err := bot.SendMessage(chatID, message, &gotgbot.SendMessageOpts{
156-
MessageThreadId: threadID,
157-
ParseMode: gotgbot.ParseModeMarkdown,
158-
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
156+
MessageThreadId: threadID,
157+
ParseMode: gotgbot.ParseModeMarkdown,
158+
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
159+
DisableNotification: disableNotification,
159160
})
160161
return err
161162
}

0 commit comments

Comments
 (0)