-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathhandler.go
648 lines (541 loc) · 19.7 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package facebook_legacy
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"net/url"
"strings"
"time"
"github.com/buger/jsonparser"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/utils"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/urns"
)
// Endpoints we hit
var (
sendURL = "https://graph.facebook.com/v3.3/me/messages"
subscribeURL = "https://graph.facebook.com/v3.3/me/subscribed_apps"
graphURL = "https://graph.facebook.com/v3.3/"
// How long we want after the subscribe callback to register the page for events
subscribeTimeout = time.Second * 2
// Facebook API says 640 is max for the body
maxMsgLength = 640
// Sticker ID substitutions
stickerIDToEmoji = map[int64]string{
369239263222822: "👍", // small
369239343222814: "👍", // medium
369239383222810: "👍", // big
}
tagByTopic = map[string]string{
"event": "CONFIRMED_EVENT_UPDATE",
"purchase": "POST_PURCHASE_UPDATE",
"account": "ACCOUNT_UPDATE",
"agent": "HUMAN_AGENT",
}
)
// keys for extra in channel events
const (
referrerIDKey = "referrer_id"
sourceKey = "source"
adIDKey = "ad_id"
typeKey = "type"
titleKey = "title"
payloadKey = "payload"
)
func init() {
courier.RegisterHandler(newHandler())
}
type handler struct {
handlers.BaseHandler
}
func newHandler() courier.ChannelHandler {
return &handler{handlers.NewBaseHandler(courier.ChannelType("FB"), "Facebook")}
}
// 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.ChannelLogTypeMultiReceive, handlers.JSONPayload(h, h.receiveEvents))
s.AddHandlerRoute(h, http.MethodGet, "receive", courier.ChannelLogTypeWebhookVerify, h.receiveVerify)
return nil
}
// receiveVerify handles Facebook's webhook verification callback
func (h *handler) receiveVerify(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, clog *courier.ChannelLog) ([]courier.Event, error) {
mode := r.URL.Query().Get("hub.mode")
// this isn't a subscribe verification, that's an error
if mode != "subscribe" {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("unknown request"))
}
// verify the token against our secret, if the same return the challenge FB sent us
secret := r.URL.Query().Get("hub.verify_token")
if secret != channel.StringConfigForKey(courier.ConfigSecret, "") {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("token does not match secret"))
}
// make sure we have an auth token
authToken := channel.StringConfigForKey(courier.ConfigAuthToken, "")
if authToken == "" {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, fmt.Errorf("missing auth token for FB channel"))
}
// everything looks good, we will subscribe to this page's messages asynchronously
go func() {
// wait a bit for Facebook to handle this response
time.Sleep(subscribeTimeout)
h.subscribeToEvents(ctx, channel, authToken)
}()
// and respond with the challenge token
_, err := fmt.Fprint(w, r.URL.Query().Get("hub.challenge"))
return nil, err
}
func (h *handler) subscribeToEvents(ctx context.Context, channel courier.Channel, authToken string) {
clog := courier.NewChannelLog(courier.ChannelLogTypePageSubscribe, channel, h.RedactValues(channel))
// subscribe to messaging events for this page
form := url.Values{}
form.Set("access_token", authToken)
req, _ := http.NewRequest(http.MethodPost, subscribeURL, strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, respBody, err := h.RequestHTTP(req, clog)
// log if we get any kind of error
success, _ := jsonparser.GetBoolean(respBody, "success")
if err != nil || resp.StatusCode/100 != 2 || !success {
slog.Error("error subscribing to Facebook page events", "channel_uuid", channel.UUID())
}
h.Backend().WriteChannelLog(ctx, clog)
}
type fbUser struct {
ID string `json:"id"`
}
// {
// "object":"page",
// "entry":[{
// "id":"180005062406476",
// "time":1514924367082,
// "messaging":[
// {
// "sender": {"id":"1630934236957797"},
// "recipient":{"id":"180005062406476"},
// "timestamp":1514924366807,
// "message":{
// "mid":"mid.$cAAD5QiNHkz1m6cyj11guxokwkhi2",
// "seq":33116,
// "text":"65863634"
// }
// }
// ]
// }]
// }
type moPayload struct {
Object string `json:"object"`
Entry []struct {
ID string `json:"id"`
Time int64 `json:"time"`
Messaging []struct {
Sender fbUser `json:"sender"`
Recipient fbUser `json:"recipient"`
Timestamp int64 `json:"timestamp"`
OptIn *struct {
Ref string `json:"ref"`
UserRef string `json:"user_ref"`
} `json:"optin"`
Referral *struct {
Ref string `json:"ref"`
Source string `json:"source"`
Type string `json:"type"`
AdID string `json:"ad_id"`
} `json:"referral"`
Postback *struct {
Title string `json:"title"`
Payload string `json:"payload"`
Referral struct {
Ref string `json:"ref"`
Source string `json:"source"`
Type string `json:"type"`
AdID string `json:"ad_id"`
} `json:"referral"`
} `json:"postback"`
Message *struct {
IsEcho bool `json:"is_echo"`
MID string `json:"mid"`
Text string `json:"text"`
StickerID int64 `json:"sticker_id"`
Attachments []struct {
Type string `json:"type"`
Payload *struct {
URL string `json:"url"`
StickerID int64 `json:"sticker_id"`
Coordinates *struct {
Lat float64 `json:"lat"`
Long float64 `json:"long"`
} `json:"coordinates"`
}
} `json:"attachments"`
} `json:"message"`
Delivery *struct {
MIDs []string `json:"mids"`
Watermark int64 `json:"watermark"`
Seq int `json:"seq"`
} `json:"delivery"`
} `json:"messaging"`
} `json:"entry"`
}
// receiveEvents is our HTTP handler function for incoming messages and status updates
func (h *handler) receiveEvents(ctx context.Context, channel courier.Channel, w http.ResponseWriter, r *http.Request, payload *moPayload, clog *courier.ChannelLog) ([]courier.Event, error) {
// not a page object? ignore
if payload.Object != "page" {
return nil, handlers.WriteAndLogRequestIgnored(ctx, h, channel, w, r, "ignoring non-page request")
}
// no entries? ignore this request
if len(payload.Entry) == 0 {
return nil, handlers.WriteAndLogRequestIgnored(ctx, h, channel, w, r, "ignoring request, no entries")
}
// the list of events we deal with
events := make([]courier.Event, 0, 2)
// the list of data we will return in our response
data := make([]any, 0, 2)
seenMsgIDs := make(map[string]bool, 2)
// for each entry
for _, entry := range payload.Entry {
// no entry, ignore
if len(entry.Messaging) == 0 {
continue
}
// grab our message, there is always a single one
msg := entry.Messaging[0]
// ignore this entry if it is to another page
if channel.Address() != msg.Recipient.ID {
continue
}
// create our date from the timestamp (they give us millis, arg is nanos)
date := time.Unix(0, msg.Timestamp*1000000).UTC()
// create our URN
urn, err := urns.New(urns.Facebook, msg.Sender.ID)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, errors.New("invalid facebook id"))
}
if msg.OptIn != nil {
// this is an opt in, if we have a user_ref, use that as our URN (this is a checkbox plugin)
// TODO:
// We need to deal with the case of them responding and remapping the user_ref in that case:
// https://developers.facebook.com/docs/messenger-platform/discovery/checkbox-plugin
// Right now that we even support this isn't documented and I don't think anybody uses it, so leaving that out.
// (things will still work, we just will have dupe contacts, one with user_ref for the first contact, then with the real id when they reply)
if msg.OptIn.UserRef != "" {
urn, err = urns.New(urns.Facebook, urns.FacebookRefPrefix+msg.OptIn.UserRef)
if err != nil {
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
}
}
event := h.Backend().NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithOccurredOn(date)
// build our extra
extra := map[string]string{referrerIDKey: msg.OptIn.Ref}
event = event.WithExtra(extra)
err := h.Backend().WriteChannelEvent(ctx, event, clog)
if err != nil {
return nil, err
}
events = append(events, event)
data = append(data, courier.NewEventReceiveData(event))
} else if msg.Postback != nil {
// by default postbacks are treated as new conversations, unless we have referral information
eventType := courier.EventTypeNewConversation
if msg.Postback.Referral.Ref != "" {
eventType = courier.EventTypeReferral
}
event := h.Backend().NewChannelEvent(channel, eventType, urn, clog).WithOccurredOn(date)
// build our extra
extra := map[string]string{
titleKey: msg.Postback.Title,
payloadKey: msg.Postback.Payload,
}
// add in referral information if we have it
if eventType == courier.EventTypeReferral {
extra[referrerIDKey] = msg.Postback.Referral.Ref
extra[sourceKey] = msg.Postback.Referral.Source
extra[typeKey] = msg.Postback.Referral.Type
if msg.Postback.Referral.AdID != "" {
extra[adIDKey] = msg.Postback.Referral.AdID
}
}
event = event.WithExtra(extra)
err := h.Backend().WriteChannelEvent(ctx, event, clog)
if err != nil {
return nil, err
}
events = append(events, event)
data = append(data, courier.NewEventReceiveData(event))
} else if msg.Referral != nil {
// this is an incoming referral
event := h.Backend().NewChannelEvent(channel, courier.EventTypeReferral, urn, clog).WithOccurredOn(date)
// build our extra
extra := map[string]string{sourceKey: msg.Referral.Source, typeKey: msg.Referral.Type}
// add referrer id if present
if msg.Referral.Ref != "" {
extra[referrerIDKey] = msg.Referral.Ref
}
// add ad id if present
if msg.Referral.AdID != "" {
extra[adIDKey] = msg.Referral.AdID
}
event = event.WithExtra(extra)
err := h.Backend().WriteChannelEvent(ctx, event, clog)
if err != nil {
return nil, err
}
events = append(events, event)
data = append(data, courier.NewEventReceiveData(event))
} else if msg.Message != nil {
// this is an incoming message
if seenMsgIDs[msg.Message.MID] {
continue
}
// ignore echos
if msg.Message.IsEcho {
data = append(data, courier.NewInfoData("ignoring echo"))
continue
}
text := msg.Message.Text
attachmentURLs := make([]string, 0, 2)
// loop on our attachments
for _, att := range msg.Message.Attachments {
// if we have a sticker ID, use that as our text
if att.Type == "image" && att.Payload != nil && att.Payload.StickerID != 0 {
text = stickerIDToEmoji[att.Payload.StickerID]
}
if att.Type == "location" {
attachmentURLs = append(attachmentURLs, fmt.Sprintf("geo:%f,%f", att.Payload.Coordinates.Lat, att.Payload.Coordinates.Long))
}
if att.Payload != nil && att.Payload.URL != "" && att.Type != "fallback" && strings.HasPrefix(att.Payload.URL, "http") {
attachmentURLs = append(attachmentURLs, att.Payload.URL)
}
}
// if we have a sticker ID, use that as our text
stickerText := stickerIDToEmoji[msg.Message.StickerID]
if stickerText != "" {
text = stickerText
}
// if we have no text or accepted attachments, don't create a message
if text == "" && len(attachmentURLs) == 0 {
continue
}
// create our message
event := h.Backend().NewIncomingMsg(channel, urn, text, msg.Message.MID, clog).WithReceivedOn(date)
// add any attachment URL found
for _, attURL := range attachmentURLs {
event.WithAttachment(attURL)
}
err := h.Backend().WriteMsg(ctx, event, clog)
if err != nil {
return nil, err
}
events = append(events, event)
data = append(data, courier.NewMsgReceiveData(event))
seenMsgIDs[msg.Message.MID] = true
} else if msg.Delivery != nil {
// this is a delivery report
for _, mid := range msg.Delivery.MIDs {
event := h.Backend().NewStatusUpdateByExternalID(channel, mid, courier.MsgStatusDelivered, clog)
err := h.Backend().WriteStatusUpdate(ctx, event)
if err != nil {
return nil, err
}
events = append(events, event)
data = append(data, courier.NewStatusData(event))
}
} else {
data = append(data, courier.NewInfoData("ignoring unknown entry type"))
}
}
return events, courier.WriteDataResponse(w, http.StatusOK, "Events Handled", data)
}
// {
// "messaging_type": "<MESSAGING_TYPE>"
// "recipient": {
// "id":"<PSID>"
// },
// "message": {
// "text":"hello, world!"
// "attachment":{
// "type":"image",
// "payload":{
// "url":"http://www.messenger-rocks.com/image.jpg",
// "is_reusable":true
// }
// }
// }
// }
type mtPayload struct {
MessagingType string `json:"messaging_type"`
Tag string `json:"tag,omitempty"`
Recipient struct {
UserRef string `json:"user_ref,omitempty"`
ID string `json:"id,omitempty"`
} `json:"recipient"`
Message struct {
Text string `json:"text,omitempty"`
QuickReplies []mtQuickReply `json:"quick_replies,omitempty"`
Attachment *mtAttachment `json:"attachment,omitempty"`
} `json:"message"`
}
type mtAttachment struct {
Type string `json:"type"`
Payload struct {
URL string `json:"url"`
IsReusable bool `json:"is_reusable"`
} `json:"payload"`
}
type mtQuickReply struct {
Title string `json:"title"`
Payload string `json:"payload"`
ContentType string `json:"content_type"`
}
func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.SendResult, clog *courier.ChannelLog) error {
accessToken := msg.Channel().StringConfigForKey(courier.ConfigAuthToken, "")
if accessToken == "" {
return courier.ErrChannelConfig
}
topic := msg.Topic()
payload := mtPayload{}
// set our message type
if msg.ResponseToExternalID() != "" {
payload.MessagingType = "RESPONSE"
} else if topic != "" {
payload.MessagingType = "MESSAGE_TAG"
payload.Tag = tagByTopic[topic]
} else {
payload.MessagingType = "NON_PROMOTIONAL_SUBSCRIPTION" // only allowed until Jan 15, 2020
}
// build our recipient
if IsFacebookRef(msg.URN()) {
payload.Recipient.UserRef = FacebookRef(msg.URN())
} else {
payload.Recipient.ID = msg.URN().Path()
}
msgURL, _ := url.Parse(sendURL)
query := url.Values{}
query.Set("access_token", accessToken)
msgURL.RawQuery = query.Encode()
msgParts := make([]string, 0)
if msg.Text() != "" {
msgParts = handlers.SplitMsgByChannel(msg.Channel(), msg.Text(), maxMsgLength)
}
// send each part and each attachment separately. we send attachments first as otherwise quick replies
// attached to text messages get hidden when images get delivered
for i := 0; i < len(msgParts)+len(msg.Attachments()); i++ {
if i < len(msg.Attachments()) {
// this is an attachment
payload.Message.Attachment = &mtAttachment{}
attType, attURL := handlers.SplitAttachment(msg.Attachments()[i])
attType = strings.Split(attType, "/")[0]
if attType == "application" {
attType = "file"
}
payload.Message.Attachment.Type = attType
payload.Message.Attachment.Payload.URL = attURL
payload.Message.Attachment.Payload.IsReusable = true
payload.Message.Text = ""
} else {
// this is still a msg part
payload.Message.Text = msgParts[i-len(msg.Attachments())]
payload.Message.Attachment = nil
}
// include any quick replies on the last piece we send
if i == (len(msgParts)+len(msg.Attachments()))-1 {
for _, qr := range msg.QuickReplies() {
payload.Message.QuickReplies = append(payload.Message.QuickReplies, mtQuickReply{qr.Text, qr.Text, "text"})
}
} else {
payload.Message.QuickReplies = nil
}
jsonBody := jsonx.MustMarshal(payload)
req, err := http.NewRequest(http.MethodPost, msgURL.String(), bytes.NewReader(jsonBody))
if err != nil {
return err
}
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
}
externalID, err := jsonparser.GetString(respBody, "message_id")
if err != nil {
return courier.ErrResponseUnexpected
}
res.AddExternalID(externalID)
if IsFacebookRef(msg.URN()) {
recipientID, err := jsonparser.GetString(respBody, "recipient_id")
if err != nil {
return courier.ErrResponseUnexpected
}
referralID := FacebookRef(msg.URN())
realIDURN, err := urns.New(urns.Facebook, recipientID)
if err != nil {
clog.RawError(fmt.Errorf("unable to make facebook urn from %s", recipientID))
}
contact, err := h.Backend().GetContact(ctx, msg.Channel(), msg.URN(), nil, "", clog)
if err != nil {
clog.RawError(fmt.Errorf("unable to get contact for %s", msg.URN().String()))
}
realURN, err := h.Backend().AddURNtoContact(ctx, msg.Channel(), contact, realIDURN, nil)
if err != nil {
clog.RawError(fmt.Errorf("unable to add real facebook URN %s to contact with uuid %s", realURN.String(), contact.UUID()))
}
referralIDExtURN, err := urns.New(urns.External, referralID)
if err != nil {
clog.RawError(fmt.Errorf("unable to make ext urn from %s", referralID))
}
extURN, err := h.Backend().AddURNtoContact(ctx, msg.Channel(), contact, referralIDExtURN, nil)
if err != nil {
clog.RawError(fmt.Errorf("unable to add URN %s to contact with uuid %s", extURN.String(), contact.UUID()))
}
referralFacebookURN, err := h.Backend().RemoveURNfromContact(ctx, msg.Channel(), contact, msg.URN())
if err != nil {
clog.RawError(fmt.Errorf("unable to remove referral facebook URN %s from contact with uuid %s", referralFacebookURN.String(), contact.UUID()))
}
}
}
return nil
}
// DescribeURN looks up URN metadata for new contacts
func (h *handler) DescribeURN(ctx context.Context, channel courier.Channel, urn urns.URN, clog *courier.ChannelLog) (map[string]string, error) {
// can't do anything with facebook refs, ignore them
if IsFacebookRef(urn) {
return map[string]string{}, nil
}
accessToken := channel.StringConfigForKey(courier.ConfigAuthToken, "")
if accessToken == "" {
return nil, fmt.Errorf("missing access token")
}
// build a request to lookup the stats for this contact
base, _ := url.Parse(graphURL)
path, _ := url.Parse(fmt.Sprintf("/%s", urn.Path()))
u := base.ResolveReference(path)
query := url.Values{}
query.Set("fields", "first_name,last_name")
query.Set("access_token", accessToken)
u.RawQuery = query.Encode()
req, _ := http.NewRequest(http.MethodGet, u.String(), nil)
resp, respBody, err := h.RequestHTTP(req, clog)
if err != nil || resp.StatusCode/100 != 2 {
return nil, errors.New("unable to look up contact data")
}
// read our first and last name
firstName, _ := jsonparser.GetString(respBody, "first_name")
lastName, _ := jsonparser.GetString(respBody, "last_name")
return map[string]string{"name": utils.JoinNonEmpty(" ", firstName, lastName)}, nil
}
func IsFacebookRef(u urns.URN) bool {
return u.Scheme() == urns.Facebook.Prefix && strings.HasPrefix(u.Path(), urns.FacebookRefPrefix)
}
// FacebookRef returns the facebook referral portion of our path, this return empty string in the case where we aren't a Facebook scheme
func FacebookRef(u urns.URN) string {
if IsFacebookRef(u) {
return strings.TrimPrefix(u.Path(), urns.FacebookRefPrefix)
}
return ""
}