forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushover.go
66 lines (53 loc) · 1.33 KB
/
pushover.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
package push
import (
"errors"
"strings"
"github.com/evcc-io/evcc/util"
"github.com/gregdel/pushover"
)
func init() {
registry.Add("pushover", NewPushOverFromConfig)
}
// PushOver implements the pushover messenger
type PushOver struct {
log *util.Logger
app *pushover.Pushover
device string
recipients []string
}
// NewPushOverFromConfig creates new pushover messenger
func NewPushOverFromConfig(other map[string]interface{}) (Messenger, error) {
var cc struct {
App string
Recipients []string
Devices []string
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.App == "" {
return nil, errors.New("missing app name")
}
m := &PushOver{
log: util.NewLogger("pushover").Redact(cc.App),
app: pushover.New(cc.App),
device: strings.Join(cc.Devices, ","),
recipients: cc.Recipients,
}
m.log.Redact(cc.Recipients...)
return m, nil
}
// Send sends to all receivers
func (m *PushOver) Send(title, msg string) {
message := pushover.NewMessageWithTitle(msg, title)
message.DeviceName = m.device
for _, id := range m.recipients {
go func(id string) {
m.log.DEBUG.Printf("sending to %s", id)
recipient := pushover.NewRecipient(id)
if _, err := m.app.SendMessage(message, recipient); err != nil {
m.log.ERROR.Print(err)
}
}(id)
}
}