-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (116 loc) · 3.41 KB
/
index.js
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
'use strict';
const async = require('async');
const request = require('request');
const config = require('./config');
const episodePicker = require('./episodePicker');
exports.router = (req, res) => {
switch (req.method) {
case 'GET':
handleGet(req, res);
break;
case 'POST':
handlePost(req, res);
break;
default:
res.status(500).send({
error: '👎'
});
}
};
const handleGet = (req, res) => {
const isSubscriptionMode = req.query['hub.mode'] === 'subscribe';
const hasValidToken = req.query['hub.verify_token'] === config.verifyToken;
if (isSubscriptionMode && hasValidToken) {
res.status(200).send(req.query['hub.challenge']);
} else {
res.status(403).send({error: '🚫'});
}
};
const handlePost = (req, res) => {
const data = req.body;
if (data.object === 'page') {
data.entry.forEach((entry) => {
const pageID = entry.id;
const timeOfEvent = entry.time;
entry.messaging.forEach((event) => {
if (event.message) {
receivedMessage(event);
} else {
console.log("Webhook received unknown event: ", event);
}
});
});
res.sendStatus(200);
}
};
function receivedMessage(event) {
const senderID = event.sender.id;
const recipientID = event.recipient.id;
const timeOfMessage = event.timestamp;
const message = event.message;
console.log(`Received message from ${senderID} at ${timeOfMessage}`);
console.log(JSON.stringify(message));
const messageId = message.mid;
const messageText = message.text;
const messageAttachments = message.attachments;
if (messageText) sendTextMessage(senderID, messageText);
else if (messageAttachments) sendTextMessage(senderID, messageAttachments);
}
function sendTextMessage(recipientId, messageText) {
const episode = episodePicker();
const message = {
"attachment": {
"type": "template",
"payload": {
"template_type": "list",
"top_element_style": "large",
"elements": [
{
"title": episode.title,
"image_url": episode.poster_url,
"subtitle": `Season ${episode.season}: Episode ${episode.number}`
},
{
"title": `${episode.time_of_day}\non a ${episode.weekday}`,
"subtitle": episode.description
}
],
"buttons": [
{
"type": "web_url",
"url": `https://netflix.com/watch/${episode.netflix_id}`,
"title": "Watch on Netflix"
}
]
}
}
};
const messageData = {
recipient: {
id: recipientId
},
message: message
};
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
method: 'POST',
json: messageData,
qs: {
access_token: config.facebookToken
}
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
const recipientId = body.recipient_id;
const messageId = body.message_id;
console.log(`Successfully sent message ${messageId} to recipient ${recipientId}`);
}
else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}