forked from nikoksr/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpushbullet.go
60 lines (50 loc) · 1.68 KB
/
pushbullet.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
package pushbullet
import (
"context"
"github.com/cschomburg/go-pushbullet"
"github.com/pkg/errors"
)
// Pushbullet struct holds necessary data to communicate with the Pushbullet API.
type Pushbullet struct {
client *pushbullet.Client
deviceNicknames []string
}
// New returns a new instance of a Pushbullet notification service.
// For more information about Pushbullet api token:
//
// -> https://docs.pushbullet.com/#api-overview
func New(apiToken string) *Pushbullet {
client := pushbullet.New(apiToken)
pb := &Pushbullet{
client: client,
deviceNicknames: []string{},
}
return pb
}
// AddReceivers takes Pushbullet device nicknames and adds them to the internal deviceNicknames list.
// The Send method will send a given message to all those devices.
func (pb *Pushbullet) AddReceivers(deviceNicknames ...string) {
pb.deviceNicknames = append(pb.deviceNicknames, deviceNicknames...)
}
// Send takes a message subject and a message body and sends them to all valid devices.
// you will need Pushbullet installed on the relevant devices
// (android, chrome, firefox, windows)
// see https://www.pushbullet.com/apps
func (pb Pushbullet) Send(ctx context.Context, subject, message string) error {
for _, deviceNickname := range pb.deviceNicknames {
select {
case <-ctx.Done():
return ctx.Err()
default:
dev, err := pb.client.Device(deviceNickname)
if err != nil {
return errors.Wrapf(err, "failed to find Pushbullet device with nickname '%s'", deviceNickname)
}
err = dev.PushNote(subject, message)
if err != nil {
return errors.Wrapf(err, "failed to send message to Pushbullet device with nickname '%s'", deviceNickname)
}
}
}
return nil
}