-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
111 lines (91 loc) · 4.61 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
/*-----------------------------------------------------------------------------
A FAQ bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
const restify = require('restify');
const builder = require('botbuilder');
const cognitiveservices = require("botbuilder-cognitiveservices");
const smallTalkReplies = require("./smalltalk");
// Setup Creative Commons FAQ KB
const faqRecognizer = new cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
subscriptionKey: process.env.QnASubscriptionKey,
top: 3});
// Setup Small Talk KB
const smalltalkRecognizer = new cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: process.env.SmallTalkKnowledgebaseId,
subscriptionKey: process.env.QnASubscriptionKey,
})
// Setup Restify Server
const 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
const connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
stateEndpoint: process.env.BotStateEndpoint,
openIdMetadata: process.env.BotOpenIdMetadata
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
/*----------------------------------------------------------------------------------------
* Bot Storage: This is a great spot to register the private state storage for your bot.
* We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
* For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
* ---------------------------------------------------------------------------------------- */
// Create your bot with a function to receive messages from the user
const bot = new builder.UniversalBot(connector)
// Send welcome when conversation with bot is started, by initiating the root dialog
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
// bot.beginDialog(message.address, '/');
var msg = new builder.Message().address(message.address);
msg.text('Hey there, how may I help you? You can ask me things about Creative Commons.');
msg.textLocale('en-US');
bot.send(msg);
}
});
}
});
const qnaMakerTools = new cognitiveservices.QnAMakerTools();
bot.library(qnaMakerTools.createLibrary());
const basicQnAMakerDialog = new cognitiveservices.QnAMakerDialog({
recognizers: [faqRecognizer, smalltalkRecognizer],
defaultMessage: 'I am having trouble understanding your question.. Can you try asking me another way?',
qnaThreshold: 0.3,
// feedbackLib: qnaMakerTools
});
// Override to also include the knowledgebase question with the answer on confident matches
basicQnAMakerDialog.respondFromQnAMakerResult = function(session, qnaMakerResult){
var result = qnaMakerResult;
var response = result.answers[0].answer;
if (response.includes('{')) {
var faqAnswer = JSON.parse(response);
console.log(faqAnswer);
session.send(faqAnswer.answer);
if (faqAnswer.followUps !== 'undefined' && faqAnswer.followUps.length > 0) {
console.log("There are followUps!");
builder.Prompts
.choice(session, 'You can also ask me the following things..',
faqAnswer.followUps, { listStyle: builder.ListStyle.button });
}
} else {
const botReplies = smallTalkReplies[response]
const replyIndex = Math.floor(Math.random() * botReplies.length)
session.send(botReplies[replyIndex]);
}
}
// // Override to log user query and matched Q&A before ending the dialog
// basicQnAMakerDialog.defaultWaitNextMessage = function(session, qnaMakerResult){
// if(session.privateConversationData.qnaFeedbackUserQuestion != null && qnaMakerResult.answers != null && qnaMakerResult.answers.length > 0
// && qnaMakerResult.answers[0].questions != null && qnaMakerResult.answers[0].questions.length > 0 && qnaMakerResult.answers[0].answer != null){
// console.log('User Query: ' + session.privateConversationData.qnaFeedbackUserQuestion);
// console.log('KB Question: ' + qnaMakerResult.answers[0].questions[0]);
// console.log('KB Answer: ' + qnaMakerResult.answers[0].answer);
// }
// session.endDialog();
// }
bot.dialog('/', basicQnAMakerDialog);