forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
72 lines (63 loc) · 2.18 KB
/
app.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
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var builder = require('botbuilder');
var restify = require('restify');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot and listen to messages
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
var DialogLabels = {
Hotels: 'Hotels',
Flights: 'Flights',
Support: 'Support'
};
var bot = new builder.UniversalBot(connector, [
function (session) {
// prompt for search option
builder.Prompts.choice(
session,
'Are you looking for a flight or a hotel?',
[DialogLabels.Flights, DialogLabels.Hotels],
{
maxRetries: 3,
retryPrompt: 'Not a valid option'
});
},
function (session, result) {
if (!result.response) {
// exhausted attemps and no selection, start over
session.send('Ooops! Too many attemps :( But don\'t worry, I\'m handling that exception and you can try again!');
return session.endDialog();
}
// on error, start over
session.on('error', function (err) {
session.send('Failed with message: %s', err.message);
session.endDialog();
});
// continue on proper dialog
var selection = result.response.entity;
switch (selection) {
case DialogLabels.Flights:
return session.beginDialog('flights');
case DialogLabels.Hotels:
return session.beginDialog('hotels');
}
}
]);
bot.dialog('flights', require('./flights'));
bot.dialog('hotels', require('./hotels'));
bot.dialog('support', require('./support'))
.triggerAction({
matches: [/help/i, /support/i, /problem/i]
});
// log any bot errors into the console
bot.on('error', function (e) {
console.log('And error ocurred', e);
});