forked from microsoft/BotFramework-BlogSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
207 lines (175 loc) · 6.57 KB
/
Copy pathindex.js
File metadata and controls
207 lines (175 loc) · 6.57 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
global.restify = require('restify');
global.builder = require('botbuilder');
global.lodash = require('lodash');
global.promise = require('bluebird');
global.request = require('request-promise');
// Misc.
global.attachments = require('./lib/attachments');
global.jokes = require('./data/jokes.json');
// Cognitive Service Clients.
const QnAClient = require('./lib/qnaclient');
const BingSearchClient = require('./lib/bingsearchclient');
const SentimentAnalyzerClient = require('./lib/sentimentanalyzerclient');
const DialogAnalyzerClient = require('./lib/dialoganalyzerclient');
// Environment variables
const BOTBUILDER_APP_ID = process.env.BOTBUILDER_APP_ID;
const BOTBUILDER_APP_PASSWORD = process.env.BOTBUILDER_APP_PASSWORD;
const LUIS_MODEL = process.env.LUIS_MODEL;
const KB_ID = process.env.KB_ID;
const QNA_KEY = process.env.QNA_KEY;
const QNA_URL = process.env.QNA_URL;
const BING_SEARCH_CONFIG = process.env.BING_SEARCH_CONFIG;
const BING_SEARCH_KEY = process.env.BING_SEARCH_KEY;
const TEXT_ANALYTICS_KEY = process.env.TEXT_ANALYTICS_KEY;
const TEXT_ANALYTICS_URL = process.env.TEXT_ANALYTICS_URL;
const DIALOG_ANALYZER_CLIENTID = process.env.DIALOG_ANALYZER_CLIENTID;
const DIALOG_ANALYZER_KEY = process.env.DIALOG_ANALYZER_KEY;
const DIALOG_ANALYZER_URL = process.env.DIALOG_ANALYZER_URL;
// Check to see if the environment has been set.
if (!(BOTBUILDER_APP_ID &&
BOTBUILDER_APP_PASSWORD &&
LUIS_MODEL &&
KB_ID &&
QNA_KEY &&
QNA_URL &&
BING_SEARCH_CONFIG &&
BING_SEARCH_KEY &&
TEXT_ANALYTICS_KEY &&
TEXT_ANALYTICS_URL &&
DIALOG_ANALYZER_CLIENTID &&
DIALOG_ANALYZER_KEY &&
DIALOG_ANALYZER_URL
)) {
console.log(`Missing one of BOTBUILDER_APP_ID, BOTBUILDER_APP_PASSWORD, \
LUIS_MODEL, KB_ID, QNA_KEY, QNA_URL, BING_SEARCH_CONFIG, BING_SEARCH_KEY, \
TEXT_ANALYTICS_KEY, TEXT_ANALYTICS_URL, DIALOG_ANALYZER_CLIENTID, DIALOG_ANALYZER_KEY or DIALOG_ANALYZER_URL \
in environment variables!`);
process.exit(1);
}
// QnAClient allows simple question and answer style responses.
global.qnaClient = new QnAClient({
knowledgeBaseId: KB_ID,
subscriptionKey: QNA_KEY
});
// Search the web for results.
global.bingSearchClient = new BingSearchClient({
bingSearchConfig: BING_SEARCH_CONFIG,
bingSearchKey: BING_SEARCH_KEY
});
// Determine user mood from text
global.sentimentAnalyzerClient = new SentimentAnalyzerClient({
key: TEXT_ANALYTICS_KEY
});
global.dialogAnalyzerClient = new DialogAnalyzerClient({
clientId: DIALOG_ANALYZER_CLIENTID,
key: DIALOG_ANALYZER_KEY,
url: DIALOG_ANALYZER_URL
});
// Setup Restify Server
const server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
const connector = new builder.ChatConnector({
appId: BOTBUILDER_APP_ID,
appPassword: BOTBUILDER_APP_PASSWORD
});
// Serves a nice web site, that has the bot framework web chat component.
server.get('/', restify.plugins.serveStatic({
'directory': `${__dirname}/static`,
'default': 'index.html'
}));
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Create a LUIS recognizer for our bot, to identify intents from
// user text.
const recognizer = new builder.LuisRecognizer(LUIS_MODEL);
// Create our bot to listen in on the chat connector.
global.bot = new builder.UniversalBot(connector, (session) => {
session.beginDialog('sobot:search')
});
bot.recognizer(recognizer);
bot.use(builder.Middleware.sendTyping());
// Sends a nice greeting on a new message.
bot.on('conversationUpdate', (message) => {
if (!message.membersAdded) {
return;
}
message.membersAdded.forEach((identity) => {
if (identity.id !== message.address.bot.id) {
return;
}
bot.send(new builder.Message()
.address(message.address)
.text(`👋 Hello! I'm Stack Overflow's Resident Expert Bot 🤖 \
and I'm here to help you find questions, answers, or to \
just entertain you with a joke. Go ahead - ask me something!`
));
});
});
// Promise for obtaining JWT Token (requested once)
global.obtainToken = promise.promisify(connector.getAccessToken.bind(connector));
global.bingSearchQuery = async (session, args) => {
session.send("Hmm... Searching...");
session.sendTyping();
if (!args) {
return;
}
if (!args.query) {
return;
}
if (!args.fullTextQuery) {
args.fullTextQuery = args.query;
}
// Start and wait for Bing Search results.
let searchResults = await fetchBingSearchResults(args.query);
// Process search results
if (searchResults && searchResults.length > 0) {
session.send("I found the following results from your question...");
session.send(attachments.buildResultsMessageWithAttachments(session, searchResults));
return session.endDialog("Feel free to ask me another question, or even ask for a joke!");
} else {
return session.endDialog('Sorry… couldnt find any results for your query! 🤐');
}
}
global.filterforUsefulResults = (resultsArray) => {
const resultsCount = resultsArray.length > 10 ? 10 : resultsArray.length;
return lodash.slice(resultsArray, 0, resultsCount);
};
global.fetchBingSearchResults = async (query) => {
var searchResults = [];
await bingSearchClient.get({ searchText: query }, (err, res) => {
if (err) {
console.error('Error from callback:', err);
} else if (res && res.webPages && res.webPages.value && res.webPages.value.length > 0) {
for (var index = 0; index < res.webPages.value.length; index++) {
var val = res.webPages.value[index];
var result = {
title: val.name,
body_markdown: val.snippet,
link: val.url
};
searchResults.push(result);
}
}
});
return searchResults;
}
global.cleanQueryString = (query, removePunctuation) => {
let retQuery = query.toLowerCase();
if (removePunctuation) {
retQuery = retQuery.replace(/[^\w\s]|_/g, "");
}
retQuery = retQuery.replace(/\s+/g, " ");
return retQuery.trim();
}
// Dialogs
require('./dialogs/brain')();
require('./dialogs/joke')();
require('./dialogs/keywordPrompt')();
require('./dialogs/languages')();
require('./dialogs/menu')();
require('./dialogs/screenshot')();
require('./dialogs/search')();
require('./dialogs/smalltalk')();