forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntfy.go
69 lines (57 loc) · 1.27 KB
/
ntfy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package push
import (
"errors"
"net/http"
"strings"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
func init() {
registry.Add("ntfy", NewNtfyFromConfig)
}
// Ntfy implements the ntfy messaging aggregator
type Ntfy struct {
log *util.Logger
uri string
priority string
tags string
}
// NewNtfyFromConfig creates new Ntfy messenger
func NewNtfyFromConfig(other map[string]interface{}) (Messenger, error) {
var cc struct {
URI string
Priority string
Tags string
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.URI == "" {
return nil, errors.New("missing uri")
}
log := util.NewLogger("ntfy")
if token, ok := strings.CutPrefix(cc.URI, "https://ntfy.sh/"); ok {
log = log.Redact(token)
}
m := &Ntfy{
log: log,
uri: cc.URI,
priority: cc.Priority,
tags: cc.Tags,
}
return m, nil
}
// Send sends to all receivers
func (m *Ntfy) Send(title, msg string) {
req, err := request.New("POST", m.uri, strings.NewReader(msg), map[string]string{
"Priority": m.priority,
"Title": title,
"Tags": m.tags,
})
if err != nil {
m.log.ERROR.Printf("ntfy: %v", err)
}
if _, err := http.DefaultClient.Do(req); err != nil {
m.log.ERROR.Printf("ntfy: %v", err)
}
}