From 3a0348471239e4f6fec628e608c1f3fca7e6eb79 Mon Sep 17 00:00:00 2001 From: privacyfr3ak <220089342+privacyfr3ak@users.noreply.github.com> Date: Thu, 24 Jul 2025 14:49:41 -0400 Subject: [PATCH 1/3] Apprise notif support --- docs/config/index.md | 1 + docs/config/notif.md | 1 + docs/notif/apprise.md | 52 ++++++++++++++ internal/model/notif.go | 1 + internal/model/notif_apprise.go | 32 +++++++++ internal/notif/apprise/client.go | 116 +++++++++++++++++++++++++++++++ internal/notif/client.go | 4 ++ mkdocs.yml | 1 + 8 files changed, 208 insertions(+) create mode 100644 docs/notif/apprise.md create mode 100644 internal/model/notif_apprise.go create mode 100644 internal/notif/apprise/client.go diff --git a/docs/config/index.md b/docs/config/index.md index 90f82338..47e8cbc3 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -245,6 +245,7 @@ Can be transposed to: * [defaults](defaults.md) * notif * [amqp](../notif/amqp.md) + * [apprise](../notif/apprise.md) * [discord](../notif/discord.md) * [gotify](../notif/gotify.md) * [mail](../notif/mail.md) diff --git a/docs/config/notif.md b/docs/config/notif.md index 7705b155..ddfeac5a 100644 --- a/docs/config/notif.md +++ b/docs/config/notif.md @@ -1,6 +1,7 @@ # Notifications configuration * [`amqp`](../notif/amqp.md) +* [`apprise`](../notif/apprise.md) * [`discord`](../notif/discord.md) * [`gotify`](../notif/gotify.md) * [`mail`](../notif/mail.md) diff --git a/docs/notif/apprise.md b/docs/notif/apprise.md new file mode 100644 index 00000000..51e8a0a1 --- /dev/null +++ b/docs/notif/apprise.md @@ -0,0 +1,52 @@ +# Apprise notifications + +Notifications can be sent using an apprise api instance. + +## Configuration + +!!! example "File" + ```yaml + notif: + apprise: + endpoint: http://apprise:8000 + tags: + - diun + timeout: 10s + templateTitle: "{{ .Entry.Image }} released" + templateBody: | + Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released. + ``` + +| Name | Default | Description | +|---------------------|-------------------------------------|------------------------------------------------------------------------------| +| `endpoint`[^1] | | Hostname and port of your apprise api instance | +| `token`[^2] | | token representing your config file (Config Key) | +| `tags` | | List of Tags in your config file you want to notify | +| `urls`[^2] | | List of [URLs](https://github.com/caronc/apprise/wiki/URLBasics) to notify | +| `timeout` | `10s` | Timeout specifies a time limit for the request to be made | +| `templateTitle` | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title | +| `templateBody` | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body | + +!!! abstract "Environment variables" + * `DIUN_NOTIF_APPRISE_ENDPOINT` + * `DIUN_NOTIF_APPRISE_TOKEN` + * `DIUN_NOTIF_APPRISE_TAGS` + * `DIUN_NOTIF_APPRISE_URLS` + * `DIUN_NOTIF_APPRISE_TIMEOUT` + * `DIUN_NOTIF_APPRISE_TEMPLATETITLE` + * `DIUN_NOTIF_APPRISE_TEMPLATEBODY` + +### Default `templateTitle` + +``` +[[ config.extra.template.notif.defaultTitle ]] +``` + +### Default `templateBody` + +``` +[[ config.extra.template.notif.defaultBody ]] +``` + +[^1]: Value required +[^2]: One of these 2 values is required \ No newline at end of file diff --git a/internal/model/notif.go b/internal/model/notif.go index 37a897d7..7261a624 100644 --- a/internal/model/notif.go +++ b/internal/model/notif.go @@ -33,6 +33,7 @@ type NotifEntry struct { // Notif holds data necessary for notification configuration type Notif struct { Amqp *NotifAmqp `yaml:"amqp,omitempty" json:"amqp,omitempty"` + Apprise *NotifApprise `yaml:"apprise,omitempty" json:"apprise,omitempty"` Discord *NotifDiscord `yaml:"discord,omitempty" json:"discord,omitempty"` Gotify *NotifGotify `yaml:"gotify,omitempty" json:"gotify,omitempty"` Mail *NotifMail `yaml:"mail,omitempty" json:"mail,omitempty"` diff --git a/internal/model/notif_apprise.go b/internal/model/notif_apprise.go new file mode 100644 index 00000000..0a5437df --- /dev/null +++ b/internal/model/notif_apprise.go @@ -0,0 +1,32 @@ +package model + +import ( + "time" + + "github.com/crazy-max/diun/v4/pkg/utl" +) + +// NotifApprise holds apprise notification configuration details +type NotifApprise struct { + Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"` + Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"` + Tags []string `yaml:"tags,omitempty" json:"tags,omitempty" validate:"omitempty"` + URLs []string `yaml:"urls,omitempty" json:"urls,omitempty" validate:"omitempty"` + Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"` + TemplateTitle string `yaml:"templateTitle,omitempty" json:"templateTitle,omitempty" validate:"required"` + TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"` +} + +// GetDefaults gets the default values +func (s *NotifApprise) GetDefaults() *NotifApprise { + n := &NotifApprise{} + n.SetDefaults() + return n +} + +// SetDefaults sets the default values +func (s *NotifApprise) SetDefaults() { + s.Timeout = utl.NewDuration(10 * time.Second) + s.TemplateTitle = NotifDefaultTemplateTitle + s.TemplateBody = NotifDefaultTemplateBody +} diff --git a/internal/notif/apprise/client.go b/internal/notif/apprise/client.go new file mode 100644 index 00000000..81757195 --- /dev/null +++ b/internal/notif/apprise/client.go @@ -0,0 +1,116 @@ +package apprise + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "net/url" + "path" + + "github.com/crazy-max/diun/v4/internal/model" + "github.com/crazy-max/diun/v4/internal/msg" + "github.com/crazy-max/diun/v4/internal/notif/notifier" + "github.com/pkg/errors" +) + +// Client represents an active apprise notification object +type Client struct { + *notifier.Notifier + cfg *model.NotifApprise + meta model.Meta +} + +// New creates a new apprise notification instance +func New(config *model.NotifApprise, meta model.Meta) notifier.Notifier { + return notifier.Notifier{ + Handler: &Client{ + cfg: config, + meta: meta, + }, + } +} + +// Name returns notifier's name +func (c *Client) Name() string { + return "apprise" +} + +// Send creates and sends a apprise notification with an entry +func (c *Client) Send(entry model.NotifEntry) error { + message, err := msg.New(msg.Options{ + Meta: c.meta, + Entry: entry, + TemplateTitle: c.cfg.TemplateTitle, + TemplateBody: c.cfg.TemplateBody, + }) + if err != nil { + return err + } + + title, body, err := message.RenderMarkdown() + if err != nil { + return err + } + + dataBuf := new(bytes.Buffer) + if err := json.NewEncoder(dataBuf).Encode(struct { + Message string `json:"body"` + Title string `json:"title"` + Tags []string `json:"tags"` + URLs []string `json:"urls"` + }{ + Message: string(body), + Title: string(title), + Tags: c.cfg.Tags, + URLs: c.cfg.URLs, + }); err != nil { + return err + } + + u, err := url.Parse(c.cfg.Endpoint) + if err != nil { + return err + } + + u.Path = path.Join(u.Path, "notify") + + if c.cfg.Token != "" { + u.Path = path.Join(u.Path, c.cfg.Token) + } + + q := u.Query() + u.RawQuery = q.Encode() + + hc := http.Client{} + ctx, cancel := context.WithTimeout(context.Background(), *c.cfg.Timeout) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, "POST", u.String(), dataBuf) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", c.meta.UserAgent) + + resp, err := hc.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + var errBody struct { + Error string `json:"error"` + ErrorCode int `json:"errorCode"` + ErrorDescription string `json:"errorDescription"` + } + if err := json.NewDecoder(resp.Body).Decode(&errBody); err != nil { + return errors.Wrapf(err, "cannot decode JSON error response for HTTP %d %s status", resp.StatusCode, http.StatusText(resp.StatusCode)) + } + return errors.Errorf("%d %s: %s", errBody.ErrorCode, errBody.Error, errBody.ErrorDescription) + } + + return nil +} diff --git a/internal/notif/client.go b/internal/notif/client.go index f18622b9..fc71ed5b 100644 --- a/internal/notif/client.go +++ b/internal/notif/client.go @@ -5,6 +5,7 @@ import ( "github.com/crazy-max/diun/v4/internal/model" "github.com/crazy-max/diun/v4/internal/notif/amqp" + "github.com/crazy-max/diun/v4/internal/notif/apprise" "github.com/crazy-max/diun/v4/internal/notif/discord" "github.com/crazy-max/diun/v4/internal/notif/gotify" "github.com/crazy-max/diun/v4/internal/notif/mail" @@ -47,6 +48,9 @@ func New(config *model.Notif, meta model.Meta) (*Client, error) { if config.Amqp != nil { c.notifiers = append(c.notifiers, amqp.New(config.Amqp, meta)) } + if config.Apprise != nil { + c.notifiers = append(c.notifiers, apprise.New(config.Apprise, meta)) + } if config.Discord != nil { c.notifiers = append(c.notifiers, discord.New(config.Discord, meta)) } diff --git a/mkdocs.yml b/mkdocs.yml index 2d7cd81a..23016ad7 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -118,6 +118,7 @@ nav: - .providers: config/providers.md - Notifications: - Amqp: notif/amqp.md + - Apprise: notif/apprise.md - Discord: notif/discord.md - Gotify: notif/gotify.md - Mail: notif/mail.md From 6abe58ba6e62e0f3517b51b9a58cfd0f8e92e7d1 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sun, 3 Aug 2025 14:59:26 +0200 Subject: [PATCH 2/3] notif: support token as secret for apprise --- docs/notif/apprise.md | 22 ++++++++++++---------- internal/model/notif_apprise.go | 1 + internal/notif/apprise/client.go | 9 +++++++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/docs/notif/apprise.md b/docs/notif/apprise.md index 51e8a0a1..bbe1eb88 100644 --- a/docs/notif/apprise.md +++ b/docs/notif/apprise.md @@ -9,6 +9,7 @@ Notifications can be sent using an apprise api instance. notif: apprise: endpoint: http://apprise:8000 + token: abc tags: - diun timeout: 10s @@ -17,15 +18,16 @@ Notifications can be sent using an apprise api instance. Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released. ``` -| Name | Default | Description | -|---------------------|-------------------------------------|------------------------------------------------------------------------------| -| `endpoint`[^1] | | Hostname and port of your apprise api instance | -| `token`[^2] | | token representing your config file (Config Key) | -| `tags` | | List of Tags in your config file you want to notify | -| `urls`[^2] | | List of [URLs](https://github.com/caronc/apprise/wiki/URLBasics) to notify | -| `timeout` | `10s` | Timeout specifies a time limit for the request to be made | -| `templateTitle` | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title | -| `templateBody` | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body | +| Name | Default | Description | +|-----------------|-------------------------------------|----------------------------------------------------------------------------| +| `endpoint`[^1] | | Hostname and port of your apprise api instance | +| `token`[^2] | | token representing your config file (Config Key) | +| `tokenFile` | | Use content of secret file as application token if `token` not defined | +| `tags` | | List of Tags in your config file you want to notify | +| `urls`[^2] | | List of [URLs](https://github.com/caronc/apprise/wiki/URLBasics) to notify | +| `timeout` | `10s` | Timeout specifies a time limit for the request to be made | +| `templateTitle` | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title | +| `templateBody` | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body | !!! abstract "Environment variables" * `DIUN_NOTIF_APPRISE_ENDPOINT` @@ -49,4 +51,4 @@ Notifications can be sent using an apprise api instance. ``` [^1]: Value required -[^2]: One of these 2 values is required \ No newline at end of file +[^2]: One of these 2 values is required diff --git a/internal/model/notif_apprise.go b/internal/model/notif_apprise.go index 0a5437df..de3649f1 100644 --- a/internal/model/notif_apprise.go +++ b/internal/model/notif_apprise.go @@ -10,6 +10,7 @@ import ( type NotifApprise struct { Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"` Token string `yaml:"token,omitempty" json:"token,omitempty" validate:"omitempty"` + TokenFile string `yaml:"tokenFile,omitempty" json:"tokenFile,omitempty" validate:"omitempty,file"` Tags []string `yaml:"tags,omitempty" json:"tags,omitempty" validate:"omitempty"` URLs []string `yaml:"urls,omitempty" json:"urls,omitempty" validate:"omitempty"` Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"` diff --git a/internal/notif/apprise/client.go b/internal/notif/apprise/client.go index 81757195..0e1952dd 100644 --- a/internal/notif/apprise/client.go +++ b/internal/notif/apprise/client.go @@ -11,6 +11,7 @@ import ( "github.com/crazy-max/diun/v4/internal/model" "github.com/crazy-max/diun/v4/internal/msg" "github.com/crazy-max/diun/v4/internal/notif/notifier" + "github.com/crazy-max/diun/v4/pkg/utl" "github.com/pkg/errors" ) @@ -75,8 +76,12 @@ func (c *Client) Send(entry model.NotifEntry) error { u.Path = path.Join(u.Path, "notify") - if c.cfg.Token != "" { - u.Path = path.Join(u.Path, c.cfg.Token) + if c.cfg.Token != "" || c.cfg.TokenFile != "" { + token, err := utl.GetSecret(c.cfg.Token, c.cfg.TokenFile) + if err != nil { + return errors.Wrap(err, "cannot retrieve token secret for Apprise notifier") + } + u.Path = path.Join(u.Path, token) } q := u.Query() From edd9608db3b5a13d0e85319146e0a2eb8b4ff584 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Sun, 3 Aug 2025 16:23:55 +0200 Subject: [PATCH 3/3] notif: config test for apprise --- internal/config/config_test.go | 8 ++++++++ internal/config/fixtures/config.test.yml | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 1feedcfe..472708a6 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -73,6 +73,14 @@ func TestLoadFile(t *testing.T) { Password: "guest", Queue: "queue", }, + Apprise: &model.NotifApprise{ + Endpoint: "http://apprise:8000", + Token: "abc", + Tags: []string{"diun"}, + Timeout: utl.NewDuration(10 * time.Second), + TemplateTitle: model.NotifDefaultTemplateTitle, + TemplateBody: model.NotifDefaultTemplateBody, + }, Discord: &model.NotifDiscord{ WebhookURL: "https://discordapp.com/api/webhooks/1234567890/Abcd-eFgh-iJklmNo_pqr", Mentions: []string{ diff --git a/internal/config/fixtures/config.test.yml b/internal/config/fixtures/config.test.yml index 6d8b94ce..cf9d6592 100644 --- a/internal/config/fixtures/config.test.yml +++ b/internal/config/fixtures/config.test.yml @@ -24,6 +24,11 @@ notif: username: guest password: guest queue: queue + apprise: + endpoint: http://apprise:8000 + token: abc + tags: + - diun discord: webhookURL: https://discordapp.com/api/webhooks/1234567890/Abcd-eFgh-iJklmNo_pqr mentions: