Skip to content

Commit 42e684d

Browse files
author
Jonas Weismueller
authored
feat: support signal rest api notifications (#650)
1 parent 34316bc commit 42e684d

File tree

8 files changed

+160
-13
lines changed

8 files changed

+160
-13
lines changed

docs/config/index.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ You can override this using the [`--config` flag or `CONFIG` env var with `serve
2828
```yaml
2929
db:
3030
path: diun.db
31-
31+
3232
watch:
3333
workers: 10
3434
schedule: "0 */6 * * *"
3535
firstCheckNotif: false
36-
36+
3737
notif:
3838
amqp:
3939
host: localhost
@@ -82,7 +82,7 @@ You can override this using the [`--config` flag or `CONFIG` env var with `serve
8282
content-type: application/json
8383
authorization: Token123456
8484
timeout: 10s
85-
85+
8686
regopts:
8787
- name: "myregistry"
8888
username: foo
@@ -93,7 +93,7 @@ You can override this using the [`--config` flag or `CONFIG` env var with `serve
9393
selector: image
9494
username: foo2
9595
password: bar2
96-
96+
9797
providers:
9898
docker:
9999
watchStopped: true
@@ -115,12 +115,12 @@ All configuration from file can be transposed into environment variables. As an
115115
```yaml
116116
db:
117117
path: diun.db
118-
118+
119119
watch:
120120
workers: 10
121121
schedule: "0 */6 * * *"
122122
firstCheckNotif: false
123-
123+
124124
notif:
125125
gotify:
126126
endpoint: http://gotify.foo.com
@@ -139,7 +139,7 @@ All configuration from file can be transposed into environment variables. As an
139139
content-type: application/json
140140
authorization: Token123456
141141
timeout: 10s
142-
142+
143143
regopts:
144144
- name: "docker.io"
145145
selector: image
@@ -164,25 +164,25 @@ Can be transposed to:
164164
??? example "environment variables"
165165
```
166166
DIUN_DB_PATH=diun.db
167-
167+
168168
DIUN_WATCH_WORKERS=10
169169
DIUN_WATCH_SCHEDULE=0 */6 * * *
170170
DIUN_WATCH_FIRSTCHECKNOTIF=false
171-
171+
172172
DIUN_NOTIF_GOTIFY_ENDPOINT=http://gotify.foo.com
173173
DIUN_NOTIF_GOTIFY_TOKEN=Token123456
174174
DIUN_NOTIF_GOTIFY_PRIORITY=1
175175
DIUN_NOTIF_GOTIFY_TIMEOUT=10s
176-
176+
177177
DIUN_NOTIF_TELEGRAM_TOKEN=aabbccdd:11223344
178178
DIUN_NOTIF_TELEGRAM_CHATIDS=123456789,987654321
179-
179+
180180
DIUN_NOTIF_WEBHOOK_ENDPOINT=http://webhook.foo.com/sd54qad89azd5a
181181
DIUN_NOTIF_WEBHOOK_METHOD=GET
182182
DIUN_NOTIF_WEBHOOK_HEADERS_CONTENT-TYPE=application/json
183183
DIUN_NOTIF_WEBHOOK_HEADERS_AUTHORIZATION=Token123456
184184
DIUN_NOTIF_WEBHOOK_TIMEOUT=10s
185-
185+
186186
DIUN_REGOPTS_0_NAME=docker.io
187187
DIUN_REGOPTS_0_SELECTOR=image
188188
DIUN_REGOPTS_0_USERNAME=foo
@@ -192,7 +192,7 @@ Can be transposed to:
192192
DIUN_REGOPTS_1_USERNAME=fii
193193
DIUN_REGOPTS_1_PASSWORD=bor
194194
DIUN_REGOPTS_1_TIMEOUT=20s
195-
195+
196196
PROVIDERS_KUBERNETES_TLSINSECURE=false
197197
PROVIDERS_KUBERNETES_NAMESPACES=default,production
198198
```
@@ -212,6 +212,7 @@ Can be transposed to:
212212
* [rocketchat](../notif/rocketchat.md)
213213
* [script](../notif/script.md)
214214
* [slack](../notif/slack.md)
215+
* [signal-rest](../notif/signalrest.md)
215216
* [teams](../notif/teams.md)
216217
* [telegram](../notif/telegram.md)
217218
* [webhook](../notif/webhook.md)

docs/config/notif.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [`pushover`](../notif/pushover.md)
1010
* [`rocketchat`](../notif/rocketchat.md)
1111
* [`script`](../notif/script.md)
12+
* [`signal-rest`](../notif/signalrest.md)
1213
* [`slack`](../notif/slack.md)
1314
* [`teams`](../notif/teams.md)
1415
* [`telegram`](../notif/telegram.md)

docs/notif/signalrest.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Signal-REST notifications
2+
3+
The notification uses the [Signal REST API](https://github.com/bbernhard/signal-cli-rest-api).
4+
5+
You can send Signal notifications via the Signal REST API with the following settings.
6+
7+
## Configuration
8+
9+
!!! example "File"
10+
```yaml
11+
notif:
12+
signalrest:
13+
endpoint: http://192.168.42.50:8080/v2/send
14+
number: "+00471147111337"
15+
recipients:
16+
- "+00472323111337"
17+
timeout: 10s
18+
```
19+
20+
| Name | Default | Description |
21+
|--------------------|---------------------------------|-----------------------------------------------------------|
22+
| `endpoint` | `http://localhost:8080/v2/send` | URL of the Signal REST API endpoint |
23+
| `number`[^1] | | The senders number you registered |
24+
| `recipients`[^1] | | A list of recipients, either phone numbers or group ID's |
25+
| `timeout` | `10s` | Timeout specifies a time limit for the request to be made |
26+
27+
!!! abstract "Environment variables"
28+
* `DIUN_NOTIF_SIGNALREST_ENDPOINT`
29+
* `DIUN_NOTIF_SIGNALREST_NUMBER`
30+
* `DIUN_NOTIF_SIGNALREST_RECIPIENTS_<KEY>`
31+
* `DIUN_NOTIF_SIGNALREST_TIMEOUT`
32+
33+
## Sample
34+
35+
The message you receive in your Signal App will look like this:
36+
37+
```text
38+
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).
39+
```
40+
41+
[^1]: Value required

internal/model/notif.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type Notif struct {
4040
Pushover *NotifPushover `yaml:"pushover,omitempty" json:"pushover,omitempty"`
4141
RocketChat *NotifRocketChat `yaml:"rocketchat,omitempty" json:"rocketchat,omitempty"`
4242
Script *NotifScript `yaml:"script,omitempty" json:"script,omitempty"`
43+
SignalRest *NotifSignalRest `yaml:"signalrest,omitempty" json:"signalrest,omitempty"`
4344
Slack *NotifSlack `yaml:"slack,omitempty" json:"slack,omitempty"`
4445
Teams *NotifTeams `yaml:"teams,omitempty" json:"teams,omitempty"`
4546
Telegram *NotifTelegram `yaml:"telegram,omitempty" json:"telegram,omitempty"`

internal/model/notif_signalrest.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package model
2+
3+
import (
4+
"time"
5+
6+
"github.com/crazy-max/diun/v4/pkg/utl"
7+
)
8+
9+
// NotifSignalRest holds SignalRest notification configuration details
10+
type NotifSignalRest struct {
11+
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty" validate:"required"`
12+
Number string `yaml:"number,omitempty" json:"method,omitempty" validate:"required"`
13+
Recipients []string `yaml:"recipients,omitempty" json:"recipients,omitempty" validate:"omitempty"`
14+
Headers map[string]string `yaml:"headers,omitempty" json:"headers,omitempty" validate:"omitempty"`
15+
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout,omitempty" validate:"required"`
16+
}
17+
18+
// GetDefaults gets the default values
19+
func (s *NotifSignalRest) GetDefaults() *NotifSignalRest {
20+
n := &NotifSignalRest{}
21+
n.SetDefaults()
22+
return n
23+
}
24+
25+
// SetDefaults sets the default values
26+
func (s *NotifSignalRest) SetDefaults() {
27+
s.Timeout = utl.NewDuration(10 * time.Second)
28+
s.Endpoint = "http://localhost:8080/v2/send"
29+
}

internal/notif/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/crazy-max/diun/v4/internal/notif/pushover"
1515
"github.com/crazy-max/diun/v4/internal/notif/rocketchat"
1616
"github.com/crazy-max/diun/v4/internal/notif/script"
17+
"github.com/crazy-max/diun/v4/internal/notif/signalrest"
1718
"github.com/crazy-max/diun/v4/internal/notif/slack"
1819
"github.com/crazy-max/diun/v4/internal/notif/teams"
1920
"github.com/crazy-max/diun/v4/internal/notif/telegram"
@@ -69,6 +70,9 @@ func New(config *model.Notif, meta model.Meta) (*Client, error) {
6970
if config.Script != nil {
7071
c.notifiers = append(c.notifiers, script.New(config.Script, meta))
7172
}
73+
if config.SignalRest != nil {
74+
c.notifiers = append(c.notifiers, signalrest.New(config.SignalRest, meta))
75+
}
7276
if config.Slack != nil {
7377
c.notifiers = append(c.notifiers, slack.New(config.Slack, meta))
7478
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package signalrest
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
8+
"github.com/crazy-max/diun/v4/internal/model"
9+
"github.com/crazy-max/diun/v4/internal/notif/notifier"
10+
)
11+
12+
// Client represents an active signalrest notification object
13+
type Client struct {
14+
*notifier.Notifier
15+
cfg *model.NotifSignalRest
16+
meta model.Meta
17+
}
18+
19+
// New creates a new signalrest notification instance
20+
func New(config *model.NotifSignalRest, meta model.Meta) notifier.Notifier {
21+
return notifier.Notifier{
22+
Handler: &Client{
23+
cfg: config,
24+
meta: meta,
25+
},
26+
}
27+
}
28+
29+
// Name returns notifier's name
30+
func (c *Client) Name() string {
31+
return "signalrest"
32+
}
33+
34+
// Send creates and sends a signalrest notification with an entry
35+
func (c *Client) Send(entry model.NotifEntry) error {
36+
hc := http.Client{
37+
Timeout: *c.cfg.Timeout,
38+
}
39+
40+
body, err := json.Marshal(struct {
41+
Message string `json:"message"`
42+
Number string `json:"number"`
43+
Recipients []string `json:"recipients"`
44+
}{
45+
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).",
46+
Number: c.cfg.Number,
47+
Recipients: c.cfg.Recipients,
48+
})
49+
if err != nil {
50+
return err
51+
}
52+
53+
req, err := http.NewRequest("POST", c.cfg.Endpoint, bytes.NewBuffer(body))
54+
if err != nil {
55+
return err
56+
}
57+
58+
if len(c.cfg.Headers) > 0 {
59+
for key, value := range c.cfg.Headers {
60+
req.Header.Add(key, value)
61+
}
62+
}
63+
64+
req.Header.Set("Content-Type", "application/json")
65+
req.Header.Set("User-Agent", c.meta.UserAgent)
66+
67+
_, err = hc.Do(req)
68+
return err
69+
}

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ nav:
125125
- Pushover: notif/pushover.md
126126
- Rocket.Chat: notif/rocketchat.md
127127
- Script: notif/script.md
128+
- Signal (REST API): notif/signalrest.md
128129
- Slack: notif/slack.md
129130
- Teams: notif/teams.md
130131
- Telegram: notif/telegram.md

0 commit comments

Comments
 (0)