-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
332 lines (307 loc) · 12.8 KB
/
index.js
File metadata and controls
332 lines (307 loc) · 12.8 KB
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
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()), // creates express http server
request = require('request'),
fetch = require('node-fetch'),
axios = require('axios');
const PAGE_ACCESS_TOKEN = "EAAisr0W2174BABxE3J7fOuItxSX9v6ZAeqtwQ5PV3MfEnkjxhTh7q4WZAA1hXuS0S48wyxxGM33xiCHEiakpCiknEoIqp5SOn4K5QxnnorPnVZAFYWRvyh85UelZBCllIbyQLuzqtqywNpPpvk2kieZAUFA5yETuXwbNZCRRgONgZDZD";
// Creates the endpoint for our webhook
let mainMenuResponse = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "We are here for you...",
"subtitle": "What do you want to do?",
// "image_url": attachment_url,
"buttons": [
{
"type": "postback",
"title": "See subjects",
"payload": "seeSubjects",
},
{
"type": "postback",
"title": "Create Resource",
"payload": "createResource",
},
// {
// "type": "postback",
// "title": "Get a Fashion tip",
// "payload": "fashionTip",
// },
// {
// "type": "postback",
// "title": "Film Industry",
// "payload": "celebrities",
// }
],
}]
}
}
}
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks 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 webhook_event = entry.messaging[0];
console.log(webhook_event);
// Get the sender PSID
let sender_psid = webhook_event.sender.id;
console.log('Sender PSID: ' + sender_psid);
request({
uri: 'https://graph.facebook.com/' + sender_psid + '?fields=locale&access_token=EAAisr0W2174BABxE3J7fOuItxSX9v6ZAeqtwQ5PV3MfEnkjxhTh7q4WZAA1hXuS0S48wyxxGM33xiCHEiakpCiknEoIqp5SOn4K5QxnnorPnVZAFYWRvyh85UelZBCllIbyQLuzqtqywNpPpvk2kieZAUFA5yETuXwbNZCRRgONgZDZD'
}, function (err2, res2, body2) {
body2 = JSON.parse(body2);
let locale = body2.locale;
// Check if the event is a message or postback and
// pass the event to the appropriate handler function
if (webhook_event.message) {
handleMessage(locale, sender_psid, webhook_event.message);
} else if (webhook_event.postback) {
handlePostback(locale, sender_psid, webhook_event.postback);
}
})
// // Check if the event is a message or postback and
// // pass the event to the appropriate handler function
// if (webhook_event.message) {
// handleMessage(sender_psid, webhook_event.message);
// } else if (webhook_event.postback) {
// handlePostback(sender_psid, webhook_event.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
app.get('/webhook', (req, res) => {
// Your verify token. Should be a random string.
let VERIFY_TOKEN = "wattbaisgreat"
// Parse the query params
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// 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);
}
}
});
function returnLessons (subjectNumber, sender_psid) {
let response;
request({
uri: 'http://wattba.h9ssxfia9b.us-west-2.elasticbeanstalk.com/api/quick/chatbot/lessons?subject_id=' + subjectNumber
}, function (err, res, body) {
body = JSON.parse(body);
var outputLessons = "";
console.log("lessonBOdy: ", body);
console.log("subject number is: ", subjectNumber);
let elements = [];
for (var i = 0; i < body.length; i++) {
// outputLessons += (i+1) + ". " + "Title: " + body[i].title + "\n" + "Content: " + body[i].content + "\n";
elements.push({
"title":body[i].title,
"image_url":body[i].image,
"subtitle":body[i].summary,
"default_action": {
"type": "web_url",
"url": "example.com",
},
"buttons": [
{
"type": "postback",
"title": "Know More",
"payload": "lesson_" + body[i].lesson_id
}
]
});
}
// response = {
// "text": outputLessons
// }
response = {
"attachment": {
"type": "template",
"payload": {
"template_type":"generic",
"elements": elements
}
}
}
callSendAPI(sender_psid, response)
})
}
// Handles messages events
function handleMessage(locale, sender_psid, received_message) {
let response;
console.log(received_message.text);
console.log('rm: ', received_message);
request({
headers: {
'Authorization': 'Bearer 6BBFKFZUO3HVNGL5U2ASTY56IHHWRNTA',
},
uri: 'https://api.wit.ai/message?v=20190429&q=' + encodeURI(received_message.text)
}, function( err, res, body) {
console.log('body is:', body);
body = JSON.parse(body);
if ((received_message.text).includes("http") || (received_message.text).includes("www")) {
response = {"text": "Thanks for the link. Generating content from your link."}
callSendAPI(sender_psid, response);
} else if (body["entities"]["subjects"] != undefined) {
request({
uri: 'http://wattba.h9ssxfia9b.us-west-2.elasticbeanstalk.com/api/v1/subjects/'
}, function (err2, res2, body2) {
body2 = JSON.parse(body2);
var outputSubjects = "";
for (var i = 0; i < body2.count; i++) {
outputSubjects += (i+1) + ". " + body2.results[i].name + "\n";
}
response = {
"text": outputSubjects
}
callSendAPI(sender_psid, response).then(() => {
response = {
"text": "Please enter subject number like 'subject 1' to get lessons for subject 1"
}
return callSendAPI(sender_psid, response);
});
})
} else if (body["entities"]["bye"] != undefined) {
response = {
"text": "Ok, see you later"
}
} else if (body["entities"]["number"] != undefined) {
console.log("subject: ", body);
returnLessons(parseInt(body.entities.number[0].value), sender_psid);
} else if (received_message.quick_reply != undefined ) {
let quickReplyPayload = received_message.quick_reply.payload;
let quickReplySubjectId = parseInt(quickReplyPayload.substring(10, ));
returnLessons(quickReplySubjectId, sender_psid)
} else {
response = {
"text": "How can i help you?"
}
callSendAPI(sender_psid, response).then(() => {
return callSendAPI(sender_psid, mainMenuResponse);
})
}
});
}
// Handles messaging_postbacks events
function handlePostback(locale, sender_psid, received_postback) {
let response;
// Get the payload for the postback
let payload = received_postback.payload;
console.log('r_payloafd:', payload);
// Set the response based on the postback payload
if (payload === "USER_DEFINED_PAYLOAD") { // which is for get_started
let responseText = "Welcome, great to see you. Together we’re building the world’s largest curriculum repository.";
response = {"text": responseText}
console.log('r1:', response);
if (locale != 'en_GB' && locale != 'en_US')
{
axios.post('http://18.236.191.192:3000/translate', {
"text": responseText,
"src_lang":"en",
"dst_lang": locale.substring(0,2)
})
.then((res) => {
console.log(res)
response = {"text": res.data.result}
callSendAPI(sender_psid, response).then(() => {
return callSendAPI(sender_psid, mainMenuResponse);
});
console.log('r2:', response);
})
.catch((error) => {
console.error(error)
})
} else {
console.log('r3:', response);
callSendAPI(sender_psid, response).then(() => {
return callSendAPI(sender_psid, mainMenuResponse);
});
}
} else if (payload.includes("lesson_")) { // which is for get_started
let lesson_id;
lesson_id = parseInt(payload.substring(7, ));
request({
uri: 'http://wattba.h9ssxfia9b.us-west-2.elasticbeanstalk.com/api/quick/chatbot/lessons/' + lesson_id + '/detail'
}, function (err2, res2, body2) {
body2 = JSON.parse(body2);
let responseText = "";
responseText += "Subject: " + body2.subject + "\n";
responseText += "Grade: " + body2.grade + "\n";
responseText += "Content: " + body2.content + "\n";
response = {
"text": responseText
}
callSendAPI(sender_psid, response);
})
} else if (payload == "seeSubjects") {
request({
uri: 'http://wattba.h9ssxfia9b.us-west-2.elasticbeanstalk.com/api/v1/subjects/'
}, function (err2, res2, body2) {
body2 = JSON.parse(body2);
let quick_replies = [];
for (var i = 0; i < body2.count; i++) {
quick_replies.push(
{
"content_type":"text",
"title":body2.results[i].name,
"payload":"subject_id" + body2.results[i].id
}
);
}
response = {
"text": "Choose from the subjects below",
"quick_replies":quick_replies
}
callSendAPI(sender_psid, response);
})
} else if (payload == "createResource") {
response = {
"text": "Send me a link to automatically generate the resource."
}
callSendAPI(sender_psid, response);
} else if (payload.includes("subject_id")) {
var res = parseInt(str.substring(10, ));
returnLessons(res, sender_psid);
}
}
// Sends response messages via the Send API
function callSendAPI(sender_psid, response) {
let body = {
recipient: {
"id": sender_psid
},
"message": response
};
const qs = 'access_token=' + encodeURIComponent(PAGE_ACCESS_TOKEN); // Here you'll need to add your PAGE TOKEN from Facebook
return fetch('https://graph.facebook.com/v2.6/me/messages?' + qs, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(body),
});
}
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () => console.log('webhook is listening'));