-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
123 lines (106 loc) · 5.4 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
/*-----------------------------------------------------------------------------
A simple echo bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");
var builder_cognitiveservices = require("botbuilder-cognitiveservices");
// 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.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
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
* ---------------------------------------------------------------------------------------- */
var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector, [
function (session){
var qnaKnowledgebaseId = process.env.QnAKnowledgebaseId;
var qnaAuthKey = process.env.QnAAuthKey || process.env.QnASubscriptionKey;
var endpointHostName = process.env.QnAEndpointHostName;
// QnA Subscription Key and KnowledgeBase Id null verification
if ((qnaAuthKey == null || qnaAuthKey == '') || (qnaKnowledgebaseId == null || qnaKnowledgebaseId == ''))
session.send('Please set QnAKnowledgebaseId, QnAAuthKey and QnAEndpointHostName (if applicable) in App Settings. Learn how to get them at https://aka.ms/qnaabssetup.');
else {
if (endpointHostName == null || endpointHostName == '')
// Replace with Preview QnAMakerDialog service
session.replaceDialog('basicQnAMakerPreviewDialog');
else
// Replace with GA QnAMakerDialog service
session.replaceDialog('basicQnAMakerDialog');
}
}
]);
var hasSaidHi = false;
bot.set('storage', tableStorage);
bot.on('conversationUpdate', (message) => {
if (message.membersAdded && !hasSaidHi) {
const hello = new builder.Message()
.address(message.address)
.text("Hi, I am Bot. I can answer your question");
bot.send(hello);
bot.beginDialog(message.address, '*:/');
hasSaidHi = true;
}
});
// Recognizer and and Dialog for preview QnAMaker service
var previewRecognizer = new builder_cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
authKey: process.env.QnAAuthKey || process.env.QnASubscriptionKey
});
var basicQnAMakerPreviewDialog = new builder_cognitiveservices.QnAMakerDialog({
recognizers: [previewRecognizer],
defaultMessage: 'Sorry, I do not get what you just said',
qnaThreshold: 0.3
}
);
bot.dialog('basicQnAMakerPreviewDialog', basicQnAMakerPreviewDialog);
// Recognizer and and Dialog for GA QnAMaker service
var recognizer = new builder_cognitiveservices.QnAMakerRecognizer({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
authKey: process.env.QnAAuthKey || process.env.QnASubscriptionKey, // Backward compatibility with QnAMaker (Preview)
endpointHostName: process.env.QnAEndpointHostName
});
var basicQnAMakerDialog = new builder_cognitiveservices.QnAMakerDialog({
recognizers: [recognizer],
defaultMessage: 'Sorry, I do not get what you just said',
qnaThreshold: 0.3
}
);
bot.dialog('basicQnAMakerDialog', basicQnAMakerDialog);
/*
bot.dialog('/', //basicQnAMakerDialog
[
function (session) {
session.send("!!");
var qnaKnowledgebaseId = process.env.QnAKnowledgebaseId;
var qnaAuthKey = process.env.QnAAuthKey || process.env.QnASubscriptionKey;
var endpointHostName = process.env.QnAEndpointHostName;
// QnA Subscription Key and KnowledgeBase Id null verification
if ((qnaAuthKey == null || qnaAuthKey == '') || (qnaKnowledgebaseId == null || qnaKnowledgebaseId == ''))
session.send('Please set QnAKnowledgebaseId, QnAAuthKey and QnAEndpointHostName (if applicable) in App Settings. Learn how to get them at https://aka.ms/qnaabssetup.');
else {
if (endpointHostName == null || endpointHostName == '')
// Replace with Preview QnAMakerDialog service
session.replaceDialog('basicQnAMakerPreviewDialog');
else
// Replace with GA QnAMakerDialog service
session.replaceDialog('basicQnAMakerDialog');
}
}
]);
*/