-
Notifications
You must be signed in to change notification settings - Fork 218
/
sms.go
61 lines (51 loc) · 2 KB
/
sms.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
package pushbullet
import (
"github.com/cschomburg/go-pushbullet"
"github.com/pkg/errors"
)
// SMS struct holds necessary data to communicate with the Pushbullet SMS API.
type SMS struct {
client *pushbullet.Client
deviceIdentifier string
phoneNumbers []string
}
// NewSMS returns a new instance of a SMS notification service
// tied to an SMS capable device. deviceNickname is the
// Pushbullet nickname of the sms capable device from which messages are sent.
// (https://help.pushbullet.com/articles/how-do-i-send-text-messages-from-my-computer/).
// For more information about Pushbullet api token:
// -> https://docs.pushbullet.com/#api-overview
func NewSMS(apiToken string, deviceNickname string) (*SMS, error) {
client := pushbullet.New(apiToken)
dev, err := client.Device(deviceNickname)
if err != nil {
return nil, errors.Wrapf(err, "failed to locate Pushbullet device with nickname '%s'", deviceNickname)
}
sms := &SMS{
client: client,
deviceIdentifier: dev.Iden,
phoneNumbers: []string{},
}
return sms, nil
}
// AddReceivers takes phone numbers and adds them to the internal phoneNumbers list. The Send method will send
// a given message to all registered phone numbers.
func (sms *SMS) AddReceivers(phoneNumbers ...string) {
sms.phoneNumbers = append(sms.phoneNumbers, phoneNumbers...)
}
// Send takes a message subject and a message body and sends them to all phone numbers.
// see https://help.pushbullet.com/articles/how-do-i-send-text-messages-from-my-computer/
func (sms SMS) Send(subject, message string) error {
fullMessage := subject + "\n" + message // Treating subject as message title
user, err := sms.client.Me()
if err != nil {
return errors.Wrapf(err, "failed to find valid pushbullet user")
}
for _, phoneNumber := range sms.phoneNumbers {
err = sms.client.PushSMS(user.Iden, sms.deviceIdentifier, phoneNumber, fullMessage)
if err != nil {
return errors.Wrapf(err, "failed to send SMS message to %s via Pushbullet", phoneNumber)
}
}
return nil
}