Apprise notif support

This commit is contained in:
privacyfr3ak
2025-07-24 14:49:41 -04:00
committed by CrazyMax
parent fe9f478455
commit 3a03484712
8 changed files with 208 additions and 0 deletions

View File

@@ -245,6 +245,7 @@ Can be transposed to:
* [defaults](defaults.md) * [defaults](defaults.md)
* notif * notif
* [amqp](../notif/amqp.md) * [amqp](../notif/amqp.md)
* [apprise](../notif/apprise.md)
* [discord](../notif/discord.md) * [discord](../notif/discord.md)
* [gotify](../notif/gotify.md) * [gotify](../notif/gotify.md)
* [mail](../notif/mail.md) * [mail](../notif/mail.md)

View File

@@ -1,6 +1,7 @@
# Notifications configuration # Notifications configuration
* [`amqp`](../notif/amqp.md) * [`amqp`](../notif/amqp.md)
* [`apprise`](../notif/apprise.md)
* [`discord`](../notif/discord.md) * [`discord`](../notif/discord.md)
* [`gotify`](../notif/gotify.md) * [`gotify`](../notif/gotify.md)
* [`mail`](../notif/mail.md) * [`mail`](../notif/mail.md)

52
docs/notif/apprise.md Normal file
View File

@@ -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

View File

@@ -33,6 +33,7 @@ type NotifEntry struct {
// Notif holds data necessary for notification configuration // Notif holds data necessary for notification configuration
type Notif struct { type Notif struct {
Amqp *NotifAmqp `yaml:"amqp,omitempty" json:"amqp,omitempty"` Amqp *NotifAmqp `yaml:"amqp,omitempty" json:"amqp,omitempty"`
Apprise *NotifApprise `yaml:"apprise,omitempty" json:"apprise,omitempty"`
Discord *NotifDiscord `yaml:"discord,omitempty" json:"discord,omitempty"` Discord *NotifDiscord `yaml:"discord,omitempty" json:"discord,omitempty"`
Gotify *NotifGotify `yaml:"gotify,omitempty" json:"gotify,omitempty"` Gotify *NotifGotify `yaml:"gotify,omitempty" json:"gotify,omitempty"`
Mail *NotifMail `yaml:"mail,omitempty" json:"mail,omitempty"` Mail *NotifMail `yaml:"mail,omitempty" json:"mail,omitempty"`

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/crazy-max/diun/v4/internal/model" "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/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/discord"
"github.com/crazy-max/diun/v4/internal/notif/gotify" "github.com/crazy-max/diun/v4/internal/notif/gotify"
"github.com/crazy-max/diun/v4/internal/notif/mail" "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 { if config.Amqp != nil {
c.notifiers = append(c.notifiers, amqp.New(config.Amqp, meta)) 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 { if config.Discord != nil {
c.notifiers = append(c.notifiers, discord.New(config.Discord, meta)) c.notifiers = append(c.notifiers, discord.New(config.Discord, meta))
} }

View File

@@ -118,6 +118,7 @@ nav:
- .providers: config/providers.md - .providers: config/providers.md
- Notifications: - Notifications:
- Amqp: notif/amqp.md - Amqp: notif/amqp.md
- Apprise: notif/apprise.md
- Discord: notif/discord.md - Discord: notif/discord.md
- Gotify: notif/gotify.md - Gotify: notif/gotify.md
- Mail: notif/mail.md - Mail: notif/mail.md