-
Notifications
You must be signed in to change notification settings - Fork 1
/
message.go
315 lines (261 loc) · 7.56 KB
/
message.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package pushover
import (
"bytes"
"context"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
)
// MessageRequest is the data for the POST to the Pushover
// REST API. Some fields in this request should contain
// numbers but the Pushover API parameters are strings.
// There is no validation performed for these fields. If
// invalid data is submitted to the Pushover API, it will
// be rejected with an appropriate error.
//
// See the Pushover API documentation for
// more information on these parameters.
type MessageRequest struct {
// The URL for the Pushover REST API POST.
//
// Leave this empty unless you wish to override the URL.
PushoverURL string
// Required fields
// Pushover API token
Token string
// The user's token for message delivery
User string
// The message sent to the user
Message string
// Optional Fields
// Message title
Title string
// Embedded URL
URL string
// The displayed text for the URL
//
// If the field URL is missing, the title will be
// displayed as normal text
URLTitle string
// If enabled together will be rejected by Pushover
// Enable HTML formatting of the message
//
// See the Pushover REST API documentation for allowed tags
HTML string
// Enable monospace formatting of the message
Monospace string
// Sound name for the sound on the user's device
//
// See the Pushover REST API documentation for valid
// values. Invalid sound names will not be rejected by
//Pushover
Sound string
// The device to send the message to rather than all the
// user's devices.
//
// Devices not registered will not be rejected by Pushover
// and will therefore fail silently.
Device string
// Priority number for the message
//
// See the Pushover REST API documentation for values and
// what they mean
//
// Invalid priority numbers will be rejected by Pushover
Priority string
// How often in seconds the Pushover servers will send
// the same notification to the user
//
// Must be set when Priority is set to "2" and must
// have a value of at least 30 seconds between retries
Retry string
// How many seconds your notification will continue to
// be retried for (every retry seconds)
//
// Must be set when Priority is set to "2" and must
// have a maximum value of at most 10800 seconds
// (3 hours)
Expire string
// Callback url for the message
//
// Optional be set when Priority is set to "2"
Callback string
// Unix timestamp for the message rather than the time
// the message was received by the Pushover REST API
//
// Invalid timestamps will not be rejected by Pushover
Timestamp string
// Reader for image (attachment) data
ImageReader io.Reader
// Optional image name
//
// Leave blank to default to image.jpg
ImageName string
}
// MessageResponse is the response from this API. It is read from
// the body of the Pushover REST API response and translated
// to this response structure.
//
// For access to the original, untranslated response, access
// the ResponseBody field.
type MessageResponse struct {
// Original response body from POST
ResponseBody string
// HTTP Status string
HTTPStatus string
// HTTP Status Code
HTTPStatusCode int
// The status as returned by the Pushover API.
//
// Value of 1 indicates 200 response received.
// Any other value indicates an error with the
// input.
APIStatus int
// ID assigned to the request by Pushover
Request string
// Receipt
//
// When a priority of 2 is given, a receipt field
// is returned
Receipt string
// List of errors returned
//
// Empty if no errors
Errors []string
// Map of parameters and corresponding errors
//
// Empty if no errors
ErrorParameters map[string]string
}
// MessageContext will submit a request to the Pushover
// Message API. This function will send a
// message, triggering a notification on a user's
// device or a group's devices.
//
// resp, err := pushover.MessageContext(context.Background(),
// pushover.MessageRequest{
// Token: token,
// User: user,
// Message: message,
// })
func MessageContext(ctx context.Context, request MessageRequest) (*MessageResponse, error) {
var requestData io.Reader
var contentType string
if len(request.PushoverURL) == 0 {
request.PushoverURL = messagesURL
}
if len(request.ImageName) == 0 {
request.ImageName = "image.jpg"
}
fields := []struct {
field string
value string
}{
{field: keyToken, value: request.Token},
{field: keyUser, value: request.User},
{field: keyMessage, value: request.Message},
{field: keyTitle, value: request.Title},
{field: keyURL, value: request.URL},
{field: keyURLTitle, value: request.URLTitle},
{field: keyHTML, value: request.HTML},
{field: keyMonospace, value: request.Monospace},
{field: keySound, value: request.Sound},
{field: keyDevice, value: request.Device},
{field: keyPriority, value: request.Priority},
{field: keyRetry, value: request.Retry},
{field: keyExpire, value: request.Expire},
{field: keyCallback, value: request.Callback},
{field: keyTimestamp, value: request.Timestamp},
}
if request.ImageReader == nil {
formData := url.Values{}
for _, v := range fields {
if len(v.value) > 0 {
formData.Set(v.field, v.value)
}
}
requestData = strings.NewReader(formData.Encode())
contentType = "application/x-www-form-urlencoded"
} else {
requestBody := &bytes.Buffer{}
writer := multipart.NewWriter(requestBody)
part, _ := writer.CreateFormFile("attachment", request.ImageName)
_, _ = io.Copy(part, request.ImageReader)
for _, v := range fields {
if len(v.value) > 0 {
_ = writer.WriteField(v.field, v.value)
}
}
writer.Close()
requestData = requestBody
contentType = writer.FormDataContentType()
}
req, err := http.NewRequest(http.MethodPost, request.PushoverURL, requestData)
if err != nil {
return nil, &ErrInvalidRequest{}
}
req = req.WithContext(ctx)
req.Header.Set("Content-Type", contentType)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return nil, err
}
defer resp.Body.Close()
r := new(MessageResponse)
body := &bytes.Buffer{}
_, err = body.ReadFrom(resp.Body)
if err != nil {
return nil, &ErrInvalidResponse{}
}
r.ResponseBody = body.String()
r.HTTPStatus = resp.Status
r.HTTPStatusCode = resp.StatusCode
// Decode json response
var result map[string]interface{}
if e := json.NewDecoder(strings.NewReader(string(r.ResponseBody))).Decode(&result); e != nil {
return nil, &ErrInvalidResponse{}
}
var ok bool
// Populate request status
if r.APIStatus, ok = mapKeyToInt(keyStatus, result); !ok {
return nil, &ErrInvalidResponse{}
}
delete(result, keyStatus)
// Populate request ID
if r.Request, ok = result[keyRequest].(string); !ok {
return nil, &ErrInvalidResponse{}
}
delete(result, keyRequest)
// Populate receipt
if r.Receipt, ok = result[keyReceipt].(string); ok {
delete(result, keyReceipt)
}
// Populate errors
r.Errors = interfaceArrayToStringArray(keyErrors, result)
delete(result, keyErrors)
// Populate parameters with corresponding errors
r.ErrorParameters = interfaceMapToStringMap(result)
return r, nil
}
// Message will submit a request to the Pushover
// Message API. This function will send a
// message, triggering a notification on a user's
// device or a group's devices.
//
// resp, err := pushover.Message(pushover.MessageRequest{
// Token: token,
// User: user,
// Message: message,
// })
func Message(request MessageRequest) (*MessageResponse, error) {
return MessageContext(context.Background(), request)
}