-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
285 lines (268 loc) · 9.13 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
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
/*-----------------------------------------------------------------------------
A simple echo bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
// This loads the environment variables from the .env file
// #ifdef ENV
// require('dotenv-extended').load();
// #endif
var restify = require('restify');
var builder = require('botbuilder');
// 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 connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
stateEndpoint: process.env.BotStateEndpoint,
openIdMetadata: process.env.BotOpenIdMetadata
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
/*----------------------------------------------------------------------------------------
* Bot Storage: This is a great spot to register the private state storage for your bot.
* We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
* For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
* ---------------------------------------------------------------------------------------- */
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, function(session) {
session.send(
'Sorry, I did not understand \'%s\'. Type \'help\' if you need assistance.',
session.message.text);
session.endDialog();
});
// Main dialog with LUIS
const LUIS_URL =
"https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/99e54023-bf56-4417-8311-fa5eb2d7245e?subscription-key=a659eee201024a6d92b6cf9a2973374a&timezoneOffset=0&verbose=true&q=";
// https://github.com/Microsoft/BotBuilder/issues/2670
// enables not rezognizing for prompts
// something says that IntentDialo may be better; switch to it if further problems?
var recognizer = new builder.LuisRecognizer(LUIS_URL).onEnabled(function(
context, callback) {
var enabled = context.dialogStack().length == 0;
callback(null, enabled);
});
bot.recognizer(recognizer);
bot.dialog('Greeting', function(session) {
// parent(session);
session.endDialog();
}).triggerAction({
matches: 'Greeting'
});
bot.dialog('Help', function(session) {
session.endDialog(
'Welcome to STRAT! We make it easy to learn about your favorite companies, and suggest stocks based on your preferences, to get started, take our profile test by saying "Risk Profile"!'
);
}).triggerAction({
matches: 'Help'
});
bot.dialog('Info', function(session, args) {
var stockEntity = builder.EntityRecognizer.findEntity(args.intent.entities,
'Stock');
parent(session, "info", stockEntity.entity);
session.endDialog();
}).triggerAction({
matches: 'Info'
});
bot.dialog('Learn', function(session, args) {
var stockEntity = builder.EntityRecognizer.findEntity(args.intent.entities,
'Stock');
parent(session, "learn", stockEntity.entity);
session.endDialog();
}).triggerAction({
matches: 'Learn'
});
bot.dialog('Profile', [
function(session) {
session.userData.rating = 0;
// Prompt user for next field
builder.Prompts.choice(session, "How long do you hold a stock?", [
"Less than 1 year",
"1 to 2 years",
"2 to 3 years",
"3 to 4 years",
"4 to 5 years",
"More than 5 years"
], {
listStyle: builder.ListStyle.button
});
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"Which of the following have you owned before or own now?", [
"N/A",
"Time deposit or money market funds",
"Bonds or bond mutual funds",
"Stock mutual funds",
"Individual stocks",
"Leveraged funds"
], {
listStyle: builder.ListStyle.button
});
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"Which best describes your experience of investment?", [
"None",
"None beyond bank savings accounts",
"Some investment experience (mutual funds or individual shares)",
"Experienced with a portfolio managed by an advisor",
"Experienced and manage my own portfolio",
"Buy AMD"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"What is your prime objective with investment?", [
"Education of your children",
"Savings",
"Capital growth and returns",
"Retirement",
"My legacy",
"Yacht"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"What percentage of your total savings are invested?", [
"<10%",
"10-20%",
"20-30%",
"30-40%",
"40-99%",
"100%"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"If you were to invest all your savings into only one of the five investments below, which would you choose?\nChoice | Worst Year | Average Year | Best Year", [
"Investment A | 4 % | 6 % | 8 %",
"Investment B | 2 % | 8 % | 14 %",
"Investment C | -5 % | 10 % | 20 %",
"Investment D | -15 % | 12 % | 25 %",
"Investment E | -20 % | 15 % | 30 %",
"Investment F | -80 % | 50 % | 150 %"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"What sport would you most like to participate in?", [
"Baseball",
"Soccer",
"Skiing",
"Rock Climbing",
"Skydiving",
"Wingsuit Flying"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"Which country would you be least likely to visit?", [
"Venezuela",
"Mexico",
"Russia",
"The United Kingdom",
"Germany",
"Iceland"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"How many bones have you broken?", [
"0",
"1-2",
"3-5",
"6-9",
"10-14",
"15+"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
builder.Prompts.choice(session,
"Which of these describes you most accurately?", [
"Cross the street only at crosswalks",
"Cross the street near crosswalks",
"Cross the street anywhere if you don’t see cars",
"Cross the street anywhere if there are no cars in your path",
"Cross the street anywhere if the cars are moving slowly or have room to stop",
"Cross the street anywhere, no matter what"
], {
listStyle: builder.ListStyle.button
}
);
},
function(session, result) {
session.userData.rating += (result.response.index * 2);
session.userData.rating >= 50 ? session.userData.rating /= 250 :
session.userData.rating /= 500;
session.userData.rating == 0.4 ? session.userData.rating = 1 : session.userData
.rating = session.userData.rating;
session.send("Your risk score is: %s", session.userData.rating);
session.endDialog();
}
]).triggerAction({
matches: 'Profile'
});
bot.dialog('Suggest', function(session, args) {
parent(session, "suggest", session.userData.rating);
session.endDialog();
}).triggerAction({
matches: 'Suggest'
});
// JS -> PYTHON code
// https://gist.github.com/cowboy/3427148
var parent = function(session, type, stock) {
var spawn = require('child_process').spawn;
var child = spawn('python3', ['-u', 'python-scripts/res.py', type, stock]);
var stdout = '';
var stderr = '';
child.stdout.on('data', function(buf) {
stdout += buf;
});
child.stderr.on('data', function(buf) {
stderr += buf;
});
child.on('close', function(code) {
// session.send('[END] code %s', code);
session.send(stdout);
if (stderr) {
session.send('[END] stderr "%s"', stderr);
}
});
};
// log any bot errors into the console
bot.on('error', function(e) {
console.log('And error ocurred', e);
});