0
0
Fork 0
mirror of https://github.com/crazy-max/diun.git synced 2025-03-16 04:13:30 +00:00

Add disable_notification support for telegram.

This commit is contained in:
imrebuild 2025-02-01 02:01:57 +08:00
parent 473ff9c9ba
commit c9952cb159
3 changed files with 27 additions and 18 deletions
docs/notif
internal
model
notif/telegram

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.
```
| 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` | | 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"]`

View file

@ -5,11 +5,12 @@ const NotifTelegramDefaultTemplateBody = `Docker tag {{ if .Entry.Image.HubLink
// 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

View file

@ -104,15 +104,20 @@ func (c *Client) Send(entry model.NotifEntry) error {
return err
}
disableNotification := false
if c.cfg.DisableNotification != nil {
disableNotification = *c.cfg.DisableNotification
}
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), 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), disableNotification); err != nil {
return err
}
}
@ -151,11 +156,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
}