forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
54 lines (46 loc) · 2.89 KB
/
bot.js
File metadata and controls
54 lines (46 loc) · 2.89 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { ActivityTypes } = require('botbuilder');
const { QnAMaker } = require('botbuilder-ai');
/**
* A simple bot that responds to utterances with answers from QnA Maker.
* If an answer is not found for an utterance, the bot responds with help.
*/
class QnAMakerBot {
/**
* The QnAMakerBot constructor requires one argument (`endpoint`) which is used to create an instance of `QnAMaker`.
* @param {QnAMakerEndpoint} endpoint The basic configuration needed to call QnA Maker. In this sample the configuration is retrieved from the .bot file.
* @param {QnAMakerOptions} config An optional parameter that contains additional settings for configuring a `QnAMaker` when calling the service.
*/
constructor(endpoint, qnaOptions) {
this.qnaMaker = new QnAMaker(endpoint, qnaOptions);
}
/**
* Every conversation turn for our QnAMakerBot will call this method.
* There are no dialogs used, since it's "single turn" processing, meaning a single request and
* response, with no stateful conversation.
* @param {TurnContext} turnContext A TurnContext instance, containing all the data needed for processing the conversation turn.
*/
async onTurn(turnContext) {
// By checking the incoming Activity type, the bot only calls QnA Maker in appropriate cases.
if (turnContext.activity.type === ActivityTypes.Message) {
// Perform a call to the QnA Maker service to retrieve matching Question and Answer pairs.
const qnaResults = await this.qnaMaker.generateAnswer(turnContext.activity.text);
// If an answer was received from QnA Maker, send the answer back to the user.
if (qnaResults[0]) {
await turnContext.sendActivity(qnaResults[0].answer);
// If no answers were returned from QnA Maker, reply with help.
} else {
await turnContext.sendActivity('No QnA Maker answers were found. This example uses a QnA Maker Knowledge Base that focuses on smart light bulbs. To see QnA Maker in action, ask the bot questions like "Why won\'t it turn on?" or "I need help."');
}
// If the Activity is a ConversationUpdate, send a greeting message to the user.
} else if (turnContext.activity.type === ActivityTypes.ConversationUpdate &&
turnContext.activity.recipient.id !== turnContext.activity.membersAdded[0].id) {
await turnContext.sendActivity('Welcome to the QnA Maker sample! Ask me a question and I will try to answer it.');
// Respond to all other Activity types.
} else if (turnContext.activity.type !== ActivityTypes.ConversationUpdate) {
await turnContext.sendActivity(`[${ turnContext.activity.type }]-type activity detected.`);
}
}
}
module.exports.QnAMakerBot = QnAMakerBot;