-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
114 lines (97 loc) · 2.9 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
package clicksend
import (
"bytes"
"context"
"net/http"
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/gocommon/httpx"
"github.com/nyaruka/gocommon/jsonx"
)
var (
maxMsgLength = 1224
sendURL = "https://rest.clicksend.com/v3/sms/send"
)
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("CS"), "ClickSend")}
}
// 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, handlers.NewTelReceiveHandler(h, "from", "body"))
return nil
}
// {
// "messages": [
// {
// "to": "+61411111111",
// "source": "sdk",
// "body": "body"
// },
// {
// "list_id": 0,
// "source": "sdk",
// "body": "body"
// }
// ]
// }
type mtPayload struct {
Messages [1]struct {
To string `json:"to"`
From string `json:"from"`
Body string `json:"body"`
Source string `json:"source"`
} `json:"messages"`
}
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 {
payload := &mtPayload{}
payload.Messages[0].To = msg.URN().Path()
payload.Messages[0].From = msg.Channel().Address()
payload.Messages[0].Body = part
payload.Messages[0].Source = "courier"
requestBody := jsonx.MustMarshal(payload)
req, err := http.NewRequest(http.MethodPost, sendURL, bytes.NewReader(requestBody))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.SetBasicAuth(username, password)
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
}
s, _ := jsonparser.GetString(respBody, "data", "messages", "[0]", "status")
if s != "SUCCESS" {
return courier.ErrResponseContent
}
id, _ := jsonparser.GetString(respBody, "data", "messages", "[0]", "message_id")
if id != "" {
res.AddExternalID(id)
} else {
return courier.ErrResponseContent
}
}
return nil
}
func (h *handler) RedactValues(ch courier.Channel) []string {
return []string{
httpx.BasicAuth(ch.StringConfigForKey(courier.ConfigUsername, ""), ch.StringConfigForKey(courier.ConfigPassword, "")),
}
}