-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
41 lines (36 loc) · 1.57 KB
/
bot.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const {ActivityHandler, MessageFactory} = require('botbuilder');
const {QnAMaker} = require('botbuilder-ai');
class ScreamBot extends ActivityHandler {
constructor(configuration, qnaOptions) {
super();
if (!configuration || !configuration.knowledgeBaseId) { throw new Error('QnA Maker configuration is required.'); }
// create a qnaMaker connector, telemetry client in options
this.qnaMaker = new QnAMaker(configuration, qnaOptions);
this.onMessage(async (context, next) => {
// send user input to QnA Maker.
const qnaResults = await this.qnaMaker.getAnswers(context);
// Send back the QnA answer, if it exists
if (qnaResults[0]) {
await context.sendActivity(qnaResults[0].answer);
} else {
// If no answers were returned from QnA Maker, reply with blanket response.
await context.sendActivity(configuration.unknownText || 'What\'s that noise?');
}
await next();
});
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
const welcomeText = configuration.welcomeText || 'Hello.';
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await context.sendActivity(MessageFactory.text(welcomeText, welcomeText));
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
}
module.exports.ScreamBot = ScreamBot;