-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
114 lines (93 loc) · 4.65 KB
/
bot.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
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is a sample Facebook bot built with Botkit.
# 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 bot.js
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var env = require('node-env-file');
env(__dirname + '/.env');
if (!process.env.page_token) {
console.log('Error: Specify a Facebook page_token in environment.');
usage_tip();
process.exit(1);
}
if (!process.env.verify_token) {
console.log('Error: Specify a Facebook verify_token in environment.');
usage_tip();
process.exit(1);
}
var Botkit = require('botkit');
var debug = require('debug')('botkit:main');
// Create the Botkit controller, which controls all instances of the bot.
var controller = Botkit.facebookbot({
// debug: true,
receive_via_postback: true,
verify_token: process.env.verify_token,
access_token: process.env.page_token,
studio_token: process.env.studio_token,
studio_command_uri: process.env.studio_command_uri,
});
// Set up an Express-powered webserver to expose oauth and webhook endpoints
var webserver = require(__dirname + '/components/express_webserver.js')(controller);
// Tell Facebook to start sending events to this application
require(__dirname + '/components/subscribe_events.js')(controller);
// Set up Facebook "thread settings" such as get started button, persistent menu
require(__dirname + '/components/thread_settings.js')(controller);
// Send an onboarding message when a user activates the bot
require(__dirname + '/components/onboarding.js')(controller);
// Enable Dashbot.io plugin
require(__dirname + '/components/plugin_dashbot.js')(controller);
var normalizedPath = require("path").join(__dirname, "skills");
require("fs").readdirSync(normalizedPath).forEach(function(file) {
require("./skills/" + file)(controller);
});
// This captures and evaluates any message sent to the bot as a DM
// or sent to the bot in the form "@bot message" and passes it to
// Botkit Studio to evaluate for trigger words and patterns.
// If a trigger is matched, the conversation will automatically fire!
// You can tie into the execution of the script using the functions
// controller.studio.before, controller.studio.after and controller.studio.validate
if (process.env.studio_token) {
controller.on('message_received', function(bot, message) {
if (message.text) {
controller.studio.runTrigger(bot, message.text, message.user, message.channel).then(function(convo) {
if (!convo) {
// no trigger was matched
// If you want your bot to respond to every message,
// define a 'fallback' script in Botkit Studio
// and uncomment the line below.
controller.studio.run(bot, 'fallback', message.user, message.channel);
} else {
// set variables here that are needed for EVERY script
// use controller.studio.before('script') to set variables specific to a script
convo.setVar('current_time', new Date());
}
}).catch(function(err) {
if (err) {
bot.reply(message, 'I experienced an error with a request to Botkit Studio: ' + err);
debug('Botkit Studio: ', err);
}
});
}
});
} else {
console.log('~~~~~~~~~~');
console.log('NOTE: Botkit Studio functionality has not been enabled');
console.log('To enable, pass in a studio_token parameter with a token from https://studio.botkit.ai/');
}
function usage_tip() {
console.log('~~~~~~~~~~');
console.log('Botkit Studio Starter Kit');
console.log('Execute your bot application like this:');
console.log('page_token=<MY PAGE TOKEN> verify_token=<MY VERIFY TOKEN> studio_token=<MY BOTKIT STUDIO TOKEN> node bot.js');
console.log('Get Facebook token here: https://developers.facebook.com/docs/messenger-platform/implementation')
console.log('Get a Botkit Studio token here: https://studio.botkit.ai/')
console.log('~~~~~~~~~~');
}