-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
435 lines (365 loc) · 14.4 KB
/
handler.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package external
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/antchfx/xmlquery"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/gsm7"
"github.com/nyaruka/gocommon/urns"
)
const (
contentURLEncoded = "urlencoded"
contentJSON = "json"
contentXML = "xml"
configFromXPath = "from_xpath"
configTextXPath = "text_xpath"
configMOFromField = "mo_from_field"
configMOTextField = "mo_text_field"
configMODateField = "mo_date_field"
configMOResponseContentType = "mo_response_content_type"
configMOResponse = "mo_response"
configMTResponseCheck = "mt_response_check"
configEncoding = "encoding"
encodingDefault = "D"
encodingSmart = "S"
)
var defaultFromFields = []string{"from", "sender"}
var defaultTextFields = []string{"text"}
var defaultDateFields = []string{"date", "time"}
var contentTypeMappings = map[string]string{
contentURLEncoded: "application/x-www-form-urlencoded",
contentJSON: "application/json",
contentXML: "text/xml; charset=utf-8",
}
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("EX"), "External")}
}
// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
s.AddHandlerRoute(h, http.MethodPost, "receive", courier.ChannelLogTypeMsgReceive, h.receiveMessage)
s.AddHandlerRoute(h, http.MethodGet, "receive", courier.ChannelLogTypeMsgReceive, h.receiveMessage)
sentHandler := h.buildStatusHandler("sent")
s.AddHandlerRoute(h, http.MethodGet, "sent", courier.ChannelLogTypeMsgStatus, sentHandler)
s.AddHandlerRoute(h, http.MethodPost, "sent", courier.ChannelLogTypeMsgStatus, sentHandler)
deliveredHandler := h.buildStatusHandler("delivered")
s.AddHandlerRoute(h, http.MethodGet, "delivered", courier.ChannelLogTypeMsgStatus, deliveredHandler)
s.AddHandlerRoute(h, http.MethodPost, "delivered", courier.ChannelLogTypeMsgStatus, deliveredHandler)
failedHandler := h.buildStatusHandler("failed")
s.AddHandlerRoute(h, http.MethodGet, "failed", courier.ChannelLogTypeMsgStatus, failedHandler)
s.AddHandlerRoute(h, http.MethodPost, "failed", courier.ChannelLogTypeMsgStatus, failedHandler)
s.AddHandlerRoute(h, http.MethodPost, "stopped", courier.ChannelLogTypeEventReceive, h.receiveStopContact)
s.AddHandlerRoute(h, http.MethodGet, "stopped", courier.ChannelLogTypeEventReceive, h.receiveStopContact)
return nil
}
type stopContactForm struct {
From string `validate:"required" name:"from"`
}
func (h *handler) receiveStopContact(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, clog *courier.ChannelLog) ([]courier.Event, error) {
form := &stopContactForm{}
err := handlers.DecodeAndValidateForm(form, r)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
// create our URN
urn := urns.NilURN
if channel.Schemes()[0] == urns.Phone.Prefix {
urn, err = urns.ParsePhone(form.From, channel.Country(), true, false)
} else {
urn, err = urns.NewFromParts(channel.Schemes()[0], form.From, nil, "")
}
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
// create a stop channel event
channelEvent := h.Backend().NewChannelEvent(channel, courier.EventTypeStopContact, urn, clog)
err = h.Backend().WriteChannelEvent(ctx, channelEvent, clog)
if err != nil {
return nil, err
}
return []courier.Event{channelEvent}, courier.WriteChannelEventSuccess(w, channelEvent)
}
// utility function to grab the form value for either the passed in name (if non-empty) or the first set
// value from defaultNames
func getFormField(form url.Values, defaultNames []string, name string) string {
if name != "" {
values, found := form[name]
if found {
return values[0]
}
}
for _, name := range defaultNames {
values, found := form[name]
if found {
return values[0]
}
}
return ""
}
// receiveMessage is our HTTP handler function for incoming messages
func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, clog *courier.ChannelLog) ([]courier.Event, error) {
var err error
var from, dateString, text string
fromXPath := channel.StringConfigForKey(configFromXPath, "")
textXPath := channel.StringConfigForKey(configTextXPath, "")
if fromXPath != "" && textXPath != "" {
// we are reading from an XML body, pull out our fields
body, err := io.ReadAll(io.LimitReader(r.Body, 100000))
defer r.Body.Close()
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("unable to read request body: %s", err))
}
doc, err := xmlquery.Parse(strings.NewReader(string(body)))
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("unable to parse request XML: %s", err))
}
fromNode := xmlquery.FindOne(doc, fromXPath)
textNode := xmlquery.FindOne(doc, textXPath)
if fromNode == nil || textNode == nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("missing from at: %s or text at: %s node", fromXPath, textXPath))
}
from = fromNode.InnerText()
text = textNode.InnerText()
} else {
// parse our form
contentType := r.Header.Get("Content-Type")
var err error
if strings.Contains(contentType, "multipart/form-data") {
err = r.ParseMultipartForm(10000000)
} else {
err = r.ParseForm()
}
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("invalid request: %w", err))
}
from = getFormField(r.Form, defaultFromFields, channel.StringConfigForKey(configMOFromField, ""))
text = getFormField(r.Form, defaultTextFields, channel.StringConfigForKey(configMOTextField, ""))
dateString = getFormField(r.Form, defaultDateFields, channel.StringConfigForKey(configMODateField, ""))
}
// must have from field
if from == "" {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("must have one of 'sender' or 'from' set"))
}
// if we have a date, parse it
date := time.Now()
if dateString != "" {
date, err = time.Parse(time.RFC3339Nano, dateString)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("invalid date format, must be RFC 3339"))
}
}
// create our URN
urn := urns.NilURN
if channel.Schemes()[0] == urns.Phone.Prefix {
urn, err = urns.ParsePhone(from, channel.Country(), true, false)
} else {
urn, err = urns.NewFromParts(channel.Schemes()[0], from, nil, "")
}
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
// build our msg
msg := h.Backend().NewIncomingMsg(channel, urn, text, "", clog).WithReceivedOn(date)
// and finally write our message
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
}
// WriteMsgSuccessResponse writes our response in TWIML format
func (h *handler) WriteMsgSuccessResponse(ctx context.Context, w http.ResponseWriter, msgs []courier.MsgIn) error {
moResponse := msgs[0].Channel().StringConfigForKey(configMOResponse, "")
if moResponse == "" {
return courier.WriteMsgSuccess(w, msgs)
}
moResponseContentType := msgs[0].Channel().StringConfigForKey(configMOResponseContentType, "")
if moResponseContentType != "" {
w.Header().Set("Content-Type", moResponseContentType)
}
w.WriteHeader(200)
_, err := fmt.Fprint(w, moResponse)
return err
}
// buildStatusHandler deals with building a handler that takes what status is received in the URL
func (h *handler) buildStatusHandler(status string) courier.ChannelHandleFunc {
return func(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, clog *courier.ChannelLog) ([]courier.Event, error) {
return h.receiveStatus(ctx, status, channel, w, r, clog)
}
}
type statusForm struct {
ID int64 `name:"id" validate:"required"`
}
var statusMappings = map[string]courier.MsgStatus{
"failed": courier.MsgStatusFailed,
"sent": courier.MsgStatusSent,
"delivered": courier.MsgStatusDelivered,
}
// receiveStatus is our HTTP handler function for status updates
func (h *handler) receiveStatus(ctx context.Context, statusString string, channel courier.Channel, w http.ResponseWriter, r *http.Request, clog *courier.ChannelLog) ([]courier.Event, error) {
form := &statusForm{}
err := handlers.DecodeAndValidateForm(form, r)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
// get our status
msgStatus, found := statusMappings[strings.ToLower(statusString)]
if !found {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("unknown status '%s', must be one failed, sent or delivered", statusString))
}
// write our status
status := h.Backend().NewStatusUpdate(channel, courier.MsgID(form.ID), msgStatus, clog)
return handlers.WriteMsgStatusAndResponse(ctx, h, channel, status, w, r)
}
func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
channel := msg.Channel()
sendURL := channel.StringConfigForKey(courier.ConfigSendURL, "")
if sendURL == "" {
return courier.ErrChannelConfig
}
// figure out what encoding to tell kannel to send as
encoding := channel.StringConfigForKey(configEncoding, encodingDefault)
responseCheck := channel.StringConfigForKey(configMTResponseCheck, "")
sendMethod := channel.StringConfigForKey(courier.ConfigSendMethod, http.MethodPost)
sendBody := channel.StringConfigForKey(courier.ConfigSendBody, "")
sendMaxLength := channel.IntConfigForKey(courier.ConfigMaxLength, 160)
contentType := channel.StringConfigForKey(courier.ConfigContentType, contentURLEncoded)
contentTypeHeader := contentTypeMappings[contentType]
if contentTypeHeader == "" {
contentTypeHeader = contentType
}
parts := handlers.SplitMsgByChannel(channel, handlers.GetTextAndAttachments(msg), sendMaxLength)
for i, part := range parts {
// build our request
form := map[string]string{
"id": msg.ID().String(),
"text": part,
"to": msg.URN().Path(),
"to_no_plus": strings.TrimPrefix(msg.URN().Path(), "+"),
"from": channel.Address(),
"from_no_plus": strings.TrimPrefix(channel.Address(), "+"),
"channel": string(channel.UUID()),
"session_status": msg.SessionStatus(),
}
useNationalStr := channel.ConfigForKey(courier.ConfigUseNational, false)
useNational, _ := useNationalStr.(bool)
// if we are meant to use national formatting (no country code) pull that out
if useNational {
nationalTo := urns.ToLocalPhone(msg.URN(), channel.Country())
form["to"] = nationalTo
form["to_no_plus"] = nationalTo
}
// if we are smart, first try to convert to GSM7 chars
if encoding == encodingSmart {
replaced := gsm7.ReplaceSubstitutions(part)
if gsm7.IsValid(replaced) {
form["text"] = replaced
}
}
formEncoded := encodeVariables(form, contentURLEncoded)
// put quick replies on last message part
if i == len(parts)-1 {
formEncoded["quick_replies"] = buildQuickRepliesResponse(msg.QuickReplies(), sendMethod, contentURLEncoded)
} else {
formEncoded["quick_replies"] = buildQuickRepliesResponse([]string{}, sendMethod, contentURLEncoded)
}
url := replaceVariables(sendURL, formEncoded)
var body io.Reader
if sendMethod == http.MethodPost || sendMethod == http.MethodPut {
formEncoded = encodeVariables(form, contentType)
if i == len(parts)-1 {
formEncoded["quick_replies"] = buildQuickRepliesResponse(msg.QuickReplies(), sendMethod, contentType)
} else {
formEncoded["quick_replies"] = buildQuickRepliesResponse([]string{}, sendMethod, contentType)
}
body = strings.NewReader(replaceVariables(sendBody, formEncoded))
}
req, err := http.NewRequest(sendMethod, url, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", contentTypeHeader)
// TODO can drop this when channels have been migrated to use ConfigSendHeaders
authorization := channel.StringConfigForKey(courier.ConfigSendAuthorization, "")
if authorization != "" {
req.Header.Set("Authorization", authorization)
}
headers := channel.ConfigForKey(courier.ConfigSendHeaders, map[string]any{}).(map[string]any)
for hKey, hValue := range headers {
req.Header.Set(hKey, fmt.Sprint(hValue))
}
resp, respBody, err := h.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 == 5 {
return courier.ErrConnectionFailed
} else if resp.StatusCode/100 != 2 {
return courier.ErrResponseStatus
}
if responseCheck != "" && !strings.Contains(string(respBody), responseCheck) {
return courier.ErrResponseContent
}
}
return nil
}
type quickReplyXMLItem struct {
XMLName xml.Name `xml:"item"`
Value string `xml:",chardata"`
}
func buildQuickRepliesResponse(quickReplies []string, sendMethod string, contentType string) string {
if quickReplies == nil {
quickReplies = []string{}
}
if (sendMethod == http.MethodPost || sendMethod == http.MethodPut) && contentType == contentJSON {
marshalled, _ := json.Marshal(quickReplies)
return string(marshalled)
} else if (sendMethod == http.MethodPost || sendMethod == http.MethodPut) && contentType == contentXML {
items := make([]quickReplyXMLItem, len(quickReplies))
for i, v := range quickReplies {
items[i] = quickReplyXMLItem{Value: v}
}
marshalled, _ := xml.Marshal(items)
return string(marshalled)
} else {
response := bytes.Buffer{}
for _, reply := range quickReplies {
reply = url.QueryEscape(reply)
response.WriteString(fmt.Sprintf("&quick_reply=%s", reply))
}
return response.String()
}
}
func encodeVariables(variables map[string]string, contentType string) map[string]string {
encoded := make(map[string]string)
for k, v := range variables {
// encode according to our content type
switch contentType {
case contentJSON:
marshalled, _ := json.Marshal(v)
v = string(marshalled)
case contentURLEncoded:
v = url.QueryEscape(v)
case contentXML:
buf := &bytes.Buffer{}
xml.EscapeText(buf, []byte(v))
v = buf.String()
}
encoded[k] = v
}
return encoded
}
func replaceVariables(text string, variables map[string]string) string {
for k, v := range variables {
text = strings.Replace(text, fmt.Sprintf("{{%s}}", k), v, -1)
}
return text
}