From 13a391aad4d24734af2c1c274ee8af19442fc2e9 Mon Sep 17 00:00:00 2001 From: imrebuild <42030593+imrebuild@users.noreply.github.com> Date: Sat, 1 Feb 2025 02:01:57 +0800 Subject: [PATCH] Add disable_notification support for telegram. --- docs/notif/telegram.md | 16 +++++++++------- internal/config/config_test.go | 6 ++++-- internal/model/notif_telegram.go | 14 +++++++++----- internal/notif/telegram/client.go | 13 +++++++------ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/docs/notif/telegram.md b/docs/notif/telegram.md index 6276e41f..12d6e709 100644 --- a/docs/notif/telegram.md +++ b/docs/notif/telegram.md @@ -23,13 +23,14 @@ 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` @@ -37,6 +38,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_DISABLENOTIFICATION` !!! example "chat IDs secret file" Chat IDs secret file must be a valid JSON array like: `["123456789","987654321","567891234:25","891256734:25;12"]` diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 472708a6..d3b0a5d4 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -189,7 +189,8 @@ for {{ .Entry.Manifest.Platform }} platform. "567891234:25", "891256734:25;12", }, - TemplateBody: model.NotifTelegramDefaultTemplateBody, + TemplateBody: model.NotifTelegramDefaultTemplateBody, + DisableNotification: utl.NewFalse(), }, Webhook: &model.NotifWebhook{ Endpoint: "http://webhook.foo.com/sd54qad89azd5a", @@ -351,7 +352,8 @@ func TestLoadEnv(t *testing.T) { "8547439", "1234567", }, - TemplateBody: model.NotifTelegramDefaultTemplateBody, + TemplateBody: model.NotifTelegramDefaultTemplateBody, + DisableNotification: utl.NewFalse(), }, }, Providers: &model.Providers{ diff --git a/internal/model/notif_telegram.go b/internal/model/notif_telegram.go index 33fe58a1..d4d13e32 100644 --- a/internal/model/notif_telegram.go +++ b/internal/model/notif_telegram.go @@ -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 @@ -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() } diff --git a/internal/notif/telegram/client.go b/internal/notif/telegram/client.go index c1329e5c..6bbadec5 100644 --- a/internal/notif/telegram/client.go +++ b/internal/notif/telegram/client.go @@ -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 } } @@ -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 }