-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook_bot_custom.js
executable file
·383 lines (310 loc) · 16.8 KB
/
facebook_bot_custom.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
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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Facebook bot built with Botkit.
This bot demonstrates many of the core features of Botkit:
* Connect to Facebook's Messenger APIs
* Receive messages based on "spoken" patterns
* Reply to messages
* Use the conversation system to ask questions
* Use the built in storage system to store and retrieve information
for a user.
# RUN THE BOT:
Follow the instructions here to set up your Facebook app and page:
-> https://developers.facebook.com/docs/messenger-platform/implementation
Run your bot from the command line:
page_token=<MY PAGE TOKEN> verify_token=<MY_VERIFY_TOKEN> node facebook_bot.js
Use localtunnel.me to make your bot available on the web:
lt --port 3000
# USE THE BOT:
Find your bot inside Facebook to send it a direct message.
Say: "Hello"
The bot will reply "Hello!"
Say: "who are you?"
The bot will tell you its name, where it running, and for how long.
Say: "Call me <nickname>"
Tell the bot your nickname. Now you are friends.
Say: "who am I?"
The bot will tell you your nickname, if it knows one for you.
Say: "shutdown"
The bot will ask if you are sure, and then shut itself down.
Make sure to invite your bot into other channels using /invite @<my bot>!
# EXTEND THE BOT:
Botkit has many features for building cool and useful bots!
Read all about it here:
-> http://howdy.ai/botkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if (!process.env.page_token) {
console.log('Error: Specify page_token in environment');
process.exit(1);
}
if (!process.env.verify_token) {
console.log('Error: Specify verify_token in environment');
process.exit(1);
}
var Botkit = require('./lib/Botkit.js');
var os = require('os');
var accountSid = process.env.TWILIO_ACCOUNT_SID;
var authToken = process.env.TWILIO_AUTH_TOKEN;
var controller = Botkit.facebookbot({
debug: true,
access_token: process.env.page_token,
verify_token: process.env.verify_token,
});
var client = require('twilio')(accountSid, authToken);
var bot = controller.spawn({});
controller.setupWebserver(process.env.port || 3000, function(err, webserver) {
controller.createWebhookEndpoints(webserver, bot, function() {
console.log('ONLINE!');
});
});
controller.hears(['hello', 'hi'], 'message_received', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (user && user.name) {
bot.reply(message, 'Hello ' + user.name + '!!');
} else {
bot.reply(message, 'Hello.');
}
});
});
controller.hears(['text my wife'], 'message_received', function(bot, message) {
bot.startConversation(message, function(err, convo) {
if (!err) {
convo.ask('Sure! What would you like to say?', function(response, convo) {
var textMessage = response.text;
client.messages.create({
body: textMessage,
to: process.env.TWILIO_TO_NUMBER,
from: process.env.TWILIO_FROM_NUMBER
}, function(err, message) {
process.stdout.write(message.sid);
});
});
}
});
});
controller.hears(['call me (.*)', 'my name is (.*)'], 'message_received', function(bot, message) {
var name = message.match[1];
controller.storage.users.get(message.user, function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.name = name;
controller.storage.users.save(user, function(err, id) {
bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
});
});
});
controller.hears(['what is your name', 'who are you'], 'message_received', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (user && user.name) {
bot.reply(message, 'Your name is ' + user.name);
} else {
bot.startConversation(message, function(err, convo) {
if (!err) {
convo.say('My name is Chappi');
convo.say('I do not know your name yet!');
convo.ask('What should I call you?', function(response, convo) {
convo.ask('You want me to call you ' + response.text + '? (yes/no)', [{
pattern: 'yes',
callback: function(response, convo) {
// since no further messages are queued after this,
// the conversation will end naturally with status == 'completed'
convo.next();
}
}, {
pattern: 'no',
callback: function(response, convo) {
// stop the conversation. this will cause it to end with status == 'stopped'
convo.stop();
}
}, {
default: true,
callback: function(response, convo) {
convo.repeat();
convo.next();
}
}]);
convo.next();
}, {
'key': 'nickname'
}); // store the results in a field called nickname
convo.on('end', function(convo) {
if (convo.status == 'completed') {
controller.storage.users.get(message.user, function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.name = convo.extractResponse('nickname');
controller.storage.users.save(user, function(err, id) {
bot.reply(message, 'Got it. I will call you ' + user.name + ' from now on.');
});
});
} else {
// this happens if the conversation ended prematurely for some reason
bot.reply(message, 'OK, nevermind!');
}
});
}
});
}
});
});
//order
controller.hears(['(.*)(get|want|order|would like)(.*)pizza(.*)'], 'message_received', function(bot, message) {
controller.storage.users.get(message.user, function(err, user) {
if (1 == 0) {
bot.reply(message, 'What kind of pizza would you like ' + user.name);
} else {
bot.startConversation(message, function(err, convo) {
if (user && user.name) {
convo.say('!');
convo.ask('What kind of pizza would you like?', function(response, convo) {
convo.ask('You want me to buy you ' + response.text + '?', [{
pattern: 'yes',
callback: function(response, convo) {
// since no further messages are queued after this,
// the conversation will end naturally with status == 'completed'
convo.next();
}
}, {
pattern: 'no',
callback: function(response, convo) {
// stop the conversation. this will cause it to end with status == 'stopped'
convo.stop();
}
}, {
default: true,
callback: function(response, convo) {
convo.repeat();
convo.next();
}
}]);
convo.next();
}, {
'key': 'choice'
}); // store the results in a field called choice
convo.on('end', function(convo) {
if (convo.status == 'completed') {
bot.reply(message, 'OK! I will get you that pizza...');
controller.storage.users.get(message.user, function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.pizzaType = convo.extractResponse('choice');
controller.storage.users.save(user, function(err, id) {
bot.reply(message, 'Here is the options for ' + user.pizzaType + ' you selected.');
});
if (user.pizzaType.toLowerCase().indexOf('pepperoni') > -1) { //(.*)(pepperoni)(.*)pizza(.*)
bot.reply(message, {
attachment: {
'type': 'template',
'payload': {
'template_type': 'generic',
'elements': [{
'title': 'Classic pepperoni pizza',
'image_url': 'https://cache.dominos.com/olo/3_16_1/assets/build/market/US/_en/images/img/products/thumbnails/S_PIZZA.jpg',
'subtitle': 'Dominos Online Ordering',
'buttons': [{
'type': 'web_url',
'url': 'https://www.dominos.com/en/pages/order/menu.jsp#/menu/category/all/',
'title': 'Place order'
}, {
'type': 'web_url',
'url': 'https://www.dominos.com/en/pages/order/menu.jsp#/menu/category/all/',
'title': 'Buy Item'
}, {
'type': 'postback',
'title': 'Bookmark Item',
'payload': 'Pepperoni Pizza'
}]
}, {
'title': 'PEPPERONI LOVERs',
'image_url': 'https://www.pizzahut.com/assets/w/tile/thor/Pepperoni_Lovers_Pizza.png',
'subtitle': 'Classic marinara sauce piled high with cheese and over 50% more authentic, old-world pepperoni hand-placed on your pizza',
'buttons': [{
'type': 'web_url',
'url': 'https://order.pizzahut.com/site/menu/pizza',
'title': 'View Item'
}, {
'type': 'web_url',
'url': 'https://order.pizzahut.com/site/menu/pizza',
'title': 'Buy Item'
}, {
'type': 'postback',
'title': 'Bookmark Item',
'payload': 'PEPPERONI'
}]
}]
}
}
});
} else if (user.pizzaType.toLowerCase().indexOf('cheese') > -1) {
bot.reply(message, {
attachment: {
'type': 'template',
'payload': {
'template_type': 'generic',
'elements': [{
'title': 'Classic cheese pizza',
'image_url': 'https://cdn.nexternal.com/cincyfav3/images/larosas_cheese_pizzas1.jpg',
'subtitle': 'Dominos Online Ordering',
'buttons': [{
'type': 'web_url',
'url': 'https://www.dominos.com/en/pages/order/menu.jsp#/menu/category/all/',
'title': 'Place order'
}, {
'type': 'web_url',
'url': 'https://www.dominos.com/en/pages/order/menu.jsp#/menu/category/all/',
'title': 'Buy Item'
}, {
'type': 'postback',
'title': 'Bookmark Item',
'payload': 'Pepperoni Pizza'
}]
}, {
'title': 'Cheese Pizza',
'image_url': 'http://cdn.schwans.com/media/images/products/56719-1-1540.jpg',
'subtitle': 'Classic cheese piled high with cheese',
'buttons': [{
'type': 'web_url',
'url': 'https://order.pizzahut.com/site/menu/pizza',
'title': 'View Item'
}, {
'type': 'web_url',
'url': 'https://order.pizzahut.com/site/menu/pizza',
'title': 'Buy Item'
}, {
'type': 'postback',
'title': 'Bookmark Item',
'payload': 'Cheese Pizza'
}]
}]
}
}
});
}
});
} else {
// this happens if the conversation ended prematurely for some reason
bot.reply(message, 'OK, nevermind!');
}
});
}
});
}
});
});
// controller.on('message_received', function(bot, message) {
// bot.reply(message, 'Chappi is glad he could help.');
// return false;
// });