forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (55 loc) · 2.23 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
var util = require('util');
var _ = require('lodash');
var builder = require('botbuilder');
var restify = require('restify');
/// <reference path="../SearchDialogLibrary/index.d.ts" />
var SearchLibrary = require('../SearchDialogLibrary');
var AzureSearch = require('../SearchProviders/azure-search');
// 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 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());
// Bot with main dialog that triggers search and display its results
var bot = new builder.UniversalBot(connector, [
function (session) {
// Trigger Search
SearchLibrary.begin(session);
},
function (session, args) {
// Process selected search results
session.send(
'Done! For future reference, you selected these properties: %s',
args.selection.map(function (i) { return i.key; }).join(', '));
}
]);
// Azure Search
var azureSearchClient = AzureSearch.create('realestate', '82BCF03D2FC9AC7F4E9D7DE1DF3618A5', 'listings');
var realStateResultsMapper = SearchLibrary.defaultResultsMapper(realstateToSearchHit);
// Register Search Dialogs Library with bot
bot.library(SearchLibrary.create({
multipleSelection: true,
search: function (query) { return azureSearchClient.search(query).then(realStateResultsMapper); },
refiners: ['region', 'city', 'type'],
refineFormatter: function (refiners) {
return _.zipObject(
refiners.map(function (r) { return 'By ' + _.capitalize(r); }),
refiners);
}
}));
// Maps the AzureSearch RealState Document into a SearchHit that the Search Library can use
function realstateToSearchHit(realstate) {
return {
key: realstate.listingId,
title: util.format('%d bedroom, %d bath in %s, $%s',
realstate.beds, realstate.baths, realstate.city, realstate.price.toFixed(2)),
description: realstate.description,
imageUrl: realstate.thumbnail
};
}