-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
180 lines (146 loc) · 5.22 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
package bongolive
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/gsm7"
"github.com/nyaruka/gocommon/urns"
"github.com/buger/jsonparser"
)
var (
sendURL = "https://api.blsmsgw.com:8443/bin/send.json"
maxMsgLength = 160
)
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("BL"), "Bongo Live")}
}
func init() {
courier.RegisterHandler(newHandler())
}
// 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.ChannelLogTypeUnknown, h.receiveMessage)
return nil
}
var statusMapping = map[int]courier.MsgStatus{
1: courier.MsgStatusDelivered,
2: courier.MsgStatusSent,
3: courier.MsgStatusErrored,
4: courier.MsgStatusErrored,
5: courier.MsgStatusErrored,
6: courier.MsgStatusErrored,
7: courier.MsgStatusErrored,
8: courier.MsgStatusSent,
9: courier.MsgStatusErrored,
10: courier.MsgStatusErrored,
11: courier.MsgStatusErrored,
}
type moForm struct {
ID string `name:"ID"`
DLRID string `name:"DLRID"`
To string `name:"DESTADDR"`
From string `name:"SOURCEADDR" `
Message string `name:"MESSAGE"`
MsgType int `name:"MSGTYPE"`
Status int `name:"STATUS"`
}
// 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
form := &moForm{}
err = handlers.DecodeAndValidateForm(form, r)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
if form.MsgType == 5 {
clog.Type = courier.ChannelLogTypeMsgStatus
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
msgStatus, found := statusMapping[form.Status]
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", form.Status))
}
// write our status
status := h.Backend().NewStatusUpdateByExternalID(channel, form.DLRID, msgStatus, clog)
return handlers.WriteMsgStatusAndResponse(ctx, h, channel, status, w, r)
}
clog.Type = courier.ChannelLogTypeMsgReceive
// create our URN
urn, err := urns.ParsePhone(form.From, channel.Country(), true, false)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
// build our msg
msg := h.Backend().NewIncomingMsg(channel, urn, form.Message, form.ID, clog).WithReceivedOn(time.Now().UTC())
// and finally queue our message
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
}
func (h *handler) WriteMsgSuccessResponse(ctx context.Context, w http.ResponseWriter, msgs []courier.MsgIn) error {
return writeBongoLiveResponse(w)
}
func (h *handler) WriteStatusSuccessResponse(ctx context.Context, w http.ResponseWriter, statuses []courier.StatusUpdate) error {
return writeBongoLiveResponse(w)
}
func (h *handler) WriteRequestIgnored(ctx context.Context, w http.ResponseWriter, details string) error {
return writeBongoLiveResponse(w)
}
func writeBongoLiveResponse(w http.ResponseWriter) error {
w.Header().Add("Content-type", "text/plain")
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte{})
return err
}
func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if username == "" || password == "" {
return courier.ErrChannelConfig
}
parts := handlers.SplitMsgByChannel(msg.Channel(), handlers.GetTextAndAttachments(msg), maxMsgLength)
for _, part := range parts {
form := url.Values{
"USERNAME": []string{username},
"PASSWORD": []string{password},
"SOURCEADDR": []string{strings.TrimPrefix(msg.Channel().Address(), "+")},
"DESTADDR": []string{strings.TrimPrefix(msg.URN().Path(), "+")},
"MESSAGE": []string{part},
"DLR": []string{"1"},
}
replaced := gsm7.ReplaceSubstitutions(part)
if gsm7.IsValid(replaced) {
form["MESSAGE"] = []string{replaced}
} else {
form["CHARCODE"] = []string{"2"}
}
partSendURL, _ := url.Parse(sendURL)
partSendURL.RawQuery = form.Encode()
req, _ := http.NewRequest(http.MethodPost, partSendURL.String(), nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, respBody, err := h.RequestHTTPInsecure(req, clog)
if err != nil || resp.StatusCode/100 == 5 {
return courier.ErrConnectionFailed
} else if resp.StatusCode/100 != 2 {
return courier.ErrResponseStatus
}
msgStatus, _ := jsonparser.GetString(respBody, "results", "[0]", "status")
if msgStatus != "0" {
return courier.ErrResponseContent
}
// grab the external id if we can
externalID, _ := jsonparser.GetString(respBody, "results", "[0]", "msgid")
if externalID != "" {
res.AddExternalID(externalID)
}
}
return nil
}