mirror of
https://github.com/crazy-max/diun.git
synced 2025-12-21 13:23:09 +01:00
Add disable_notification support for telegram.
This commit is contained in:
@@ -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",
|
||||
@@ -351,7 +352,8 @@ func TestLoadEnv(t *testing.T) {
|
||||
"8547439",
|
||||
"1234567",
|
||||
},
|
||||
TemplateBody: model.NotifTelegramDefaultTemplateBody,
|
||||
TemplateBody: model.NotifTelegramDefaultTemplateBody,
|
||||
DisableNotification: utl.NewFalse(),
|
||||
},
|
||||
},
|
||||
Providers: &model.Providers{
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user