-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
324 lines (309 loc) · 11.5 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//Make sure the following two modules are installed through npm!
var restify = require('restify');
var builder = require('botbuilder');
var campuses = ['college ave', 'college avenue', 'busch','bush','livingston','livi',
'douglass','douglas','dougie','cook'];
var fillerText = [
'I\'m here if you need me',
'I heard that new star wars movie is great',
'Yes',
'Maybe',
'No',
'I\'d rather not say',
'Just another day on the Banks of the Old Raritan',
'Is that all you can think of right now?',
'Happy birthday to the University!',
'Isn\'t technology amazing?',
'{{endpoint: no response; buffer overflow exception}, null}',
'Winter is upon us',
'Some things never change',
'I would be the authority on that',
'That sounds like something you should talk about with the TA',
'.......'
];
var start = -1;
var end = -1;
var theDate = new Date();
var today = theDate.getDay();
var builder = require('botbuilder');
// Setup Restify Server
var server = restify.createServer();
// Serve a static web page
server.get(/.*/, restify.serveStatic({
'directory': '.',
'default': 'index.html'
}));
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
//create chat bot
var connector = new builder.ChatConnector({
appId: 'b3378cc0-02c2-4510-ad05-30a98f1c5ab2',
appPassword: 'vGt0wRf4UxL4LZPLUHHuBcO'
});
var helloBot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
var active = false;
var history = 0;
helloBot.dialog('/', new builder.CommandDialog()
.matches(['fuc','bitch','dick','shit','wtf','stupid','pussy','damn','freaki','wank','piss','nigg'
], function (session){
if(active){
session.endDialog('I will not tolerate your vulgarity!');
active = false;
}
})
.matches(['can','why','you','me'], function (session){
if(active)
session.beginDialog('/help');
})
.matches(['travel','hurry','bus','when'], function (session) {
if(active)
session.beginDialog('/travel');
})
.matches(['no','quit','bye','leave','go','away'], function (session) {
if(active){
var closing = "";
if(history % 3 == 0)
closing = 'Good day...'
else if (history % 2 == 0)
closing = 'We can chat more later...'
else
closing = 'I\'ll give you some space...'
session.endDialog(closing);
active = false;
}
})
.matches(['henry'], function (session){
if(history == 0 )
session.send('Greetings.');
else
session.send('What do you need?');
active = true;
++history;
})
.onDefault(function (session) {
if(active){
if(today == 0 || today==6)
session.send('Must you bother me with nonsense on the weekend as well?');
else{
var choice = Math.floor(Math.random() * fillerText.length);
session.send(fillerText[choice]);
choice = Math.floor(Math.random() * 63);
if(choice == 62)
session.send('^ Sorry, wrong chat');
}
}
})
);
helloBot.dialog('/travel', [
function (session) {
builder.Prompts.text(session,
'What campus will you be starting from? (only enter the name!)');
},
function (session, results) {
var answer = results.response.toLowerCase();
var location = campuses.indexOf(answer);
var moveOn = false;
switch(location){
case -1:
default:
break;
case 0:
case 1:
start = 0;
moveOn=true;
break;
case 2:
case 3:
start = 1;
moveOn=true;
break;
case 4:
case 5:
start = 4;
moveOn=true;
break;
case 6:
case 7:
case 8:
start = 2;
moveOn=true;
break;
case 9:
start = 5;
moveOn=true;
break;
}
if(moveOn)
builder.Prompts.text(session, 'What campus are you going to?');
else
session.endDialog(
'I don\'t recognize that campus. Did you really want to go anywhere?');
},
function (session, results) {
var answer = results.response.toLowerCase();
var location = campuses.indexOf(answer);
var moveOn = false;
switch(location){
case -1:
default:
break;
case 0:
case 1:
end = 0;
moveOn=true;
break;
case 2:
case 3:
end = 1;
moveOn=true;
break;
case 4:
case 5:
end = 4;
moveOn=true;
break;
case 6:
case 7:
case 8:
end = 2;
moveOn=true;
break;
case 9:
end = 5;
moveOn=true;
break;
}
if(moveOn){
var distance = Math.abs(start - end);
if(distance == 0){
session.endDialog('I recommend you walk there whenever you\'re ready.')
return;
}
//fun for later
/*if( (start == 1 || start == 4) && (end == 2 || end == 5) )
session.send("You have quite a journey ahead of you, eh?");*/
builder.Prompts.number(session, 'And you need to be there how many hours from now?'
+' (less than 1 hour means right now!)');
}
else
session.endDialog(
'I don\'t recognize that campus. Did you really want to go anywhere?');
},
function (session, results){
var soon=Number(results.response);
if(soon == NaN){
session.endDialog(
'Why don\'t we try this again when you\'re ready to stop playing games');
}
else
soon = Math.round(soon);
var now = new Date();
var target = new Date(now);
target.setTime((target.getTime() + (3600000 * soon) ) );
//session.send("Look at my trimmings!"+target.toString().substring(0,21));
//would still need to convert away from military time!
if(target.getHours() > 20 || target.getHours() < 10){
if(soon < 1){
session.endDialog("There won't be too much traffic during those wee hours. "
+"But you still don't have much time left. I recommend you get moving.");
return;
}
else{
target.setTime((target - 1800000));
session.endDialog("There won't be too much traffic during those wee hours. "
+"Leave no later than "+target+" and you should arrive on time.");
return;
}
}
var temp = new Date(now);
temp.setHours(10);
temp.setMinutes(0);
var timeTable = [];
timeTable.push(new Date(temp));
//I have a wondrous proof of this...
for(var i=1; i<19; ++i){
if((i % 3) == (1 % 3) )
temp.setTime(temp.getTime() + 1800000);
else
temp.setTime(temp.getTime() + 2100000);
var moreTemp = new Date(temp.getTime());
timeTable.push(moreTemp);
}
//now loop through the timetable and stop at the first entry that
//is larger than our query. Go back one and if it is not the campus
//we are on, then recommend you leave BY that time. If it is the
//campus we are one, recommend you leave BEFORE that time.
var b = 0;
for(b; b<timeTable.length; ++b){
if( target.getTime() < timeTable[b].getTime()){
break;
}
}
if(soon<1){
session.send('Okay. I understand that you need to get there ASAP.');
//is this person on a high-traffic campus?
var isCrowded = ( (start % 3)==( (b-1) % 3) );
if(isCrowded){
session.send(
"If you have more than 30 minutes left, I would recommend you wrap up what"
+"you're doing and start catching a bus to avoid traffic.")
session.endDialog("If you have even less time left, then hurry up and go!"
+"And remember to take off your backpack so more people can squeeze"
+"onto the bus.")
return;
}
else{
session.send(
"If you have more than 30 minutes left, you can definitely wait around a little"
+"longer. Maybe grab a snack at a particular diner...")
session.endDialog("If you have even less time left, then get to the bus, posthaste!"
+"There should be plenty of wiggle room on the bus.")
return;
}
}
else{
//they entered a time between 10 and 10:30
if(b == 1){
if((start % 3)==(0 % 3)){ //college ave
var early = new Date(timeTable[0]);
early.setTime((early.getTime() - (3600000) ) );
session.endDialog("It will be quite busy on your campus near that time. "
+"I recommend you leave somewhere between"+"\n"
+early.toString()+"\n"+"and"+"\n"+timeTable[0].toString());
return;
}
//the first recommendable timeslot is NOT a busy one for their current campus
else{
session.endDialog("The buses in your area should be somewhat spacious near then. "
+"You'll find most success by leaving between"+"\n"
+timeTable[b-1]+"\n"+"and"+"\n"+timeTable[b]);
return;
}
}
//between 10:30 and 8:00
//the first recommendable timeslot is a busy one for their current campus
if((start % 3)==((b-1) % 3)){
session.endDialog("It's gonna be pretty busy on your campus near that time."
+"I recommend you leave somewhere between"+"\n"
+timeTable[b-2].toString()+"\n"+"and"+"\n"+timeTable[b-1].toString());
return;
}
//the first recommendable timeslot is NOT a busy one for their current campus
else{
session.endDialog("The buses in your area should be somewhat spacious near then. "
+"You'll find most success by leaving between"+"\n"
+timeTable[b-1]+"\n"+"and"+"\n"+timeTable[b]);
return;
}
//maybe set an alarm?
return;
}
}
]);
helloBot.dialog('/help', [
function (session) {
session.endDialog('I am here to serve.\nI recognize the following commands:\nbus\nquit');
}
]);