forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
117 lines (103 loc) · 4.8 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
var builder = require('botbuilder');
var restify = require('restify');
var Store = require('./store');
// 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 bot
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
var bot = new builder.UniversalBot(connector);
server.post('/api/messages', connector.listen());
// You can provide your own model by specifing the 'LUIS_MODEL_URL' environment variable
// This Url can be obtained by uploading or creating your model from the LUIS portal: https://www.luis.ai/
const LuisModelUrl = process.env.LUIS_MODEL_URL ||
'https://api.projectoxford.ai/luis/v1/application?id=162bf6ee-379b-4ce4-a519-5f5af90086b5&subscription-key=11be6373fca44ded80fbe2afa8597c18';
// Main dialog with LUIS
var recognizer = new builder.LuisRecognizer(LuisModelUrl);
var intents = new builder.IntentDialog({ recognizers: [recognizer] })
.matches('SearchHotels', [
function (session, args, next) {
session.send('Welcome to the Hotels finder! we are analyzing your message: \'%s\'', session.message.text);
// try extracting entities
var cityEntity = builder.EntityRecognizer.findEntity(args.entities, 'builtin.geography.city');
var airportEntity = builder.EntityRecognizer.findEntity(args.entities, 'AirportCode');
if (cityEntity) {
// city entity detected, continue to next step
session.dialogData.searchType = 'city';
next({ response: cityEntity.entity });
} else if (airportEntity) {
// airport entity detected, continue to next step
session.dialogData.searchType = 'airport';
next({ response: airportEntity.entity });
} else {
// no entities detected, ask user for a destination
builder.Prompts.text(session, 'Please enter your destination');
}
},
function (session, results) {
var destination = results.response;
var message = 'Looking for hotels';
if (session.dialogData.searchType === 'airport') {
message += ' near %s airport...';
} else {
message += ' in %s...';
}
session.send(message, destination);
// Async search
Store
.searchHotels(destination)
.then((hotels) => {
// args
session.send('I found %d hotels:', hotels.length);
var message = new builder.Message()
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(hotels.map(hotelAsAttachment));
session.send(message);
// End
session.endDialog();
});
}
])
.matches('ShowHotelsReviews', (session, args) => {
// retrieve hotel name from matched entities
var hotelEntity = builder.EntityRecognizer.findEntity(args.entities, 'Hotel');
if (hotelEntity) {
session.send('Looking for reviews of \'%s\'...', hotelEntity.entity);
Store.searchHotelReviews(hotelEntity.entity)
.then((reviews) => {
var message = new builder.Message()
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(reviews.map(reviewAsAttachment));
session.send(message)
});
}
})
.matches('Help', builder.DialogAction.send('Hi! Try asking me things like \'search hotels in Seattle\', \'search hotels near LAX airport\' or \'show me the reviews of The Bot Resort\''))
.onDefault((session) => {
session.send('Sorry, I did not understand \'%s\'. Type \'help\' if you need assistance.', session.message.text);
});
bot.dialog('/', intents);
// 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))
]);
}
function reviewAsAttachment(review) {
return new builder.ThumbnailCard()
.title(review.title)
.text(review.text)
.images([new builder.CardImage().url(review.image)])
}