-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
174 lines (144 loc) · 5.24 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
package highconnection
import (
"context"
"fmt"
"mime"
"net/http"
"net/url"
"strconv"
"time"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/urns"
)
var (
sendURL = "https://highpushfastapi-v2.hcnx.eu/api"
maxMsgLength = 1500
)
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("HX"), "High Connection")}
}
// 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, "status", courier.ChannelLogTypeMsgStatus, h.receiveStatus)
return nil
}
type moForm struct {
ID int64 `name:"ID"`
To string `name:"TO" validate:"required"`
From string `name:"FROM" validate:"required"`
Message string `name:"MESSAGE"`
ReceiveDate string `name:"RECEPTION_DATE"`
}
// 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)
}
date := time.Now()
if form.ReceiveDate != "" {
date, err = time.Parse("2006-01-02T15:04:05", form.ReceiveDate)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
}
// create our URN
urn, err := urns.ParsePhone(form.From, channel.Country())
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
// Hign connection URL encodes escapes ISO 8859 escape sequences
text, _ := url.QueryUnescape(form.Message)
// decode from ISO 8859
text = mime.BEncoding.Encode("ISO-8859-1", text)
text, _ = new(mime.WordDecoder).DecodeHeader(text)
msgID := ""
if form.ID != 0 {
msgID = strconv.FormatInt(form.ID, 10)
}
// build our Message
msg := h.Backend().NewIncomingMsg(channel, urn, text, msgID, clog).WithReceivedOn(date.UTC())
// and finally write our message
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
}
type statusForm struct {
RetID int64 `name:"ret_id" validate:"required"`
Status int `name:"status" validate:"required"`
}
var statusMapping = map[int]courier.MsgStatus{
2: courier.MsgStatusFailed,
4: courier.MsgStatusSent,
6: courier.MsgStatusDelivered,
11: courier.MsgStatusFailed,
12: courier.MsgStatusFailed,
13: courier.MsgStatusFailed,
14: courier.MsgStatusFailed,
15: courier.MsgStatusFailed,
16: courier.MsgStatusFailed,
}
// 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)
}
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 2, 4, 6, 11, 12, 13, 14, 15 or 16", form.Status))
}
// write our status
status := h.Backend().NewStatusUpdate(channel, courier.MsgID(form.RetID), 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 {
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if username == "" || password == "" {
return courier.ErrChannelConfig
}
callbackDomain := msg.Channel().CallbackDomain(h.Server().Config().Domain)
statusURL := fmt.Sprintf("https://%s/c/hx/%s/status", callbackDomain, msg.Channel().UUID())
receiveURL := fmt.Sprintf("https://%s/c/hx/%s/receive", callbackDomain, msg.Channel().UUID())
parts := handlers.SplitMsgByChannel(msg.Channel(), handlers.GetTextAndAttachments(msg), maxMsgLength)
var flowName string
if msg.Flow() != nil {
flowName = msg.Flow().Name
}
for _, part := range parts {
form := url.Values{
"accountid": []string{username},
"password": []string{password},
"text": []string{part},
"to": []string{msg.URN().Path()},
"ret_id": []string{msg.ID().String()},
"datacoding": []string{"8"},
"user_data": []string{flowName},
"ret_url": []string{statusURL},
"ret_mo_url": []string{receiveURL},
}
msgURL, _ := url.Parse(sendURL)
msgURL.RawQuery = form.Encode()
req, err := http.NewRequest(http.MethodPost, msgURL.String(), nil)
if err != nil {
return err
}
resp, _, err := h.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 == 5 {
return courier.ErrConnectionFailed
} else if resp.StatusCode/100 != 2 {
return courier.ErrResponseStatus
}
}
return nil
}