-
Notifications
You must be signed in to change notification settings - Fork 0
/
answerDialog.js
executable file
·115 lines (105 loc) · 3.36 KB
/
answerDialog.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
const {
ComponentDialog,
TextPrompt,
WaterfallDialog,
} = require("botbuilder-dialogs");
const { QnAMaker } = require("botbuilder-ai");
const { MessageFactory } = require("botbuilder");
const { ActionTypes } = require("botframework-schema");
const ANSWER_DIALOG = "ANSWER_DIALOG";
const WATERFALL_DIALOG = "WATERFALL_DIALOG";
const TEXT_PROMPT = "TEXT_PROMPT";
/**
* The dialog to do QnA with user
* @class AnswerDialog
* @extends {ComponentDialog}
*/
class AnswerDialog extends ComponentDialog {
constructor() {
super(ANSWER_DIALOG);
try {
// create qnaMaker
this.qnaMaker = new QnAMaker({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
endpointKey: process.env.QnAEndpointKey,
host: process.env.QnAEndpointHostName,
});
} catch (err) {
console.warn(
`QnAMaker Exception: ${err} Check your QnAMaker configuration in .env`
);
}
this.addDialog(new TextPrompt(TEXT_PROMPT));
this.addDialog(
new WaterfallDialog(WATERFALL_DIALOG, [
this.beginStep.bind(this),
this.answerStep.bind(this),
])
);
this.initialDialogId = WATERFALL_DIALOG;
}
/**
* A waterfall step to guide user when user enters the QnA mode or to answer the question user has asked if the dialog is restarted.
* @param {*} stepContext
* @return {*}
* @memberof AnswerDialog
*/
async beginStep(stepContext) {
const msg = stepContext.options.restartMsg
? stepContext.options.restartMsg
: "Welcome! You can go on ask questions, or click `quit` to quit the QnA mode whenever feeling like so.";
// user can quit QnA anytime by clicking `quit`
const cardActions = [
{
type: ActionTypes.PostBack,
title: "quit",
value: "quit",
},
];
const message = MessageFactory.suggestedActions(cardActions, msg);
// Ask the user to enter their name.
return await stepContext.prompt(TEXT_PROMPT, message);
}
/**
* A waterfall step to fetch answer from KB and send to user.
* @param {*} stepContext
* @return {*}
* @memberof AnswerDialog
*/
async answerStep(stepContext) {
if (stepContext.result == "quit") {
return await stepContext.endDialog();
}
console.log("Calling QnA Maker");
try {
const qnaResults = await this.qnaMaker.getAnswers(stepContext.context);
var msg = "";
// If an answer was received from QnA Maker, send the answer back to the user.
if (qnaResults[0]) {
msg = qnaResults[0].answer;
// If no answers were returned from QnA Maker, reply with help.
} else {
msg =
"Sorry, I haven't got this one. Please contact the administrator for feeding relevant material to my knowledge base at https://www.qnamaker.ai.";
}
} catch (error) {
console.log(error);
await stepContext.context.sendActivity(
"Sorry, QnA maker is currently unavailable."
);
return await stepContext.endDialog();
}
return await stepContext.replaceDialog(this.initialDialogId, {
restartMsg: msg,
});
}
/**
* QnAMaker setter
* @memberof AnswerDialog
*/
set a(n) {
this.qnaMaker = n;
}
}
module.exports.AnswerDialog = AnswerDialog;
module.exports.ANSWER_DIALOG = ANSWER_DIALOG;