forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (109 loc) · 5.13 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
// This loads the environment variables from the .env file
require('dotenv-extended').load();
var builder = require('botbuilder');
var azure = require('botbuilder-azure');
var restify = require('restify');
// 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 HelpMessage = '\n * If you want to know which city I\'m using for my searches type \'current city\'. \n * Want to change the current city? Type \'change city to cityName\'. \n * Want to change it just for your searches? Type \'change my city to cityName\'';
var UserNameKey = 'UserName';
var UserWelcomedKey = 'UserWelcomed';
var CityKey = 'City';
// Setup bot with default dialog
var bot = new builder.UniversalBot(connector, function (session) {
// initialize with default city
if (!session.conversationData[CityKey]) {
session.conversationData[CityKey] = 'Seattle';
session.send('Welcome to the Search City bot. I\'m currently configured to search for things in %s', session.conversationData[CityKey]);
}
// is user's name set?
var userName = session.userData[UserNameKey];
if (!userName) {
return session.beginDialog('greet');
}
// has the user been welcomed to the conversation?
if (!session.privateConversationData[UserWelcomedKey]) {
session.privateConversationData[UserWelcomedKey] = true;
return session.send('Welcome back %s! Remember the rules: %s', userName, HelpMessage);
}
session.beginDialog('search');
});
// Azure DocumentDb State Store
var docDbClient = new azure.DocumentDbClient({
host: process.env.DOCUMENT_DB_HOST,
masterKey: process.env.DOCUMENT_DB_MASTER_KEY,
database: process.env.DOCUMENT_DB_DATABASE,
collection: process.env.DOCUMENT_DB_COLLECTION
});
var botStorage = new azure.AzureBotStorage({ gzipData: false }, docDbClient);
// Set Custom Store
bot.set('storage', botStorage);
// Enable Conversation Data persistence
bot.set('persistConversationData', true);
// search dialog
bot.dialog('search', function (session, args, next) {
// perform search
var city = session.privateConversationData[CityKey] || session.conversationData[CityKey];
var userName = session.userData[UserNameKey];
var messageText = session.message.text.trim();
session.send('%s, wait a few seconds. Searching for \'%s\' in \'%s\'...', userName, messageText, city);
session.send('https://www.bing.com/search?q=%s', encodeURIComponent(messageText + ' in ' + city));
session.endDialog();
});
// reset bot dialog
bot.dialog('reset', function (session) {
// reset data
delete session.userData[UserNameKey];
delete session.conversationData[CityKey];
delete session.privateConversationData[CityKey];
delete session.privateConversationData[UserWelcomedKey];
session.endDialog('Ups... I\'m suffering from a memory loss...');
}).triggerAction({ matches: /^reset/i });
// print current city dialog
bot.dialog('printCurrentCity', function (session) {
// print city settings
var userName = session.userData[UserNameKey];
var defaultCity = session.conversationData[CityKey];
var userCity = session.privateConversationData[CityKey];
if (userCity) {
session.endDialog(
'%s, you have overridden the city. Your searches are for things in %s. The default conversation city is %s.',
userName, userCity, defaultCity);
} else {
session.endDialog('Hey %s, I\'m currently configured to search for things in %s.', userName, defaultCity);
}
}).triggerAction({ matches: /^current city/i });
// change current city dialog
bot.dialog('changeCurrentCity', function (session, args) {
// change default city
var newCity = args.intent.matched[1].trim();
session.conversationData[CityKey] = newCity;
var userName = session.userData[UserNameKey];
session.endDialog('All set %s. From now on, all my searches will be for things in %s.', userName, newCity);
}).triggerAction({ matches: /^change city to (.*)/i });
// change my current city dialog
bot.dialog('changeMyCurrentCity', function (session, args) {
// change user's city
var newCity = args.intent.matched[1].trim();
session.privateConversationData[CityKey] = newCity;
var userName = session.userData[UserNameKey];
session.endDialog('All set %s. I have overridden the city to %s just for you', userName, newCity);
}).triggerAction({ matches: /^change my city to (.*)/i });
// Greet dialog
bot.dialog('greet', new builder.SimpleDialog(function (session, results) {
if (results && results.response) {
session.userData[UserNameKey] = results.response;
session.privateConversationData[UserWelcomedKey] = true;
return session.endDialog('Welcome %s! %s', results.response, HelpMessage);
}
builder.Prompts.text(session, 'Before get started, please tell me your name?');
}));