-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support signal rest api notifications (#650)
- Loading branch information
Showing
8 changed files
with
160 additions
and
13 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,41 @@ | ||
# Signal-REST notifications | ||
|
||
The notification uses the [Signal REST API](https://github.com/bbernhard/signal-cli-rest-api). | ||
|
||
You can send Signal notifications via the Signal REST API with the following settings. | ||
|
||
## Configuration | ||
|
||
!!! example "File" | ||
```yaml | ||
notif: | ||
signalrest: | ||
endpoint: http://192.168.42.50:8080/v2/send | ||
number: "+00471147111337" | ||
recipients: | ||
- "+00472323111337" | ||
timeout: 10s | ||
``` | ||
|
||
| 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 | | ||
|
||
!!! abstract "Environment variables" | ||
* `DIUN_NOTIF_SIGNALREST_ENDPOINT` | ||
* `DIUN_NOTIF_SIGNALREST_NUMBER` | ||
* `DIUN_NOTIF_SIGNALREST_RECIPIENTS_<KEY>` | ||
* `DIUN_NOTIF_SIGNALREST_TIMEOUT` | ||
|
||
## 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). | ||
``` | ||
|
||
[^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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package model | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/crazy-max/diun/v4/pkg/utl" | ||
) | ||
|
||
// 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"` | ||
} | ||
|
||
// GetDefaults gets the default values | ||
func (s *NotifSignalRest) GetDefaults() *NotifSignalRest { | ||
n := &NotifSignalRest{} | ||
n.SetDefaults() | ||
return n | ||
} | ||
|
||
// SetDefaults sets the default values | ||
func (s *NotifSignalRest) SetDefaults() { | ||
s.Timeout = utl.NewDuration(10 * time.Second) | ||
s.Endpoint = "http://localhost:8080/v2/send" | ||
} |
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,69 @@ | ||
package signalrest | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/crazy-max/diun/v4/internal/model" | ||
"github.com/crazy-max/diun/v4/internal/notif/notifier" | ||
) | ||
|
||
// Client represents an active signalrest notification object | ||
type Client struct { | ||
*notifier.Notifier | ||
cfg *model.NotifSignalRest | ||
meta model.Meta | ||
} | ||
|
||
// New creates a new signalrest notification instance | ||
func New(config *model.NotifSignalRest, 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 "signalrest" | ||
} | ||
|
||
// Send creates and sends a signalrest notification with an entry | ||
func (c *Client) Send(entry model.NotifEntry) error { | ||
hc := http.Client{ | ||
Timeout: *c.cfg.Timeout, | ||
} | ||
|
||
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).", | ||
Number: c.cfg.Number, | ||
Recipients: c.cfg.Recipients, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req, err := http.NewRequest("POST", c.cfg.Endpoint, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(c.cfg.Headers) > 0 { | ||
for key, value := range c.cfg.Headers { | ||
req.Header.Add(key, value) | ||
} | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("User-Agent", c.meta.UserAgent) | ||
|
||
_, err = hc.Do(req) | ||
return err | ||
} |
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