-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
129 lines (103 loc) · 4.24 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
var builder = require('botbuilder');
var restify = require('restify');
var cognitiveservices = require('botbuilder-cognitiveservices');
var google = require('google');
google.resultsPerPage = 3;
// 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 connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector);
var luisAppUrl = process.env.LUIS_APP_URL;
var luisRecognizer = new builder.LuisRecognizer(luisAppUrl);
var qnarecognizer = new cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: process.env.QNA_KB_ID,
subscriptionKey: process.env.QNA_SUB_KEY,
top: 1,
qnaThreshold: 0.65
});
var intents = new builder.IntentDialog({
recognizers: [qnarecognizer, luisRecognizer],
recognizeOrder: builder.RecognizeOrder.series
});
bot.on('conversationUpdate', function(message) {
if (message.membersAdded) {
message.membersAdded.forEach(function(identity) {
if (identity.id == message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text("Welcome to Lexis Advance.<br>How can I help you?<br>You can ask questions like \"How do I filter results after search?\" or \"How to create a new folder?\"");
bot.send(reply);
}
});
}
});
bot.dialog('/', intents);
intents.matches('qna', [
function(session, args) {
var answerEntity = JSON.parse(builder.EntityRecognizer.findEntity(args.entities, 'answer').entity);
session.dialogData.answerEntity = answerEntity;
session.send(answerEntity.summary);
if (answerEntity.additionalLinks && answerEntity.additionalLinks != "None") {
builder.Prompts.confirm(session, "I found some additional links topics. Do you want to view them?");
} else {
session.endConversation();
}
},
function(session, results) {
if (results.response) {
var additionalLinks = session.dialogData.answerEntity.additionalLinks.replace(/\,/g, "<br>");
session.send(additionalLinks);
} else {
session.send("That's alright, you can continue exploring more topics.");
}
}
]);
intents.matches('Help.me', [
function(session) {
session.send("How can I help you?<br>You can ask questions like \"How do I filter results after search?\" or \"How to create a new folder?\"");
}
]);
intents.matches('Incorrect.result', [
function(session) {
session.send("I am sorry to hear that. I am still learning your responses.<br>In the mean time can you modify the search query?");
}
]);
intents.onDefault([
function(session) {
session.dialogData.searchMsg = session.message.text;
builder.Prompts.confirm(session, "No results found.<br>Do you wish to perform a google search?");
},
function(session, results) {
if (results.response) {
console.log("Atit" + session.dialogData.searchMsg);
google(session.dialogData.searchMsg, function(err, res) {
if (err) {
console.error(err);
} else {
console.log(JSON.stringify(res))
var searches = "";
for (var i in res.links) {
console.log(res.links[i])
if (res.links[i] && res.links[i].title && res.links[i].href) {
var link = res.links[i];
searches += link.title + ' - ' + link.href + "<br><br>";
}
}
session.send(searches);
}
});
} else {
session.send("That's alright, you can refine your search.");
}
}
]);