-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
196 lines (166 loc) · 6.31 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
package clickatell
import (
"bytes"
"context"
"fmt"
"mime"
"net/http"
"net/url"
"strings"
"time"
"unicode/utf16"
"unicode/utf8"
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/urns"
)
var (
maxMsgLength = 640
sendURL = "https://platform.clickatell.com/messages/http/send"
)
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("CT"), "Clickatell")}
}
// 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, handlers.JSONPayload(h, h.receiveMessage))
s.AddHandlerRoute(h, http.MethodPost, "status", courier.ChannelLogTypeMsgStatus, handlers.JSONPayload(h, h.receiveStatus))
return nil
}
type statusPayload struct {
MessageID string `name:"messageId"`
StatusCode int `name:"statusCode"`
}
var statusMapping = map[int]courier.MsgStatus{
1: courier.MsgStatusFailed, // incorrect msg id
2: courier.MsgStatusWired, // queued
3: courier.MsgStatusSent, // delivered to upstream gateway
4: courier.MsgStatusSent, // delivered to upstream gateway
5: courier.MsgStatusFailed, // error in message
6: courier.MsgStatusFailed, // terminated by user
7: courier.MsgStatusFailed, // error delivering
8: courier.MsgStatusWired, // msg received
9: courier.MsgStatusFailed, // error routing
10: courier.MsgStatusFailed, // expired
11: courier.MsgStatusWired, // delayed but queued
12: courier.MsgStatusFailed, // out of credit
14: courier.MsgStatusFailed, // too long
}
// receiveStatus is our HTTP handler function for status updates
func (h *handler) receiveStatus(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, payload *statusPayload, clog *courier.ChannelLog) ([]courier.Event, error) {
if payload.MessageID == "" || payload.StatusCode == 0 {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r,
fmt.Errorf("missing one of 'messageId' or 'statusCode' in request parameters"))
}
msgStatus, found := statusMapping[payload.StatusCode]
if !found {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r,
fmt.Errorf("unknown status '%d', must be one of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14", payload.StatusCode))
}
// write our status
status := h.Backend().NewStatusUpdateByExternalID(channel, payload.MessageID, msgStatus, clog)
return handlers.WriteMsgStatusAndResponse(ctx, h, channel, status, w, r)
}
type moPayload struct {
MessageID string `name:"messageId"`
FromNumber string `name:"fromNumber"`
ToNumber string `name:"toNumber"`
Timestamp int64 `name:"timestamp"`
Text string `name:"text"`
Charset string `name:"charset"`
}
// 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, payload *moPayload, clog *courier.ChannelLog) ([]courier.Event, error) {
if payload.FromNumber == "" || payload.MessageID == "" || payload.Text == "" || payload.Timestamp == 0 {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r,
fmt.Errorf("missing one of 'messageId', 'fromNumber', 'text' or 'timestamp' in request body"))
}
date := time.Unix(0, payload.Timestamp*1000000)
text := payload.Text
if payload.Charset == "UTF-16BE" {
// unescape the JSON
text, _ = url.QueryUnescape(text)
// then decode from UTF16
textBytes := []byte{}
for _, textByte := range text {
textBytes = append(textBytes, byte(textByte))
}
text, _ = decodeUTF16BE(textBytes)
}
// clickatell URL encodes escapes ISO 8859 escape sequences
if payload.Charset == "ISO-8859-1" {
// unescape the JSON
text, _ = url.QueryUnescape(text)
// then decode from 8859
text = mime.BEncoding.Encode("ISO-8859-1", text)
text, _ = new(mime.WordDecoder).DecodeHeader(text)
}
// create our URN
urn, err := urns.ParsePhone(payload.FromNumber, channel.Country())
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
// build our msg
msg := h.Backend().NewIncomingMsg(channel, urn, text, payload.MessageID, clog).WithReceivedOn(date.UTC())
// and finally write our message
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
}
// utility method to decode crazy clickatell 16 bit format
func decodeUTF16BE(b []byte) (string, error) {
if len(b)%2 != 0 {
return "", fmt.Errorf("byte slice must be of even length: %v", b)
}
u16s := make([]uint16, 1)
ret := &bytes.Buffer{}
b8buf := make([]byte, 4)
lb := len(b)
for i := 0; i < lb; i += 2 {
u16s[0] = uint16(b[i+1]) + (uint16(b[i]) << 8)
r := utf16.Decode(u16s)
n := utf8.EncodeRune(b8buf, r[0])
ret.Write(b8buf[:n])
}
return ret.String(), nil
}
func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
apiKey := msg.Channel().StringConfigForKey(courier.ConfigAPIKey, "")
if apiKey == "" {
return courier.ErrChannelConfig
}
parts := handlers.SplitMsgByChannel(msg.Channel(), handlers.GetTextAndAttachments(msg), maxMsgLength)
for _, part := range parts {
form := url.Values{
"apiKey": []string{apiKey},
"from": []string{strings.TrimPrefix(msg.Channel().Address(), "+")},
"to": []string{strings.TrimPrefix(msg.URN().Path(), "+")},
"content": []string{part},
}
partSendURL, _ := url.Parse(sendURL)
partSendURL.RawQuery = form.Encode()
req, _ := http.NewRequest(http.MethodGet, partSendURL.String(), nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
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
}
// try to read out our message id, if we can't then this was a failure
externalID, _ := jsonparser.GetString(respBody, "messages", "[0]", "apiMessageId")
if externalID != "" {
res.AddExternalID(externalID)
} else {
return courier.ErrResponseUnexpected
}
}
return nil
}