I am creating a chat bot using azure bot framework in Nodejs. QnA maker to store question answers and one LUIS app. Now I want to detect end of conversation(either by checking no reply from long time or refreshing a webpage) and add feedback card at the end of conversation.
问题:
回答1:
You can achieve this by use of the onEndDialog
method and the use of a separate class to manage the feedback process.
First, I have a component dialog that imports the feedback.js
file and calls the associated onTurn()
method within onEndDialog
.
Next, I create the mainDialog.js file in which MainDialog extends FeedbackDialog
. In this way, FeedbackDialog
sits "on top" of MainDialog
listening for specific user inputs or activities. In this case, it is listening for EndDialog()
to be called. You will likely want to add additional validation to be sure it only fires when the EndDialg()
you want is called.
Lastly, in the feedback.js
file, this is where your feedback code/logic lives. For simplicity, I'm using a community project, botbuilder-feedback
, for generating a user feedback interface. The majority of the code is focused on creating and managing the "base" dialog. Additional dialog activity comes from within the botbuilder-feedback
package.
For reference, this code is based partly on the 13.core-bot sample found in the Botbuilder-Samples repo.
Hope of help!
feedbackDialog.js:
const { ComponentDialog } = require('botbuilder-dialogs');
const { Feedback } = require('./feedback');
class FeedbackDialog extends ComponentDialog {
constructor() {
super();
this.feedback = new Feedback();
}
async onEndDialog ( innerDc ) {
return await this.feedback.onTurn( innerDc );
}
}
module.exports.FeedbackDialog = FeedbackDialog;
mainDialog.js:
const { FeedbackDialog } = require( './feedbackDialog' );
class MainDialog extends FeedbackDialog {
[...]
}
module.exports.MainDialog = MainDialog;
feedback.js:
const { ActivityTypes } = require('botbuilder');
const { DialogTurnStatus } = require('botbuilder-dialogs');
const Botbuilder_Feedback = require('botbuilder-feedback').Feedback;
class Feedback {
async onTurn(turnContext, next) {
if (turnContext.activity.type === ActivityTypes.Message) {
await Botbuilder_Feedback.sendFeedbackActivity(turnContext, 'Please rate this dialog');
return { 'status': DialogTurnStatus.waiting };
} else {
return { 'status': DialogTurnStatus.cancelled };
}
await next();
};
}
module.exports.Feedback = Feedback;