Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
342 lines (305 loc) · 8.63 KB
/
Copy pathindex.js
File metadata and controls
342 lines (305 loc) · 8.63 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
333
334
335
336
337
338
339
340
341
342
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var request = require('nodemon');
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
// Server frontpage
app.get('/', function (req, res) {
res.render('index.html');
});
app.get('/cases', function (req, res) {
res.render('cases.html');
});
// Facebook Webhook
app.get('/webhook', function (req, res) {
if (req.query['hub.verify_token'] === 'testbot_verify_token') {
res.send(req.query['hub.challenge']);
} else {
res.send('Invalid verify token');
}
});
// handler receiving messages
app.post('/webhook', function (req, res) {
var events = req.body.entry[0].messaging;
for (i = 0; i < events.length; i++) {
var event = events[i];
if (event.message && event.message.text) {
sendInitialMessage(event.sender.id);
} else if (event.postback) {
receivedPostback(event);
}
}
res.sendStatus(200);
});
/*
* Message Event
*
* This event is called when a message is sent to your page. The 'message'
* object format can vary depending on the kind of message that was received.
* Read more at https://developers.facebook.com/docs/messenger-platform/webhook-reference#received_message
*
*/
function receivedMessage(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfMessage = event.timestamp;
var message = event.message;
console.log("Received message for user %d and page %d at %d with message:",
senderID, recipientID, timeOfMessage);
console.log(JSON.stringify(message));
var messageId = message.mid;
// You may get a text or attachment but not both
var messageText = message.text;
var messageAttachments = message.attachments;
if (messageText) {
// If we receive a text message, check to see if it matches any special
// keywords and send back the corresponding example. Otherwise, just initial default message.
switch (messageText) {
case 'keyword_1':
sendInitialMessage(senderID);
break;
case 'keyword_2':
sendInitialMessage(senderID);
break;
default:
sendInitialMessage(senderID);
}
} else if (messageAttachments) {
sendTextMessage(senderID, "Message with attachment received");
}
}
/*
* Postback Event
*
* This event is called when a postback is tapped on a Structured Message. Read
* more at https://developers.facebook.com/docs/messenger-platform/webhook-reference#postback
*
*/
function receivedPostback(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfPostback = event.timestamp;
// The 'payload' param is a developer-defined field which is set in a postback
// button for Structured Messages.
var payload = event.postback.payload;
if (payload) {
// If we receive a payload, check to see if it matches any special
// keywords and send back the corresponding example. Otherwise, just echo
// the text we received.
switch (payload){
case 'yes_interested':
sendProductsMessage(senderID);
break;
case 'read_basic':
sendBasicTierMessage(senderID);
break;
case 'read_plus':
sendPlusTierMessage(senderID);
break;
case 'read_nlp':
sendNaturalTierMessage(senderID);
break;
case 'farewell':
sendFarewellMessage(senderID);
break;
default:
sendTextMessage(senderID, payload);
}
}
}
/*
* Send initial message using the Send API.
*
*/
function sendInitialMessage(recipientId) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "button",
text: "Hi, my name is Susana and Iam your automated assistant. Im guessing you are interested in our products. Is that right?",
buttons:[{
type: "postback",
title: "Sure :)",
payload: "yes_interested"
},{
type: "postback",
title: "mmm... not now",
payload: "farewell"
}]
}
}
}
};
callSendAPI(messageData);
}
/*
* Send a Structured Message with Campaigns landings using the Send API.
*
*/
function sendProductsMessage(recipientId) {
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: [
{
title: "Basic Tier",
subtitle: "Scripted generic chatBot",
item_url: "http://promocion.clinicaloyola.com.co/blefaroplastia",
image_url: "https://s3-sa-east-1.amazonaws.com/cannedhead.clinicaloyola/landing/junio/blefaro_mobile_junio.jpg",
buttons: [
{
type: "web_url",
url: "http://promocion.clinicaloyola.com.co/blefaroplastia",
title: "View on Web"
},{
type: "postback",
title: "Read Here",
payload: "read_basic",
}
],
},
{
title: "Plus Tier",
subtitle: "Custom designed scripted chatbot",
item_url: "http://promocion.clinicaloyola.com/abdominoplastia",
image_url: "https://s3-sa-east-1.amazonaws.com/cannedhead.clinicaloyola/landing/junio/abdo_mobile_junio.jpg",
buttons: [
{
type: "web_url",
url: "http://promocion.clinicaloyola.com/abdominoplastia",
title: "View on Web"
},{
type: "postback",
title: "Read Here",
payload: "read_plus",
}
]
},
{
title: "NLP Tier",
subtitle: "Natural Language Processing chatbot",
item_url: "http://promocion.clinicaloyola.com/implantesdentales",
image_url: "https://s3-sa-east-1.amazonaws.com/cannedhead.clinicaloyola/landing/junio/implantes_miniatura.jpg",
buttons: [
{
type: "web_url",
url: "http://promocion.clinicaloyola.com/implantesdentales",
title: "View on Web"
},{
type: "postback",
title: "Read Here",
payload: "read_nlp",
}
]
}
] // close elements
} // close payload
} // close attachment
} // close message
};
callSendAPI(messageData);
}
/*
* Send a farewell message using the Send API.
*
*/
function sendFarewellMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: 'Thanks for your attention :). Bye!'
}
};
callSendAPI(messageData);
}
/*
* Send basic plan message using the Send API.
*
*/
function sendBasicTierMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: 'The Basic Tier consist of a generic scripted bot. Much like this one, to generate leads and/or introduce your products to your facebook followers.'
}
};
callSendAPI(messageData);
}
/*
* Send plus plan message using the Send API.
*
*/
function sendPlusTierMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: 'The Plus Tier offers an scripted bot. The difference though, is that in this case the script is customized to your needs and requirements.'
}
};
callSendAPI(messageData);
}
/*
* Send natural plan message using the Send API.
*
*/
function sendNaturalTierMessage(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: 'The NLP Tier is to some extend also an scripted bot. The difference relies on the fact that this kind of bot can understand commands that exhibit natural language features, and is not bounded to exact commands and/or buttons.'
}
};
callSendAPI(messageData);
}
/*
* Call the Send API. The message data goes in the body. If successful, we'll
* get the message id in a response
*
*/
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token: process.env.PAGE_ACCESS_TOKEN},
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s",
messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
// Start server
// Webhooks must be available via SSL with a certificate signed by a valid
// certificate authority.
app.listen((process.env.PORT || 3000));