Add disable_notification support for telegram.

This commit is contained in:
imrebuild
2025-02-01 02:01:57 +08:00
parent 4c5e8ac53a
commit 13a391aad4
4 changed files with 29 additions and 20 deletions

View File

@@ -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. Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released.
``` ```
| Name | Default | Description | | Name | Default | Description |
|--------------------|------------------------------------|---------------------------------------------------------------------------| |-----------------------|------------------------------------|---------------------------------------------------------------------------|
| `token` | | Telegram bot token | | `token` | | Telegram bot token |
| `tokenFile` | | Use content of secret file as Telegram bot token if `token` not defined | | `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 | | `chatIDs` | | List of [chat IDs](#chatids-format) to send notifications to |
| `chatIDsFile` | | Use content of secret file as chat IDs if `chatIDs` not defined | | `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 | | `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" !!! abstract "Environment variables"
* `DIUN_NOTIF_TELEGRAM_TOKEN` * `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_CHATIDS` (comma separated)
* `DIUN_NOTIF_TELEGRAM_CHATIDSFILE` * `DIUN_NOTIF_TELEGRAM_CHATIDSFILE`
* `DIUN_NOTIF_TELEGRAM_TEMPLATEBODY` * `DIUN_NOTIF_TELEGRAM_TEMPLATEBODY`
* `DIUN_NOTIF_TELEGRAM_DISABLENOTIFICATION`
!!! example "chat IDs secret file" !!! example "chat IDs secret file"
Chat IDs secret file must be a valid JSON array like: `["123456789","987654321","567891234:25","891256734:25;12"]` Chat IDs secret file must be a valid JSON array like: `["123456789","987654321","567891234:25","891256734:25;12"]`

View File

@@ -189,7 +189,8 @@ for <code>{{ .Entry.Manifest.Platform }}</code> platform.
"567891234:25", "567891234:25",
"891256734:25;12", "891256734:25;12",
}, },
TemplateBody: model.NotifTelegramDefaultTemplateBody, TemplateBody: model.NotifTelegramDefaultTemplateBody,
DisableNotification: utl.NewFalse(),
}, },
Webhook: &model.NotifWebhook{ Webhook: &model.NotifWebhook{
Endpoint: "http://webhook.foo.com/sd54qad89azd5a", Endpoint: "http://webhook.foo.com/sd54qad89azd5a",
@@ -351,7 +352,8 @@ func TestLoadEnv(t *testing.T) {
"8547439", "8547439",
"1234567", "1234567",
}, },
TemplateBody: model.NotifTelegramDefaultTemplateBody, TemplateBody: model.NotifTelegramDefaultTemplateBody,
DisableNotification: utl.NewFalse(),
}, },
}, },
Providers: &model.Providers{ Providers: &model.Providers{

View File

@@ -1,15 +1,18 @@
package model package model
import "github.com/crazy-max/diun/v4/pkg/utl"
// NotifTelegramDefaultTemplateBody ... // 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).` 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 // NotifTelegram holds Telegram notification configuration details
type NotifTelegram struct { type NotifTelegram struct {
Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"` Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"`
TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"` TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"`
ChatIDs []string `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"` ChatIDs []string `yaml:"chatIDs,omitempty" json:"chatIDs,omitempty" validate:"omitempty"`
ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"` ChatIDsFile string `yaml:"chatIDsFile,omitempty" json:"chatIDsFile,omitempty" validate:"omitempty,file"`
TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"` 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 // GetDefaults gets the default values
@@ -22,4 +25,5 @@ func (s *NotifTelegram) GetDefaults() *NotifTelegram {
// SetDefaults sets the default values // SetDefaults sets the default values
func (s *NotifTelegram) SetDefaults() { func (s *NotifTelegram) SetDefaults() {
s.TemplateBody = NotifTelegramDefaultTemplateBody s.TemplateBody = NotifTelegramDefaultTemplateBody
s.DisableNotification = utl.NewFalse()
} }

View File

@@ -107,12 +107,12 @@ func (c *Client) Send(entry model.NotifEntry) error {
for _, cid := range parsedChatIDs { for _, cid := range parsedChatIDs {
if len(cid.topics) > 0 { if len(cid.topics) > 0 {
for _, topic := range cid.topics { 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 return err
} }
} }
} else { } 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 return err
} }
} }
@@ -151,11 +151,12 @@ func parseChatIDs(entries []string) ([]chatID, error) {
return chatIDs, nil 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{ _, err := bot.SendMessage(chatID, message, &gotgbot.SendMessageOpts{
MessageThreadId: threadID, MessageThreadId: threadID,
ParseMode: gotgbot.ParseModeMarkdown, ParseMode: gotgbot.ParseModeMarkdown,
LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true}, LinkPreviewOptions: &gotgbot.LinkPreviewOptions{IsDisabled: true},
DisableNotification: disableNotification,
}) })
return err return err
} }