-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
374 lines (346 loc) · 14.4 KB
/
index.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
'use strict';
module.change_code = 1;
var _ = require('lodash')
var Alexa = require('alexa-app');
var app = new Alexa.app('makers_rooms');
var moment = require('moment');
var DbHelper = require('./db_helper');
var dbHelper = new DbHelper();
var PASSWORD = "code smell";
var validatedPassword;
app.pre = function(request, response, type) {
dbHelper.createBookedEventsTable();
};
app.launch(function(req, res) {
var prompt = 'Welcome to Makers Rooms<break time="1s"/>' + 'Make a booking <break time="0.5s"/> check a room\'s schedule <break time="0.5s"/> or say help for more information.';
var cardText = buildCard("Makers Rooms", "Welcome to Makers Rooms. Make a booking, check a schedule, or say help for more information.");
res.card(cardText);
res.say(prompt).reprompt(prompt).shouldEndSession(false);
});
app.intent('startBookingIntent', {
'utterances': ['{create|make} {|a} {booking|new booking}']
},
function(req, res) {
res.say('To create a new booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(false);
return true;
});
app.intent('passwordBookingIntent', {
'slots': {
'PASSWORD': 'PASSWORDS',
},
'utterances': ['{the password is} {-|PASSWORD}']
},
function(req, res) {
var password = req.slot('PASSWORD');
if (password === PASSWORD) {
var session = req.getSession();
session.set("PasswordValidation", true);
res.say('Thank you. What is the date of your booking?').shouldEndSession(false);
return true;
}
else {
res.say('Sorry. I do not recognise this password. Please try again or ask makers for the correct password.').shouldEndSession(false);
return true;
}
});
app.intent('dateBookingIntent', {
'slots': {
'DATE': 'AMAZON.DATE',
},
'utterances': ['{-|DATE}']
},
function(req, res) {
validatedPassword = res.sessionObject.attributes.PasswordValidation;
if (validatedPassword === true) {
var date = req.slot('DATE');
var session = req.getSession();
session.set("Date", date);
res.say('You are making a booking for ' + date + '. Which room would you like to book?').shouldEndSession(false);
return true;
}
else {
res.say('Sorry. You can not create a booking without providing a password. To create a new booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(true);
return true;
}
});
app.intent('roomBookingIntent', {
'slots': {
'ROOM': 'LIST_OF_ROOMS',
},
'utterances': ['{|book} {-|ROOM}']
},
function(req, res) {
if (validatedPassword === true) {
var room = req.slot('ROOM');
var session = req.getSession();
session.set("RoomName", room);
res.say('You are booking ' + room + '. What time would you like to book ' + room + ' for?').shouldEndSession(false);
return true;
}
else {
res.say('Sorry. You can not create a booking without providing a password. To create a new booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(true);
return true;
}
});
app.intent('timeBookingIntent', {
'slots': {
'TIME': 'AMAZON.TIME',
},
'utterances': ['{|book} {|for|at} {-|TIME}']
},
function(req, res) {
if (validatedPassword === true) {
var time = req.slot('TIME');
var session = req.getSession();
session.set("StartTime", time);
res.say('You are booking the room from ' + time + '. How long would you like to book it for?').shouldEndSession(false);
return true;
}
else {
res.say('Sorry. You can not create a booking without providing a password. To create a new booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(true);
return true;
}
});
app.intent('durationBookingIntent', {
'slots': {
'DURATION': 'AMAZON.DURATION',
},
'utterances': ['{|book} {|for} {-|DURATION}']
},
function(req, res) {
if (validatedPassword === true) {
var duration = req.slot('DURATION');
var stringDuration = moment.duration(duration, moment.ISO_8601).asMinutes();
var session = req.getSession();
session.set("Duration", duration);
res.say('You are booking the room for ' + stringDuration + ' minutes. What is the name of your event?').shouldEndSession(false);
return true;
}
else {
res.say('Sorry. You can not create a booking without providing a password. To create a new booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(true);
return true;
}
});
app.intent('nameBookingIntent', {
'slots': {
'NAME': 'DESCRIPTION',
},
'utterances': ['{|my event is called} {-|NAME}']
},
function(req, res) {
if (validatedPassword === true) {
var name = req.slot('NAME');
var session = req.getSession();
session.set("Name", name);
res.say('You are booking the room for ' + name + '. Finally, what is your name?').shouldEndSession(false);
return true;
}
else {
res.say('Sorry. You can not create a booking without providing a password. To create a new booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(true);
return true;
}
});
app.intent('ownerBookingIntent', {
'slots': {
'OWNER': 'LIST_OF_MAKERS',
},
'utterances': ['{|my name is} {-|OWNER}']
},
function(req, res) {
if (validatedPassword === true) {
var bookingData = res.sessionObject.attributes;
var owner = req.slot('OWNER');
var session = req.getSession();
session.clear("PasswordValidation", true);
session.set("Owner", owner);
session.set("RoomDate", bookingData.RoomName + " " + bookingData.Date);
var bookingDataComplete = res.sessionObject.attributes;
var stringDuration = moment.duration(bookingData.Duration, moment.ISO_8601).asMinutes();
return dbHelper.addRecord(bookingDataComplete)
.then(function(overlaps) {
if (overlaps >= 1) {
res.say('Sorry, the room is booked at that time').shouldEndSession(false);
} else if (overlaps === 0) {
res.say('Thanks ' + owner + '. You have booked the ' + bookingData.RoomName + ' for ' + bookingData.Date + ' from ' + bookingData.StartTime + ' for ' + stringDuration + ' minutes for ' + bookingData.Name).shouldEndSession(true);
var cardText = buildCard("You've Booked a Room!", "Success! You've booked " + bookingData.RoomName + " for " + bookingData.Date + " from " + bookingData.StartTime + " for " + stringDuration + " minutes for " + bookingData.Name + ".");
res.card(cardText);
} else {
res.say('Sorry, I have failed to add the booking to the database. Please retry.').shouldEndSession(true);
}
});
}
else {
res.say('Sorry. You can not create a booking without providing a password. To create a new booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(true);
return true;
}
});
app.intent('findByRoomDateIntent', {
'slots': {
'ROOM': 'LIST_OF_ROOMS',
'DATE': 'AMAZON.DATE'
},
'utterances': ['{find|tell|give} {|me} {|all} {|the} {bookings|events} {in} {-|ROOM} {|on|for} {-|DATE}']
},
function(req, res){
var room = req.slot('ROOM');
var date = req.slot('DATE');
var roomDate = req.slot('ROOM') + ' ' + req.slot('DATE');
return dbHelper.readRoomDateRecords(roomDate)
.then(function(results) {
if (results.length !== 0) {
res.say('On ' + date + ' the ' + room + ' is booked for <break time="0.5s"/>').shouldEndSession(false);
results.forEach(function(event) {
res.say(event.Owner + 's' + event.Name + ' at ' + event.StartTime + ' <break time="0.5s"/>').shouldEndSession(false);
});
res.shouldEndSession(true);
} else {
res.say('The ' + room + ' is free the whole day on ' + date).shouldEndSession(true);
}
});
});
app.intent('findByRoomWithNowIntent', {
'slots': {
'ROOM': 'LIST_OF_ROOMS'
},
'utterances': ['{find|tell|give} {|me} {what is on now|what\'s on now|what is going on now|what\'s going on now} {in} {-|ROOM}']
},
function(req, res) {
var date = new Date().toISOString().slice(0,10);
var room = req.slot('ROOM');
var roomDate = (room + " " + date);
return dbHelper.readRoomDateRecordsForNow(roomDate)
.then(function(ongoingEvent) {
if (ongoingEvent !== undefined) {
var stringDuration = moment.duration(ongoingEvent.Duration, moment.ISO_8601).asMinutes();
res.say(ongoingEvent.RoomName + ' is booked from ' + ongoingEvent.StartTime + ' for ' + stringDuration + ' minutes for ' + ongoingEvent.Name).shouldEndSession(true);
} else {
res.say(room + ' is currently available').shouldEndSession(true);
}
});
});
app.intent('findByRoomWithTimeAndDateIntent', {
'slots': {
'TIME': 'AMAZON.TIME',
'ROOM': 'LIST_OF_ROOMS',
'DATE': 'AMAZON.DATE'
},
'utterances': ['{find|tell|give} {|me} {what is on at|what\'s on at } {-|TIME} {|on} {-|DATE} {in} {-|ROOM}']
},
function (req, res) {
var time = req.slot('TIME');
var date2 = req.slot('DATE');
var room = req.slot('ROOM');
var roomDate = (room + " " + date2);
return dbHelper.readRoomDateRecordsForTime(roomDate, date2, time)
.then(function(ongoingEvent) {
console.log("index", ongoingEvent)
if (ongoingEvent !== undefined) {
var stringDuration = moment.duration(ongoingEvent.Duration, moment.ISO_8601).asMinutes();
res.say(ongoingEvent.Owner + ' has booked the room from ' + ongoingEvent.StartTime + ' for ' + stringDuration + ' minutes for ' + ongoingEvent.Name).shouldEndSession(true);
} else {
res.say(room + ' is available on ' + date2 + ' at ' + time).shouldEndSession(true);
}
});
});
app.intent('startDeleteBookingIntent', {
'utterances': ['{delete|remove} {|a} {booking}']
},
function(req, res) {
res.say('To delete a booking please say delete with password, followed by the password').shouldEndSession(false);
return true;
});
app.intent('passwordDeleteBookingIntent', {
'slots': {
'PASSWORD': 'PASSWORDS',
},
'utterances': ['{delete with password} {-|PASSWORD}']
},
function(req, res) {
var password = req.slot('PASSWORD');
if (password === PASSWORD) {
var session = req.getSession();
session.set("PasswordValidation", true);
res.say('Thank you To delete a booking say delete <break time="0.5s"/> name from <break time="0.5s"/> room on <break time="0.5s"/> date').shouldEndSession(false);
return true;
}
else {
res.say('Sorry. I do not recognise this password. Please try again or ask makers for the correct password.').shouldEndSession(false);
return true;
}
});
app.intent('deleteBookingIntent', {
'slots': {
'NAME': 'DESCRIPTION',
'ROOM': 'LIST_OF_ROOMS',
'DATE': 'AMAZON.DATE'
},
'utterances': ['{remove|delete} {-|NAME} {from} {-|ROOM} {|on|for} {-|DATE}']},
function(req, res) {
validatedPassword = res.sessionObject.attributes.PasswordValidation;
if (validatedPassword === true) {
var eventName = req.slot('NAME');
var eventRoom = req.slot('ROOM');
var eventDate = req.slot('DATE');
var roomDate = req.slot('ROOM') + ' ' + req.slot('DATE');
return dbHelper.deleteRoomDateRecord(roomDate, eventName)
.then(function(deletedEvents) {
if (deletedEvents === 0) {
res.say('Sorry, I found no such booking to delete').shouldEndSession(true);
} else if (deletedEvents === 1) {
res.say('You have deleted ' + eventName + ' from ' + eventRoom + ' for ' + eventDate).shouldEndSession(true);
} else {
res.say('Sorry, I have failed to delete the booking from the database. Please retry.').shouldEndSession(true);
}
});
}
else {
res.say('Sorry. You can not delete a booking without providing a password. To delete a booking please say <break time="0.5s"/> the password is <break time="0.5s"/> followed by the password').shouldEndSession(true);
return true;
}
});
app.intent('secretIntent', {
'utterances': ['{who\'s|who is} {Rob Holden}']
},
function(req, res) {
var answer = 'I know <break time="0.5s"/> but I won\'t tell you';
res.say(answer).shouldEndSession(true);
var cardText = buildCard("Secret revealed", "Here is Rob!");
res.card(cardText);
});
app.intent('AMAZON.HelpIntent', {},
function(req, res) {
var help = 'Welcome to Makers Rooms Help <break time="0.5s"/>';
var content = 'To create a new booking, say <break time="0.5s"/> create a new booking on a date and then follow the instructions <break time="1s"/>' +
'To check a room\'s schedule for a certain date, say <break time="0.5s"/> tell me all the events in room for date <break time="1s"/>' +
'To see what\'s going on in a room now, say <break time="0.5s"/> what is on now in room <break time="1s"/>' +
'To see what\'s going on in a room at a certain date and time, say <break time="0.5s"/> what is on at time on date in room <break time="1s"/>' +
'To delete a booking, say <break time="0.5s"/> delete booking name from room on date <break time="1s"/>' +
'You can also say <break time="0.5s"/> stop or cancel to exit.';
var cardText = buildCard("Makers Room Help", content);
res.say(help + content).shouldEndSession(true);
res.card(cardText);
});
var cancelIntentFunction = function(req, res) {
res.say('Sayonara!').shouldEndSession(true);
};
app.intent('AMAZON.CancelIntent', {}, cancelIntentFunction);
app.intent('AMAZON.StopIntent', {}, cancelIntentFunction);
// Only use the following intent locally, to add a quick set of sample bookings to
// DynamoDB Local. Also un-comment the relevant lines at the end of db_helper.js.
// The sample bookings are stored in localSampleRecords.json.
// app.intent('addLocalSampleBookingsIntent', {},
// function(req, res){
// dbHelper.addSampleRecords();
// res.say('You have added some sample bookings to DynamoDB Local').shouldEndSession(false);
// });
function buildCard(title, text){
return {
"type": "Standard",
"title": title,
"text": text,
"image": {
"smallImageUrl": "https://cdn-images-1.medium.com/max/1600/1*HIJGMWtNFLBwpG5kpfmAXg.jpeg"
}
};
}
module.exports = app;