-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #787 from blueberryapple/feat/adds-ntfy
feat(notifications): Adds ntfy.sh notification option
- Loading branch information
Showing
11 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Ntfy notifications | ||
|
||
Notifications can be sent using a [ntfy](https://ntfy.sh/) instance. | ||
|
||
## Configuration | ||
|
||
!!! example "File" | ||
```yaml | ||
notif: | ||
ntfy: | ||
endpoint: https://ntfy.sh | ||
topic: diun-acce65a0-b777-46f9-9a11-58c67d1579c4 | ||
priority: 3 | ||
tags: | ||
- whale | ||
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] | `https://ntfy.sh` | Ntfy base URL | | ||
| `topic` | | Ntfy topic | | ||
| `priority` | 3 | The priority of the message | | ||
| `tags` | `["package"]` | Emoji to go in your notiication | | ||
| `timeout` | `10s` | Timeout specifies a time limit for the request to be made | | ||
| `templateTitle`[^1] | See [below](#default-templatetitle) | [Notification template](../faq.md#notification-template) for message title | | ||
| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body | | ||
|
||
!!! abstract "Environment variables" | ||
* `DIUN_NOTIF_NTFY_ENDPOINT` | ||
* `DIUN_NOTIF_NTFY_TOPIC` | ||
* `DIUN_NOTIF_NTFY_PRIORITY` | ||
* `DIUN_NOTIF_NTFY_TAGS` | ||
* `DIUN_NOTIF_NTFY_TIMEOUT` | ||
* `DIUN_NOTIF_NTFY_TEMPLATETITLE` | ||
* `DIUN_NOTIF_NTFY_TEMPLATEBODY` | ||
|
||
### Default `templateTitle` | ||
|
||
``` | ||
[[ config.extra.template.notif.defaultTitle ]] | ||
``` | ||
|
||
### Default `templateBody` | ||
|
||
``` | ||
[[ config.extra.template.notif.defaultBody ]] | ||
``` | ||
|
||
[^1]: Value required |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/crazy-max/diun/v4/pkg/utl" | ||
) | ||
|
||
// NotifNtfy holds ntfy notification configuration details | ||
type NotifNtfy struct { | ||
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"` | ||
Topic string `yaml:"topic,omitempty" json:"topic,omitempty" validate:"required"` | ||
Priority int `yaml:"priority,omitempty" json:"priority,omitempty" validate:"omitempty,min=0"` | ||
Tags []string `yaml:"tags,omitempty" json:"tags,omitempty" validate:"required"` | ||
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 *NotifNtfy) GetDefaults() *NotifNtfy { | ||
n := &NotifNtfy{} | ||
n.SetDefaults() | ||
return n | ||
} | ||
|
||
// SetDefaults sets the default values | ||
func (s *NotifNtfy) SetDefaults() { | ||
s.Endpoint = "https://ntfy.sh" | ||
s.Priority = 3 | ||
s.Tags = []string{"package"} | ||
s.Timeout = utl.NewDuration(10 * time.Second) | ||
s.TemplateTitle = NotifDefaultTemplateTitle | ||
s.TemplateBody = NotifDefaultTemplateBody | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package ntfy | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"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" | ||
) | ||
|
||
// Client represents an active ntfy notification object | ||
type Client struct { | ||
*notifier.Notifier | ||
cfg *model.NotifNtfy | ||
meta model.Meta | ||
} | ||
|
||
// New creates a new ntfy notification instance | ||
func New(config *model.NotifNtfy, 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 "ntfy" | ||
} | ||
|
||
// Send creates and sends a ntfy notification with an entry | ||
func (c *Client) Send(entry model.NotifEntry) error { | ||
hc := http.Client{ | ||
Timeout: *c.cfg.Timeout, | ||
} | ||
|
||
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 { | ||
Topic string `json:"topic"` | ||
Message string `json:"message"` | ||
Title string `json:"title"` | ||
Priority int `json:"priority"` | ||
Tags []string `json:"tags"` | ||
}{ | ||
Topic: c.cfg.Topic, | ||
Message: string(body), | ||
Title: string(title), | ||
Priority: c.cfg.Priority, | ||
Tags: c.cfg.Tags, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
u, err := url.Parse(c.cfg.Endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
q := u.Query() | ||
u.RawQuery = q.Encode() | ||
|
||
req, err := http.NewRequest("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 | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
var errBody struct { | ||
Error string `json:"error"` | ||
ErrorCode int `json:"errorCode"` | ||
ErrorDescription string `json:"errorDescription"` | ||
} | ||
err := json.NewDecoder(resp.Body).Decode(&errBody) | ||
if err != nil { | ||
return err | ||
} | ||
return fmt.Errorf("%d %s: %s", errBody.ErrorCode, errBody.Error, errBody.ErrorDescription) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters