forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotels.js
83 lines (72 loc) · 2.6 KB
/
hotels.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
var builder = require('botbuilder');
var Store = require('./store');
module.exports = [
// Destination
function (session) {
session.send('Welcome to the Hotels finder!');
builder.Prompts.text(session, 'Please enter your destination');
},
function (session, results, next) {
session.dialogData.destination = results.response;
session.send('Looking for hotels in %s', results.response);
next();
},
// Check-in
function (session) {
builder.Prompts.time(session, 'When do you want to check in?');
},
function (session, results, next) {
session.dialogData.checkIn = results.response.resolution.start;
next();
},
// Nights
function (session) {
builder.Prompts.number(session, 'How many nights do you want to stay?');
},
function (session, results, next) {
session.dialogData.nights = results.response;
next();
},
// Search...
function (session) {
var destination = session.dialogData.destination;
var checkIn = new Date(session.dialogData.checkIn);
var checkOut = checkIn.addDays(session.dialogData.nights);
session.send(
'Ok. Searching for Hotels in %s from %d/%d to %d/%d...',
destination,
checkIn.getMonth() + 1, checkIn.getDate(),
checkOut.getMonth() + 1, checkOut.getDate());
// Async search
Store
.searchHotels(destination, checkIn, checkOut)
.then(function (hotels) {
// Results
session.send('I found in total %d hotels for your dates:', hotels.length);
var message = new builder.Message()
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(hotels.map(hotelAsAttachment));
session.send(message);
// End
session.endDialog();
});
}
];
// Helpers
function hotelAsAttachment(hotel) {
return new builder.HeroCard()
.title(hotel.name)
.subtitle('%d stars. %d reviews. From $%d per night.', hotel.rating, hotel.numberOfReviews, hotel.priceStarting)
.images([new builder.CardImage().url(hotel.image)])
.buttons([
new builder.CardAction()
.title('More details')
.type('openUrl')
.value('https://www.bing.com/search?q=hotels+in+' + encodeURIComponent(hotel.location))
]);
}
Date.prototype.addDays = function (days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
};