forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
146 lines (129 loc) · 5.25 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
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var builder = require('botbuilder');
var restify = require('restify');
var Store = require('./store');
var spellService = require('./spell-service');
// 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 connector and listen for messages
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
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);
});
// 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/
var recognizer = new builder.LuisRecognizer(process.env.LUIS_MODEL_URL);
bot.recognizer(recognizer);
bot.dialog('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.intent.entities, 'builtin.geography.city');
var airportEntity = builder.EntityRecognizer.findEntity(args.intent.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(function (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();
});
}
]).triggerAction({
matches: 'SearchHotels',
onInterrupted: function (session) {
session.send('Please provide a destination');
}
});
bot.dialog('ShowHotelsReviews', function (session, args) {
// retrieve hotel name from matched entities
var hotelEntity = builder.EntityRecognizer.findEntity(args.intent.entities, 'Hotel');
if (hotelEntity) {
session.send('Looking for reviews of \'%s\'...', hotelEntity.entity);
Store.searchHotelReviews(hotelEntity.entity)
.then(function (reviews) {
var message = new builder.Message()
.attachmentLayout(builder.AttachmentLayout.carousel)
.attachments(reviews.map(reviewAsAttachment));
session.endDialog(message);
});
}
}).triggerAction({
matches: 'ShowHotelsReviews'
});
bot.dialog('Help', function (session) {
session.endDialog('Hi! Try asking me things like \'search hotels in Seattle\', \'search hotels near LAX airport\' or \'show me the reviews of The Bot Resort\'');
}).triggerAction({
matches: 'Help'
});
// Spell Check
if (process.env.IS_SPELL_CORRECTION_ENABLED === 'true') {
bot.use({
botbuilder: function (session, next) {
spellService
.getCorrectedText(session.message.text)
.then(function (text) {
session.message.text = text;
next();
})
.catch(function (error) {
console.error(error);
next();
});
}
});
}
// 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)]);
}