-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpushover.go
236 lines (201 loc) · 7 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Package pushover provides a wrapper around the Pushover API
package pushover
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
)
// Regexp validation.
var tokenRegexp *regexp.Regexp
func init() {
tokenRegexp = regexp.MustCompile(`^[A-Za-z0-9]{30}$`)
}
// APIEndpoint is the API base URL for any request.
var APIEndpoint = "https://api.pushover.net/1"
// Pushover custom errors.
var (
ErrHTTPPushover = errors.New("pushover: http error")
ErrEmptyToken = errors.New("pushover: empty API token")
ErrEmptyURL = errors.New("pushover: empty URL, URLTitle needs an URL")
ErrEmptyRecipientToken = errors.New("pushover: empty recipient token")
ErrInvalidRecipientToken = errors.New("pushover: invalid recipient token")
ErrInvalidHeaders = errors.New("pushover: invalid headers in server response")
ErrInvalidPriority = errors.New("pushover: invalid priority")
ErrInvalidToken = errors.New("pushover: invalid API token")
ErrMessageEmpty = errors.New("pushover: message empty")
ErrMessageTitleTooLong = errors.New("pushover: message title too long")
ErrMessageTooLong = errors.New("pushover: message too long")
ErrMessageAttachmentTooLarge = errors.New("pushover: message attachment is too large")
ErrMessageURLTitleTooLong = errors.New("pushover: message URL title too long")
ErrMessageURLTooLong = errors.New("pushover: message URL too long")
ErrMissingAttachment = errors.New("pushover: missing attachment")
ErrMissingEmergencyParameter = errors.New("pushover: missing emergency parameter")
ErrInvalidDeviceName = errors.New("pushover: invalid device name")
ErrEmptyReceipt = errors.New("pushover: empty receipt")
ErrGlancesMissingData = errors.New("pushover: glance update data missing")
ErrGlancesTitleTooLong = errors.New("pushover: glance title too long")
ErrGlancesTextTooLong = errors.New("pushover: glance text too long")
ErrGlancesSubtextTooLong = errors.New("pushover: glance subtext too long")
ErrGlancesInvalidPercent = errors.New("pushover: glance percent must be in range of 0-100")
)
// API limitations.
const (
// MessageMaxLength is the max message number of characters.
MessageMaxLength = 1024
// MessageTitleMaxLength is the max title number of characters.
MessageTitleMaxLength = 250
// MessageURLMaxLength is the max URL number of characters.
MessageURLMaxLength = 512
// MessageURLTitleMaxLength is the max URL title number of characters.
MessageURLTitleMaxLength = 100
// MessageMaxAttachmentByte is the max attachment size in byte.
MessageMaxAttachmentByte = 2621440
)
// Message priorities
const (
PriorityLowest = -2
PriorityLow = -1
PriorityNormal = 0
PriorityHigh = 1
PriorityEmergency = 2
)
// Sounds
const (
SoundPushover = "pushover"
SoundBike = "bike"
SoundBugle = "bugle"
SoundCashRegister = "cashregister"
SoundClassical = "classical"
SoundCosmic = "cosmic"
SoundFalling = "falling"
SoundGamelan = "gamelan"
SoundIncoming = "incoming"
SoundIntermission = "intermission"
SoundMagic = "magic"
SoundMechanical = "mechanical"
SoundPianobar = "pianobar"
SoundSiren = "siren"
SoundSpaceAlarm = "spacealarm"
SoundTugBoat = "tugboat"
SoundAlien = "alien"
SoundClimb = "climb"
SoundPersistent = "persistent"
SoundEcho = "echo"
SoundUpDown = "updown"
SoundVibrate = "vibrate"
SoundNone = "none"
)
// Pushover is the representation of an app using the pushover API.
type Pushover struct {
token string
}
// New returns a new app to talk to the pushover API.
func New(token string) *Pushover {
return &Pushover{token}
}
// Validate Pushover token.
func (p *Pushover) validate() error {
// Check empty token
if p.token == "" {
return ErrEmptyToken
}
// Check invalid token
if !tokenRegexp.MatchString(p.token) {
return ErrInvalidToken
}
return nil
}
// SendMessage is used to send message to a recipient.
func (p *Pushover) SendMessage(message *Message, recipient *Recipient) (*Response, error) {
// Validate pushover
if err := p.validate(); err != nil {
return nil, err
}
// Validate recipient
if err := recipient.validate(); err != nil {
return nil, err
}
// Validate message
if err := message.validate(); err != nil {
return nil, err
}
return message.send(p.token, recipient.token)
}
// SendGlanceUpdate is used to send glance updates to a recipient.
// It can be used to display widgets on a smart watch
func (p *Pushover) SendGlanceUpdate(msg *Glance, rec *Recipient) (*Response, error) {
// Validate pushover
if err := p.validate(); err != nil {
return nil, err
}
// Validate rec
if err := rec.validate(); err != nil {
return nil, err
}
// Validate msg
if err := msg.validate(); err != nil {
return nil, err
}
return msg.send(p.token, rec.token)
}
// GetReceiptDetails return detailed information about a receipt. This is used
// used to check the acknowledged status of an Emergency notification.
func (p *Pushover) GetReceiptDetails(receipt string) (*ReceiptDetails, error) {
url := fmt.Sprintf("%s/receipts/%s.json?token=%s", APIEndpoint, receipt, p.token)
if receipt == "" {
return nil, ErrEmptyReceipt
}
// Send request
resp, err := http.Get(url)
if err != nil {
return nil, err
}
// Decode the JSON response
var details *ReceiptDetails
if err = json.NewDecoder(resp.Body).Decode(&details); err != nil {
return nil, err
}
return details, nil
}
// GetRecipientDetails allows to check if a recipient exists, if it's a group
// and the devices associated to this recipient. The Errors field of the
// RecipientDetails object will contain an error if the recipient is not valid
// in the Pushover API.
func (p *Pushover) GetRecipientDetails(recipient *Recipient) (*RecipientDetails, error) {
endpoint := fmt.Sprintf("%s/users/validate.json", APIEndpoint)
// Validate pushover
if err := p.validate(); err != nil {
return nil, err
}
// Validate recipient
if err := recipient.validate(); err != nil {
return nil, err
}
req, err := newURLEncodedRequest("POST", endpoint,
map[string]string{"token": p.token, "user": recipient.token})
if err != nil {
return nil, err
}
var response RecipientDetails
if err := do(req, &response, false); err != nil {
return nil, err
}
return &response, nil
}
// CancelEmergencyNotification helps stop a notification retry in case of a
// notification with an Emergency priority before reaching the expiration time.
// It requires the response receipt in order to stop the right notification.
func (p *Pushover) CancelEmergencyNotification(receipt string) (*Response, error) {
endpoint := fmt.Sprintf("%s/receipts/%s/cancel.json", APIEndpoint, receipt)
req, err := newURLEncodedRequest("POST", endpoint, map[string]string{"token": p.token})
if err != nil {
return nil, err
}
response := &Response{}
if err := do(req, response, false); err != nil {
return nil, err
}
return response, nil
}