forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
whatsapp.go
288 lines (248 loc) · 8.34 KB
/
whatsapp.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package whatsapp
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/urns"
"github.com/pkg/errors"
)
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("WA"), "WhatsApp")}
}
// Initialize is called by the engine once everything is loaded
func (h *handler) Initialize(s courier.Server) error {
h.SetServer(s)
err := s.AddHandlerRoute(h, http.MethodPost, "receive", h.ReceiveMessage)
if err != nil {
return err
}
return s.AddHandlerRoute(h, http.MethodPost, "status", h.UpdateStatus)
}
// {
// "meta": null,
// "payload": {
// "from": "16315555555",
// "message_id": "345b5e14775782",
// "timestamp": "1476225801",
// "message": {
// "address": "1 Hacker Way, Menlo Park, CA 94025",
// "latitude": 37.483253479003906,
// "longitude": -122.14960479736328,
// "has_media": true,
// "text": "This is the media caption.",
// "type": "image"
// }
// },
// "error": false
// }
type waReceive struct {
Payload struct {
From string `json:"from" validate:"required"`
MessageID string `json:"message_id" validate:"required"`
Timestamp string `json:"timestamp" validate:"required"`
Message struct {
HasMedia bool `json:"has_media"`
Address string `json:"address"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Text string `json:"text"`
Type string `json:"type" validate:"required"`
} `json:"message" validate:"required"`
} `json:"payload"`
Error bool `json:"error"`
}
// 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) ([]courier.Event, error) {
waReceive := &waReceive{}
err := handlers.DecodeAndValidateJSON(waReceive, r)
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, err)
}
// if this is an error, that's an erro
if waReceive.Error {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, fmt.Errorf("received errored message"))
}
// create our date from the timestamp
ts, err := strconv.ParseInt(waReceive.Payload.Timestamp, 10, 64)
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, fmt.Errorf("invalid timestamp: %s", waReceive.Payload.Timestamp))
}
date := time.Unix(ts, 0).UTC()
// create our URN
urn, err := urns.NewWhatsAppURN(waReceive.Payload.From)
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, err)
}
// ignore this if there's no message
if waReceive.Payload.Message.Text == "" {
return nil, courier.WriteAndLogRequestIgnored(ctx, w, r, channel, "ignoring, empty message")
}
// TODO: should we be hitting the API to look up contact information?
// TODO: deal with media messages
// TODO: deal with location messages
// build our msg
msg := h.Backend().NewIncomingMsg(channel, urn, waReceive.Payload.Message.Text).WithReceivedOn(date).WithExternalID(waReceive.Payload.MessageID)
// queue our message
err = h.Backend().WriteMsg(ctx, msg)
if err != nil {
return nil, err
}
return []courier.Event{msg}, courier.WriteMsgSuccess(ctx, w, r, []courier.Msg{msg})
}
// {
// "meta": null,
// "payload": {
// "message_id": "157b5e14568e8",
// "to": "16315555555",
// "timestamp": "1476225801",
// "message_status": "read"
// },
// "error": false
// }
type waStatus struct {
Payload struct {
MessageID string `json:"message_id" validate:"required"`
To string `json:"to" validate:"required"`
Timestamp string `json:"timestamp" validate:"required"`
MessageStatus string `json:"message_status" validate:"required"`
} `json:"payload"`
}
var waStatusMapping = map[string]courier.MsgStatusValue{
"sending": courier.MsgWired,
"sent": courier.MsgSent,
"delivered": courier.MsgDelivered,
"read": courier.MsgDelivered,
"failed": courier.MsgFailed,
}
// UpdateStatus is our HTTP handler function for status updates
func (h *handler) UpdateStatus(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request) ([]courier.Event, error) {
// get our params
waStatus := &waStatus{}
err := handlers.DecodeAndValidateJSON(waStatus, r)
if err != nil {
return nil, courier.WriteAndLogRequestError(ctx, w, r, channel, err)
}
msgStatus, found := waStatusMapping[waStatus.Payload.MessageStatus]
if !found {
return nil, courier.WriteAndLogRequestError(
ctx, w, r, channel,
fmt.Errorf("unknown status '%s', must be one of 'sending', 'sent', 'delivered', 'read' or 'failed'", waStatus.Payload.MessageStatus))
}
// if we have no status, then build it from the external (twilio) id
status := h.Backend().NewMsgStatusForExternalID(channel, waStatus.Payload.MessageID, msgStatus)
// write our status
err = h.Backend().WriteMsgStatus(ctx, status)
// we can receive read statuses for msgs we didn't trigger
if err == courier.ErrMsgNotFound {
return nil, courier.WriteAndLogStatusMsgNotFound(ctx, w, r, channel)
}
if err != nil {
return nil, err
}
return []courier.Event{status}, courier.WriteStatusSuccess(ctx, w, r, []courier.MsgStatus{status})
}
// {
// "payload": {
// "to": "16315555555",
// "body": "hello world"
// }
// }
type waPayload struct {
To string `json:"to" validate:"required"`
Body string `json:"body" validate:"required"`
}
// {
// "payload": {
// "message_id": "1015511",
// },
// "error": false
// }
type waSendResponse struct {
Payload struct {
MessageID string `json:"message_id" validate:"required"`
} `json:"payload"`
Error bool `json:"error"`
}
// whatsapp only allows messages up to 4096 chars
const maxMsgLength = 4096
// SendMsg sends the passed in message, returning any error
func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStatus, error) {
// get our username and password
username := msg.Channel().StringConfigForKey(courier.ConfigUsername, "")
if username == "" {
return nil, fmt.Errorf("no username set for WA channel")
}
password := msg.Channel().StringConfigForKey(courier.ConfigPassword, "")
if password == "" {
return nil, fmt.Errorf("no password set for WA channel")
}
urlStr := msg.Channel().StringConfigForKey(courier.ConfigBaseURL, "")
if urlStr == "" {
return nil, fmt.Errorf("no base url set for WA channel")
}
url, err := url.Parse(urlStr)
if err != nil {
return nil, fmt.Errorf("invalid base url set for WA channel: %s", err)
}
sendPath, _ := url.Parse("/api/rest_send.php")
sendURL := url.ResolveReference(sendPath).String()
// TODO: figure out sending media
status := h.Backend().NewMsgStatusForID(msg.Channel(), msg.ID(), courier.MsgErrored)
parts := handlers.SplitMsg(msg.Text(), maxMsgLength)
for i, part := range parts {
payload := waPayload{
To: msg.URN().Path(),
Body: part,
}
body := map[string]interface{}{"payload": payload}
jsonBody, err := json.Marshal(body)
if err != nil {
return status, err
}
req, err := http.NewRequest(http.MethodPost, sendURL, bytes.NewReader(jsonBody))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.SetBasicAuth(username, password)
rr, err := utils.MakeHTTPRequest(req)
// record our status and log
log := courier.NewChannelLogFromRR("Message Sent", msg.Channel(), msg.ID(), rr).WithError("Message Send Error", err)
status.AddLog(log)
if err != nil {
return status, nil
}
// was this an error?
wasError, err := jsonparser.GetBoolean([]byte(rr.Body), "error")
if err != nil || wasError {
log.WithError("Message Send Error", errors.Errorf("received error from send endpoint"))
return status, nil
}
// grab the id
externalID, err := jsonparser.GetString([]byte(rr.Body), "payload", "message_id")
if err != nil {
log.WithError("Message Send Error", errors.Errorf("unable to get message_id from body"))
return status, nil
}
// if this is our first message, record the external id
if i == 0 {
status.SetExternalID(externalID)
}
// this was wired successfully
status.SetStatus(courier.MsgWired)
}
return status, nil
}