-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
198 lines (163 loc) · 6.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package dart
/*
GET /handlers/dartmedia/received/uuid?userid=username&password=xxxxxxxx&original=6285218761111&sendto=93456&messagetype=0&messageid=170503131327@170504131327@93456SMS9755064&message=Msg&date=20170503131559&dcs=0&udhl=0&charset=utf-8
*/
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/stringsx"
"github.com/nyaruka/gocommon/urns"
)
var (
sendURL = "http://202.43.169.11/APIhttpU/receive2waysms.php"
maxMsgLength = 160
errorCodes = map[string]string{
"001": "Authentication error.",
"101": "Account expired or invalid parameters.",
}
)
type handler struct {
handlers.BaseHandler
sendURL string
maxLength int
}
// NewHandler returns a new DartMedia ready to be registered
func NewHandler(channelType string, name string, sendURL string, maxLength int) courier.ChannelHandler {
return &handler{
handlers.NewBaseHandler(courier.ChannelType(channelType), name),
sendURL,
maxLength,
}
}
func init() {
courier.RegisterHandler(NewHandler("DA", "DartMedia", sendURL, maxMsgLength))
}
// 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.MethodGet, "receive", courier.ChannelLogTypeMsgReceive, h.receiveMessage)
s.AddHandlerRoute(h, http.MethodGet, "delivered", courier.ChannelLogTypeMsgStatus, h.receiveStatus)
return nil
}
type moForm struct {
Message string `name:"message"`
Original string `name:"original"`
SendTo string `name:"sendto"`
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, clog *courier.ChannelLog) ([]courier.Event, error) {
form := &moForm{}
err := handlers.DecodeAndValidateForm(form, r)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
if form.Original == "" || form.SendTo == "" {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("missing required parameters original and sendto"))
}
// create our URN
urn, err := urns.ParsePhone(form.Original, channel.Country())
if err != nil {
urn, err = urns.New(urns.External, form.Original)
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.MessageID, clog)
// and finally queue our message
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
}
type statusForm struct {
MessageID string `name:"messageid"`
Status string `name:"status"`
}
// 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, 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)
}
if form.Status == "" || form.MessageID == "" {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("parameters messageid and status should not be empty"))
}
statusInt, err := strconv.Atoi(form.Status)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("parsing failed: status '%s' is not an integer", form.Status))
}
msgStatus := courier.MsgStatusSent
if statusInt >= 10 && statusInt <= 12 {
msgStatus = courier.MsgStatusDelivered
}
if statusInt > 20 {
msgStatus = courier.MsgStatusFailed
}
msgID, err := strconv.ParseInt(strings.Split(form.MessageID, ".")[0], 10, 64)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("parsing failed: messageid '%s' is not an integer", form.MessageID))
}
// write our status
status := h.Backend().NewStatusUpdate(channel, courier.MsgID(msgID), msgStatus, clog)
return handlers.WriteMsgStatusAndResponse(ctx, h, channel, status, w, r)
}
// DartMedia expects "000" from a message receive request
func (h *handler) WriteStatusSuccessResponse(ctx context.Context, w http.ResponseWriter, statuses []courier.StatusUpdate) error {
w.WriteHeader(200)
_, err := fmt.Fprint(w, "000")
return err
}
// DartMedia expects "000" from a status request
func (h *handler) WriteMsgSuccessResponse(ctx context.Context, w http.ResponseWriter, msgs []courier.MsgIn) error {
w.WriteHeader(200)
_, err := fmt.Fprint(w, "000")
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), h.maxLength)
for i, part := range parts {
form := url.Values{
"userid": []string{username},
"password": []string{password},
"sendto": []string{strings.TrimPrefix(msg.URN().Path(), "+")},
"original": []string{strings.TrimPrefix(msg.Channel().Address(), "+")},
"udhl": []string{"0"},
"dcs": []string{"0"},
"message": []string{part},
}
messageid := msg.ID().String()
if i > 0 {
messageid = fmt.Sprintf("%s.%d", msg.ID().String(), i+1)
}
form["messageid"] = []string{messageid}
partSendURL, _ := url.Parse(h.sendURL)
partSendURL.RawQuery = form.Encode()
req, err := http.NewRequest(http.MethodGet, partSendURL.String(), nil)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
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
}
responseCode := stringsx.Truncate(string(respBody), 3)
if responseCode != "000" {
return courier.ErrFailedWithReason(responseCode, errorCodes[responseCode])
}
}
return nil
}