-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (135 loc) · 5.11 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
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
var restify = require('restify');
var builder = require('botbuilder');
var api = require('./api_wrapper');
var config = require('./config');
var util = require('./util');
//=========================================================
// Bot Setup
//=========================================================
// Setup Restify Server
var server = restify.createServer();
exports.server = server;
server.listen(config.port, () => {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat bot
var connector = new builder.ChatConnector({
appId: config.MSAppId,
appPassword: config.MSAppPassword,
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
// Static root page
server.get('/', (req, res) => {
res.send(200, `API Version ${config.apiVersion} currently running.`);
});
// Setup Capital One
util.resetDemo();
server.get('/resetDemo', util.resetDemo);
// LUIS Setup
var model = `https://api.projectoxford.ai/luis/v1/application?id=${config.LuisAppId}&subscription-key=${config.LuisSubscriptionKey}`;
var recognizer = new builder.LuisRecognizer(model);
var dialog = new builder.IntentDialog({ recognizers: [recognizer] });
bot.dialog('/', dialog);
//=========================================================
// Bots Dialogs
//=========================================================
dialog.matches('Greeting', [
(session) => {
session.send(['Greetings! How can I help you today?', 'Hello! How can I help you today?', 'Hi! How can I help you today?']);
},
]);
dialog.matches('CreateAccount', [
(session, args, next) => {
var accountAction = builder.EntityRecognizer.findEntity(args.entities, 'AccountAction');
var accountType = builder.EntityRecognizer.findEntity(args.entities, 'AccountType');
if (!accountType) {
builder.Prompts.choice(session, 'What type of account do you want to open?', ['Checking', 'Savings', 'Credit Card']);
} else {
return next({ response: accountType });
}
},
(session, results) => {
if (results.response) {
session.userData = results.response.entity;
builder.Prompts.text(session, 'Enter a name for your new ' + results.response.entity + ' account');
} else {
session.send('Something went wrong.');
}
},
(session, results) => {
var str = '';
for (var i in session.userData) {
str = str + session.userData[i];
}
api.createAccount(results.response, str, function (str) {
session.send(`${str} \n\nIs there anything else I can help you with?`);
});
},
]);
dialog.matches('ViewAccount', [
(session, args, next) => {
var accountType = builder.EntityRecognizer.findEntity(args.entities, 'AccountType');
if (!accountType) {
builder.Prompts.choice(session, 'Which account do you want to view?', ['Checking', 'Savings', 'Credit Card', 'All']);
} else {
return next({ response: accountType });
}
},
(session, results) => {
if (results.response) {
if (results.response.entity == 'All'){
session.send('Ok, here are all of your accounts.');
api.getAccounts('Checking', function(str){ session.send(str); });
api.getAccounts('Savings', function(str){ session.send(str); });
api.getAccounts('Credit Card', function(str){
session.send(`${str}`);
})
} else {
session.send(`Ok, here is your ${results.response.entity} account.`);
api.getAccounts(results.response.entity, function (str) {
session.send(`${str} \n\nIs there anything else I can help you with?`);
});
}
} else {
session.send('Something went wrong.');
}
},
]);
dialog.matches('FindATM', [
(session, args, next) => {
session.send('Here are the nearest ATMs:');
api.findAtms(function (str) {
session.send(`${str} \n\nIs there anything else I can help you with?`);
});
},
]);
dialog.matches('ScanCheck', [
(session, args) => {
builder.Prompts.attachment(session, 'Please take a picture of the check.');
},
(session, results) => {
console.error('Processing image: ' + session.message.attachments[0].contentUrl);
api.scanCheck(session.message.attachments[0].contentUrl, (str) => {
session.send(`${str} \n\nIs there anything else I can help you with?`);
});
},
]);
dialog.matches('NetWorth', [
(session, args) => {
api.getNetWorth((str) => {
session.send(`Here are your results:\n${str}\n\nIs there anything else I can help you with?`);
});
},
]);
dialog.matches('Confirm', [
(session) => {
session.send('What would you like to do?');
},
]);
dialog.matches('GoodBye', [
(session) => {
session.send('Goodbye. Thank you for using ChatBot!');
},
]);
dialog.onDefault(builder.DialogAction.send('I\'m sorry, I didn\'t quite catch that. How can I help you?'));