forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexmo.go
215 lines (177 loc) · 6.92 KB
/
nexmo.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
package nexmo
import (
"context"
"fmt"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/nyaruka/courier/gsm7"
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/urns"
"github.com/pkg/errors"
)
/*
GET /handlers/nexmo/status/uuid/?msisdn=4527631111&to=Tak&network-code=23820&messageId=0C0000002EEBDA56&price=0.01820000&status=delivered&scts=1705021324&err-code=0&message-timestamp=2017-05-02+11%3A24%3A03
GET /handlers/nexmo/receive/uuid/?msisdn=15862151111&to=12812581111&messageId=0B0000004B65F62F&text=Msg&type=text&keyword=Keyword&message-timestamp=2017-05-01+21%3A52%3A49
*/
const configNexmoAPIKey = "nexmo_api_key"
const configNexmoAPISecret = "nexmo_api_secret"
const configNexmoAppID = "nexmo_app_id"
const configNexmoAppPrivateKey = "nexmo_app_private_key"
var maxMsgLength = 1600
var sendURL = "https://rest.nexmo.com/sms/json"
var throttledRE = regexp.MustCompile(`.*Throughput Rate Exceeded - please wait \[ (\d+) \] and retry.*`)
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("NX"), "Nexmo")}
}
// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
err := s.AddHandlerRoute(h, http.MethodGet, "receive", h.ReceiveMessage)
if err != nil {
return err
}
err = s.AddHandlerRoute(h, http.MethodPost, "receive", h.ReceiveMessage)
if err != nil {
return err
}
return s.AddHandlerRoute(h, http.MethodGet, "status", h.StatusMessage)
}
type nexmoDeliveryReport struct {
To string `name:"to"`
MessageID string `name:"messageID"`
Status string `name:"status"`
}
var statusMappings = map[string]courier.MsgStatusValue{
"failed": courier.MsgFailed,
"expired": courier.MsgFailed,
"rejected": courier.MsgFailed,
"buffered": courier.MsgSent,
"accepted": courier.MsgSent,
"unknown": courier.MsgWired,
"delivered": courier.MsgDelivered,
}
// StatusMessage is our HTTP handler function for status updates
func (h *handler) StatusMessage(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
nexmoDeliveryReport := &nexmoDeliveryReport{}
handlers.DecodeAndValidateForm(nexmoDeliveryReport, r)
if nexmoDeliveryReport.MessageID == "" {
return nil, courier.WriteAndLogRequestIgnored(ctx, w, r, channel, "no messageId parameter, ignored")
}
msgStatus, found := statusMappings[nexmoDeliveryReport.Status]
if !found {
return nil, courier.WriteAndLogRequestIgnored(ctx, w, r, channel, "ignoring unknown status report")
}
status := h.Backend().NewMsgStatusForExternalID(channel, nexmoDeliveryReport.MessageID, msgStatus)
// write our status
err := h.Backend().WriteMsgStatus(ctx, status)
// nexmo can return more than one message id when doing multipart, so ignore status updates which might be for one of those parts
if err == courier.ErrMsgNotFound {
return nil, courier.WriteAndLogStatusMsgNotFound(ctx, w, r, channel)
}
if err != nil {
return nil, err
}
return []courier.Event{status}, courier.WriteStatusSuccess(ctx, w, r, []courier.MsgStatus{status})
}
type nexmoIncomingMessage struct {
To string `name:"to"`
From string `name:"msisdn"`
Text string `name:"text"`
MessageID string `name:"messageId"`
}
// 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) ([]courier.Event, error) {
nexmoIncomingMessage := &nexmoIncomingMessage{}
handlers.DecodeAndValidateForm(nexmoIncomingMessage, r)
if nexmoIncomingMessage.To == "" {
return nil, courier.WriteAndLogRequestIgnored(ctx, w, r, channel, "no to parameter, ignored")
}
// create our URN
urn := urns.NewTelURNForCountry(nexmoIncomingMessage.From, channel.Country())
// build our msg
msg := h.Backend().NewIncomingMsg(channel, urn, nexmoIncomingMessage.Text)
// and write it
err := h.Backend().WriteMsg(ctx, msg)
if err != nil {
return nil, err
}
return []courier.Event{msg}, courier.WriteMsgSuccess(ctx, w, r, []courier.Msg{msg})
}
// SendMsg sends the passed in message, returning any error
func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) {
nexmoAPIKey := msg.Channel().StringConfigForKey(configNexmoAPIKey, "")
if nexmoAPIKey == "" {
return nil, fmt.Errorf("no nexmo API key set for NX channel")
}
nexmoAPISecret := msg.Channel().StringConfigForKey(configNexmoAPISecret, "")
if nexmoAPISecret == "" {
return nil, fmt.Errorf("no nexmo API secret set for NX channel")
}
// build our callback URL
callbackDomain := msg.Channel().CallbackDomain(h.Server().Config().Domain)
callbackURL := fmt.Sprintf("https://%s/c/nx/%s/status", callbackDomain, msg.Channel().UUID())
text := handlers.GetTextAndAttachments(msg)
textType := "text"
if !gsm7.IsValid(text) {
textType = "unicode"
}
status := h.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), courier.MsgErrored)
parts := handlers.SplitMsg(text, maxMsgLength)
for _, part := range parts {
form := url.Values{
"api_key": []string{nexmoAPIKey},
"api_secret": []string{nexmoAPISecret},
"from": []string{strings.TrimPrefix(msg.Channel().Address(), "+")},
"to": []string{strings.TrimPrefix(msg.URN().Path(), "+")},
"text": []string{part},
"status-report-req": []string{"1"},
"callback": []string{callbackURL},
"type": []string{textType},
}
req, err := http.NewRequest(http.MethodPost, sendURL, strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
var rr *utils.RequestResponse
var requestErr error
for i := 0; i < 3; i++ {
rr, requestErr = utils.MakeHTTPRequest(req)
matched := throttledRE.FindAllStringSubmatch(string([]byte(rr.Body)), -1)
if len(matched) > 0 && len(matched[0]) > 0 {
sleepTime, _ := strconv.Atoi(matched[0][1])
time.Sleep(time.Duration(sleepTime) * time.Millisecond)
} else {
break
}
}
// record our status and log
log := courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr)
status.AddLog(log)
if requestErr != nil {
log.WithError("Message Send Error", requestErr)
return status, nil
}
nexmoStatus, err := jsonparser.GetString([]byte(rr.Body), "messages", "[0]", "status")
if err != nil || nexmoStatus != "0" {
log.WithError("Message Send Error", errors.Errorf("failed to send message, received error status [%s]", nexmoStatus))
return status, nil
}
externalID, err := jsonparser.GetString([]byte(rr.Body), "messages", "[0]", "message-id")
if err == nil {
status.SetExternalID(externalID)
}
}
status.SetStatus(courier.MsgWired)
return status, nil
}