-
Notifications
You must be signed in to change notification settings - Fork 11
/
bot.js
162 lines (128 loc) · 4.74 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
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
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
______ ______ ______ __ __ __ ______
/\ == \ /\ __ \ /\__ _\ /\ \/ / /\ \ /\__ _\
\ \ __< \ \ \/\ \ \/_/\ \/ \ \ _"-. \ \ \ \/_/\ \/
\ \_____\ \ \_____\ \ \_\ \ \_\ \_\ \ \_\ \ \_\
\/_____/ \/_____/ \/_/ \/_/\/_/ \/_/ \/_/
This is the DOREMUS Bot! Built with Botkit, using the Dialogflow middleware.
Authors:
- Luca LOMBARDO <lombardo@eurecom.fr>
- Claudio SCALZO <scalzo@eurecom.fr>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
// LOAD THE NECESSARY ENVIRONMENT VARIABLES IN THE .env FILE
const dotenv = require('dotenv');
['.env', './config/.env', '/config/.env'].forEach(path => dotenv.config({ path }));
// VARIABLES DECLARATION
var Botkit = require('botkit');
var request = require('request');
var http = require('http');
var logger = require('./logger.js');
// CHECKS FOR THE SLACK AND DIALOGFLOW TOKENS
if (!process.env.slackToken) {
console.error('Specify Slack token in environment');
process.exit(1);
}
if (!process.env.dialogflow) {
console.error('Specify Dialogflow token in environment');
process.exit(1);
}
if (!process.env.fbAccessToken || !process.env.fbVerifyToken || !process.env.fbAppSecret) {
console.error('Specify Facebook tokens in environment');
process.exit(1);
}
if (!process.env.PORT2) {
console.error('Specify PORT2 token in environment to launch dialogflow webhook');
process.exit(1);
}
// SLACK
var slackBotOptions = {
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
debug: false,
scopes: ['bot'],
};
var slackController = Botkit.slackbot(slackBotOptions);
var slackBot = slackController.spawn({
token: process.env.slackToken,
retry: 10,
});
// CLOSE THE BOT IF IT FAILS TO RECONNECT
slackController.on('rtm_reconnect_failed', function(bot) {
throw new Error('RTM failed to reconnect. Closing the bot...');
});
// FB MESSENGER
var fbBotOptions = {
debug: true,
log: true,
access_token: process.env.fbAccessToken,
verify_token: process.env.fbVerifyToken,
app_secret: process.env.fbAppSecret,
validate_requests: true,
};
var fbController = Botkit.facebookbot(fbBotOptions);
var fbBot = fbController.spawn({});
fbController.setupWebserver(
process.env.PORT || 5000,
(err, webserver) => fbController.createWebhookEndpoints(webserver, fbBot)
);
// LOAD 'SpellChecker' MIDDLEWARE
var spellCheckerMiddleware = require('./spell-checker-middleware.js')();
// LOAD 'Dialogflow' MIDDLEWARE
var dialogflowMiddleware = require('botkit-middleware-dialogflow')({
token: process.env.dialogflow,
sessionIdProps: ['channel'],
});
// WEBCHAT
var webchatBotOptions = {
replyWithTyping: true,
debug: false,
};
var webchatConfig = {
baseUrl: process.env.webchatBaseUrl || '/',
port: process.env.webchatPort
}
var webchatController = Botkit.socketbot(webchatBotOptions);
var webchatBot = webchatController.spawn({
token: process.env.webchatToken,
retry: 10,
});
// Set up an Express-powered webserver to expose oauth and webhook endpoints
require('./webchat/components/express_webserver.js')(
webchatController,
webchatConfig,
);
// Open the web socket server
webchatController.openSocketServer(webchatController.httpserver);
// Start the Web bot controller
webchatController.startTicking();
// SLACK: 'SpellChecker' MIDDLEWARE INIT
slackController.middleware.receive.use(spellCheckerMiddleware.receive);
// SLACK: 'Dialogflow' MIDDLEWARE INIT
slackController.middleware.receive.use(dialogflowMiddleware.receive);
// FACEBOOK: 'SpellChecker' MIDDLEWARE INIT
fbController.middleware.receive.use(spellCheckerMiddleware.receive);
// FACEBOOK: 'Dialogflow' MIDDLEWARE INIT
fbController.middleware.receive.use(dialogflowMiddleware.receive);
// WEBCHAT: 'SpellChecker' MIDDLEWARE INIT
webchatController.middleware.receive.use(spellCheckerMiddleware.receive);
// WEBCHAT: 'Dialogflow' MIDDLEWARE INIT
webchatController.middleware.receive.use(dialogflowMiddleware.receive);
// START THE LOGGER
log = logger();
// BOT: START THE SERVICE
slackBot.startRTM();
// EXPORTS
exports.slackController = slackController;
exports.fbController = fbController;
exports.webchatController = webchatController;
exports.slackBot = slackBot;
exports.webchatBot = webchatBot;
exports.dialogflowMiddleware = dialogflowMiddleware;
exports.spellCheckerMiddleware = spellCheckerMiddleware;
exports.log = log;
// IMPORT HEARS
var slackHears = require('./slack/slack_io.js');
var fbHears = require('./facebook/facebook_io.js');
var webchatHears = require('./webchat/webchat_io.js');
// WEBHOOK SERVER
require("./dialogflow/index.js")(process.env.PORT2);