-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
154 lines (127 loc) · 4.51 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
package clickmobile
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"encoding/xml"
"fmt"
"net/http"
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/dates"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
)
var (
sendURL = "http://206.225.81.36/ucm_api/index.php"
maxMsgLength = 160
configAppID = "app_id"
configOrgID = "org_id"
)
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("CM"), "Click Mobile")}
}
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.MethodPost, "receive", courier.ChannelLogTypeMsgStatus, h.receiveMessage)
return nil
}
// <request>
// <shortCode>3014</shortCode>
// <mobile>2659900993333</mobile>
// <referenceID>1232434354</referenceID>
// <text>This is a test message</text>
// </request>
type moPayload struct {
XMLName xml.Name `xml:"request"`
Shortcode string `xml:"shortCode"`
Mobile string `xml:"mobile"`
ReferenceID string `xml:"referenceID"`
Text string `xml:"text"`
}
// 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) {
payload := &moPayload{}
err := handlers.DecodeAndValidateXML(payload, r)
if err != nil {
return nil, err
}
if payload.Mobile == "" || payload.Shortcode == "" {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("missing parameters, must have 'mobile' and 'shortcode'"))
}
// create our URN
urn, err := urns.ParsePhone(payload.Mobile, 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, payload.Text, payload.ReferenceID, clog)
// and finally write our message
return handlers.WriteMsgsAndResponse(ctx, h, []courier.MsgIn{msg}, w, r, clog)
}
type mtPayload struct {
AppID string `json:"app_id"`
OrgID string `json:"org_id"`
UserID string `json:"user_id"`
Timestamp string `json:"timestamp"`
AuthKey string `json:"auth_key"`
Operation string `json:"operation"`
Reference string `json:"reference"`
MessageType string `json:"message_type"`
From string `json:"src_address"`
To string `json:"dst_address"`
Message string `json:"message"`
}
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, "")
appID := msg.Channel().StringConfigForKey(configAppID, "")
orgID := msg.Channel().StringConfigForKey(configOrgID, "")
if username == "" || password == "" || appID == "" || orgID == "" {
return courier.ErrChannelConfig
}
cmSendURL := msg.Channel().StringConfigForKey(courier.ConfigSendURL, sendURL)
for _, part := range handlers.SplitMsgByChannel(msg.Channel(), handlers.GetTextAndAttachments(msg), maxMsgLength) {
timestamp := dates.Now().UTC().Format("20060102150405")
hasher := md5.New()
hasher.Write([]byte(appID + timestamp + password))
hash := hex.EncodeToString(hasher.Sum(nil))
payload := &mtPayload{
AppID: appID,
OrgID: orgID,
UserID: username,
Timestamp: timestamp,
AuthKey: hash,
Operation: "send",
Reference: msg.ID().String(),
MessageType: "1",
From: msg.Channel().Address(),
To: msg.URN().Path(),
Message: part,
}
requestBody := jsonx.MustMarshal(payload)
req, _ := http.NewRequest(http.MethodPost, cmSendURL, bytes.NewReader(requestBody))
req.Header.Set("Content-Type", "application/json")
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
}
responseCode, _ := jsonparser.GetString(respBody, "code")
if responseCode != "000" {
return courier.ErrResponseContent
}
}
return nil
}