-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
354 lines (284 loc) · 11.2 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
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
/*
* Copyright (c) 2020, Psiphon Inc.
* Released under the MIT license.
*/
'use strict';
// Imports dependencies and set up https server
const
msgTemplate = require("./messages.json"),
express = require("express"),
bodyParser = require("body-parser"),
fs = require("fs"),
https = require("https"),
app = express().use(bodyParser.json()); // creates express http server
require('dotenv').config();
const privkeyPath = process.env.PRIVKEYPATH;
const fullChainPem = process.env.FULLCHAINPEMPATH;
const serverPort = process.env.PORT;
// Your verify token. Should be a random string.
const verifyToken = process.env.WEBTOKEN;
// const accessToken = process.env.ACCESSTOKEN;
const accessToken = process.env.ACCESSTOKEN;
// Facebook API URL to send POST requests to.
// hostNamePath can be changed for new versions of the API.
const hostNameFB = "graph.facebook.com";
const hostNamePath = "/v7.0/me/messages?access_token=";
// Creates the endpoint for our webhook
// Reference (step 3) - https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks if this is an event from a page subscription
if (body.object === "page") {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
let webhookEvent = entry.messaging[0];
// Gets the Sender ID to be able to send messages to sender in messenger API
let senderPsid = webhookEvent.sender.id;
// Handle Messenger API events
if (webhookEvent.message) {
handleMessage(senderPsid, webhookEvent.message);
} else if (webhookEvent.postback) {
handlePostback(senderPsid, webhookEvent.postback);
}
});
// Returns a '200 OK' response to all requests
res.status(200).send("EVENT_RECEIVED");
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
// Adds support for GET requests to our webhook
// Reference (step 4) - https://developers.facebook.com/docs/messenger-platform/getting-started/webhook-setup
app.get('/webhook', (req, res) => {
// Parse the query params
const mode = req.query["hub.mode"];
const token = req.query["hub.verify_token"];
const challenge = req.query["hub.challenge"];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
console.log("mode & token are confirmed");
// Checks the mode and token sent is correct
if (mode === "subscribe" && token === verifyToken) {
// Responds with the challenge token from the request
console.log("WEBHOOK_VERIFIED");
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});
// Create the server to listen to webhook events on
https.createServer({
key: fs.readFileSync(privkeyPath),
cert: fs.readFileSync(fullChainPem)
}, app).listen(serverPort, function() {
console.log('The Server is open and we are listening');
});
// Sends messages of any type to user via the Send API by FaceBook
// Reference: https://developers.facebook.com/docs/messenger-platform/reference/send-api
function callSendAPI(senderPsid, response) {
let requestBody = {
recipient: {
id: senderPsid,
},
message: response,
};
// access path to make POST requests to - SendAPI URL
const accessPath = hostNamePath + accessToken;
// configure webhook options
const options = {
messaging_type: "RESPONSE",
hostname: hostNameFB,
path: accessPath,
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
// Send https request to messenger platform
let req = https.request(options, (res) => {
console.log("Status code:", res.statusCode);
console.log("Status code message:", res.statusMessage);
}).on("error", (err) => {
console.log("error:", err.message);
});
req.write(JSON.stringify(requestBody));
console.log("sent the message!");
req.end();
}
// Handles messages events which is the Facebook name for any type of user input in messenger.
// Reference: https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messages
function handleMessage(senderPsid, received_message) {
let response;
let text = received_message.text;
// Use the quick reply function to handle these message postbacks.
// Quick replies are a type of FB messenger button
if (received_message.quick_reply) {
handleQuickReply(senderPsid, received_message.quick_reply);
return;
} else if (received_message.text && ["HI", "HELLO"].includes(text.toUpperCase())) {
console.log("recieved hi/hello");
response = {
text: msgTemplate.prompts.greeting,
quick_replies: [{
content_type: "text",
title: msgTemplate.qk_replies.what,
payload: "what-is-psiphon",
},
{
content_type: "text",
title: msgTemplate.qk_replies.download,
payload: "download-psiphon-1",
},
{
content_type: "text",
title: msgTemplate.qk_replies.connect,
payload: "connection-problems-1",
}
],
}
console.info(response.quick_replies);
} else {
// If the message sent from the user is none of the above then we prompt them to select "Options" to receive the "Options" menu.
response = {
text: msgTemplate.qk_responses.error,
quick_replies: [{
content_type: "text",
title: "Options",
payload: "yes-help",
}, ]
};
}
callSendAPI(senderPsid, response);
return;
}
// Handles messaging_postbacks events
// Reference: "https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/messaging_postbacks/"
function handlePostback(senderPsid, received_postback) {
// Get the payload for the postback
let payload = received_postback.payload;
// Set the response based on the postback payload
if (payload === "download-psiphon-1") {
let response = {
text: msgTemplate.qk_responses["download-resp"]
};
callSendAPI(senderPsid, response);
sendHelpMsg(senderPsid, response);
} else if (payload === "what-is-psiphon") {
let response = {
text: msgTemplate.qk_responses["what-resp-1"]
};
let response2 = {
text: msgTemplate.qk_responses["what-resp-2"]
};
callSendAPI(senderPsid, response);
setTimeout(() => callSendAPI(senderPsid, response2), 1000);
sendHelpMsg(senderPsid, [response, response2]);
// sendHelpMsg(senderPsid, [
// { text: msgTemplate.qk_responses["what-resp-1"] },
// { text: msgTemplate.qk_responses["what-resp-2"] }
// ]);
} else if (payload === "connection-problems-1") {
let response = {
text: msgTemplate.qk_responses["connect-resp-1"]
};
let response2 = {
text: msgTemplate.qk_responses["connect-resp-2"]
};
callSendAPI(senderPsid, response);
setTimeout(() => callSendAPI(senderPsid, response2), 1000);
sendHelpMsg(senderPsid, [response2, response]);
// sendHelpMsg(senderPsid, [
// { text: msgTemplate.qk_responses["connect-resp-1"] },
// { text: msgTemplate.qk_responses["connect-resp-2"] }
// ]);
}
}
// Function to handle quick replies postbacks - the response sent after a user chooses one of our quick replies.
// Payloads are attributes in postback events used to identify which quick reply was chosen.
// Reference: https://developers.facebook.com/docs/messenger-platform/reference/buttons/quick-replies
function handleQuickReply(senderPsid, received_message) {
let payload = received_message.payload;
if (payload === "yes-help") {
sendMainMenu(senderPsid);
} else if (payload === "no-help") {
let response = {
text: msgTemplate.prompts["end-msg"]
};
callSendAPI(senderPsid, response);
} else if (payload === "what-is-psiphon") {
let response = {
text: msgTemplate.qk_responses["what-resp-1"]
};
let response2 = {
text: msgTemplate.qk_responses["what-resp-2"]
};
callSendAPI(senderPsid, response);
setTimeout(() => callSendAPI(senderPsid, response2), 1000);
sendHelpMsg(senderPsid, [response, response2]);
} else if (payload === "download-psiphon-1") {
let response = {
text: msgTemplate.qk_responses["download-resp"]
};
callSendAPI(senderPsid, response);
sendHelpMsg(senderPsid, response);
} else if (payload === "connection-problems-1") {
let response = {
text: msgTemplate.qk_responses["connect-resp-1"]
};
let response2 = {
text: msgTemplate.qk_responses["connect-resp-2"]
};
callSendAPI(senderPsid, response);
setTimeout(() => callSendAPI(senderPsid, response2), 1000);
sendHelpMsg(senderPsid, [response2, response]);
} else {
callSendAPI(senderPsid, {
text: msgTemplate.qk_responses.error
});
sendMainMenu(senderPsid);
}
}
// Main menu is a our core set of quick reply options.
function sendMainMenu(senderPsid) {
const response = {
text: msgTemplate.prompts.greeting,
quick_replies: [{
content_type: "text",
title: msgTemplate.qk_replies.what,
payload: "what-is-psiphon",
},
{
content_type: "text",
title: msgTemplate.qk_replies.download,
payload: "download-psiphon-1",
},
{
content_type: "text",
title: msgTemplate.qk_replies.connect,
payload: "connection-problems-1",
}
],
}
callSendAPI(senderPsid, response);
}
// This function adds a message to the all of the quick reply messages asking the user if they need any more help and giving them a yes/no option which will then give them the main menu again if they select yes. This message asking if they need more help is sent 5 seconds after the first message/messages.
function sendHelpMsg(senderPsid, response) {
let response3 = {
text: msgTemplate.prompts.help,
quick_replies: [{
content_type: "text",
title: "Yes",
payload: "yes-help",
}, {
content_type: "text",
title: "No",
payload: "no-help",
}, ]
}
setTimeout(() => callSendAPI(senderPsid, response3), 5000);
}