From d7909f0156f452b596db917b5b8b586da6f9a41f Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 29 Dec 2022 07:47:38 +0100 Subject: [PATCH] Allow customizing Signal notification message --- docs/notif/signalrest.md | 23 ++++++++++++++++------- internal/model/notif_signalrest.go | 15 ++++++++++----- internal/notif/signalrest/client.go | 17 ++++++++++++++++- 3 files changed, 42 insertions(+), 13 deletions(-) diff --git a/docs/notif/signalrest.md b/docs/notif/signalrest.md index cab7a950..db67bbdd 100644 --- a/docs/notif/signalrest.md +++ b/docs/notif/signalrest.md @@ -15,14 +15,17 @@ You can send Signal notifications via the Signal REST API with the following set recipients: - "+00472323111337" timeout: 10s + templateBody: | + Docker tag {{ .Entry.Image }} which you subscribed to through {{ .Entry.Provider }} provider has been released. ``` -| Name | Default | Description | -|--------------------|---------------------------------|-----------------------------------------------------------| -| `endpoint` | `http://localhost:8080/v2/send` | URL of the Signal REST API endpoint | -| `number`[^1] | | The senders number you registered | -| `recipients`[^1] | | A list of recipients, either phone numbers or group ID's | -| `timeout` | `10s` | Timeout specifies a time limit for the request to be made | +| Name | Default | Description | +|--------------------|------------------------------------|---------------------------------------------------------------------------| +| `endpoint` | `http://localhost:8080/v2/send` | URL of the Signal REST API endpoint | +| `number`[^1] | | The senders number you registered | +| `recipients`[^1] | | A list of recipients, either phone numbers or group ID's | +| `timeout` | `10s` | Timeout specifies a time limit for the request to be made | +| `templateBody`[^1] | See [below](#default-templatebody) | [Notification template](../faq.md#notification-template) for message body | !!! abstract "Environment variables" * `DIUN_NOTIF_SIGNALREST_ENDPOINT` @@ -30,12 +33,18 @@ You can send Signal notifications via the Signal REST API with the following set * `DIUN_NOTIF_SIGNALREST_RECIPIENTS_` * `DIUN_NOTIF_SIGNALREST_TIMEOUT` +### Default `templateBody` + +``` +Docker tag {{ .Entry.Image }} 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 {{ .Meta.Hostname }} host). +``` + ## Sample The message you receive in your Signal App will look like this: ```text -Docker tag docker.io/diun/testnotif:latest which you subscribed to through file provider new has been updated on docker.io registry (triggered by5bfaae601770 host). +Docker tag docker.io/diun/testnotif:latest which you subscribed to through file provider new has been updated on docker.io registry (triggered by 5bfaae601770 host). ``` [^1]: Value required diff --git a/internal/model/notif_signalrest.go b/internal/model/notif_signalrest.go index e8f9fd20..c7ac5f85 100644 --- a/internal/model/notif_signalrest.go +++ b/internal/model/notif_signalrest.go @@ -6,13 +6,17 @@ import ( "github.com/crazy-max/diun/v4/pkg/utl" ) +// NotifSignalRestDefaultTemplateBody ... +const NotifSignalRestDefaultTemplateBody = `Docker tag {{ .Entry.Image }} 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 {{ .Meta.Hostname }} host).` + // NotifSignalRest holds SignalRest notification configuration details type NotifSignalRest struct { - Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"` - Number string `yaml:"number,omitempty" json:"method,omitempty" validate:"required"` - Recipients []string `yaml:"recipients,omitempty" json:"recipients,omitempty" validate:"omitempty"` - Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty" validate:"omitempty"` - Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"` + Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"` + Number string `yaml:"number,omitempty" json:"method,omitempty" validate:"required"` + Recipients []string `yaml:"recipients,omitempty" json:"recipients,omitempty" validate:"omitempty"` + Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty" validate:"omitempty"` + Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"` + TemplateBody string `yaml:"templateBody,omitempty" json:"templateBody,omitempty" validate:"required"` } // GetDefaults gets the default values @@ -26,4 +30,5 @@ func (s *NotifSignalRest) GetDefaults() *NotifSignalRest { func (s *NotifSignalRest) SetDefaults() { s.Timeout = utl.NewDuration(10 * time.Second) s.Endpoint = "http://localhost:8080/v2/send" + s.TemplateBody = NotifSignalRestDefaultTemplateBody } diff --git a/internal/notif/signalrest/client.go b/internal/notif/signalrest/client.go index 0289b88b..405a0d41 100644 --- a/internal/notif/signalrest/client.go +++ b/internal/notif/signalrest/client.go @@ -6,6 +6,7 @@ import ( "net/http" "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" ) @@ -37,12 +38,26 @@ func (c *Client) Send(entry model.NotifEntry) error { Timeout: *c.cfg.Timeout, } + message, err := msg.New(msg.Options{ + Meta: c.meta, + Entry: entry, + TemplateBody: c.cfg.TemplateBody, + }) + if err != nil { + return err + } + + _, bodyrender, err := message.RenderMarkdown() + if err != nil { + return err + } + body, err := json.Marshal(struct { Message string `json:"message"` Number string `json:"number"` Recipients []string `json:"recipients"` }{ - Message: "Docker tag " + entry.Image.String() + " which you subscribed to through " + entry.Provider + " provider " + string(entry.Status) + " has been updated on " + entry.Image.Domain + " registry (triggered by" + c.meta.Hostname + " host).", + Message: string(bodyrender), Number: c.cfg.Number, Recipients: c.cfg.Recipients, })