如何在bot框架javascript中从welcome开始一个对话框



我想用javascript在bot框架sdk中开始一个多层次的多选择对话框。我有一个主对话框(finalAnswerDialog),它调用LUIS来预测意图,还有一个多层次的多选择菜单对话框(menuDialog)来引导用户到一些意图。我希望机器人从一开始就显示菜单对话框,但我总是得到错误。下面是我的代码:

dialogAndWelcomeBot

const { CardFactory } = require('botbuilder');
const { DialogBot } = require('./dialogBot');
const { ActivityHandler, MessageFactory } = require('botbuilder');
const { ActionTypes } = require('botframework-schema');
const { MenuDialog } = require('../dialogs/menuDialog');
class DialogAndWelcomeBot extends DialogBot {
constructor(conversationState, userState, dialog) {
super(conversationState, userState, dialog);
this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; cnt++) {
if (membersAdded[cnt].id !== context.activity.recipient.id) {
await dialog.run(context, conversationState.createProperty('DialogState'));
await step.beginDialog('menuDialog');
}
}
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}
}

module.exports.DialogAndWelcomeBot = DialogAndWelcomeBot;

menuDialog谁调用finalAnswerDialog来管理任意点的LUIS调用

class MenuDialog extends ComponentDialog {
constructor(finalAnswerDialog) {
super('menuDialog');
this.answered=true;
this.addDialog(new TextPrompt(TEXT_PROMPT));
this.addDialog(new TextPrompt(NAME_PROMPT));
this.addDialog(new ChoicePrompt(CHOICE_PROMPT));
this.addDialog(new ConfirmPrompt(CONFIRM_PROMPT));
this.addDialog(finalAnswerDialog);
this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
this.firstQuestion.bind(this),
this.secondQuestion.bind(this),
this.thirdQuestion.bind(this),
this.answerTheQuestion.bind(this),
this.finalStep.bind(this)
]));

this.initialDialogId = WATERFALL_DIALOG;
}

finalAnswerDialog

class FinalAnswerDialog extends ComponentDialog {
constructor(luisRecognizer) {
super('finalAnswerDialog');
if (!luisRecognizer) throw new Error('[MainDialog]: Missing parameter 'luisRecognizer' is required');
this.luisRecognizer = luisRecognizer;
// Define the main dialog and its related components.
// This is a sample "book a flight" dialog.
this.addDialog(new TextPrompt('TextPrompt'));
this.addDialog(new WaterfallDialog(MAIN_WATERFALL_DIALOG, [
//this.initStep.bind(this),
this.actStep.bind(this),
]));
this.initialDialogId = MAIN_WATERFALL_DIALOG;
}
async run(turnContext, accessor) {
const dialogSet = new DialogSet(accessor);
dialogSet.add(this);
const dialogContext = await dialogSet.createContext(turnContext);
const results = await dialogContext.continueDialog();
if (results.status === DialogTurnStatus.empty) {
await dialogContext.beginDialog(this.id);
}
}
async initStep(stepContext) {
return await stepContext.prompt(TEXT_PROMPT, promptOptions);
}
async actStep(stepContext) {
if (!this.luisRecognizer.isConfigured) {
const messageText = 'NOTE: LUIS is not configured. To enable all capabilities, add `LuisAppId`, `LuisAPIKey` and `LuisAPIHostName` to the .env file.';
await stepContext.context.sendActivity(messageText, null, InputHints.IgnoringInput);
return await stepContext.next();
}
const luisResult = await this.luisRecognizer.executeLuisQuery(stepContext.context);
let top_intent = LuisRecognizer.topIntent(luisResult)
//let top_intent="contacto"
console.log("intent predicha por Luis: " + top_intent)

switch (top_intent) {
//SendActivity

index.js

const dialog = new FinalAnswerDialog(luisRecognizer);
const bot = new DialogAndWelcomeBot(conversationState, userState, dialog);

如何在欢迎中启动菜单对话框?

是,您可以从欢迎代码启动特定的(菜单/任何)对话框。

这里的关键是不要使用您的bot代码中的conversationUpdate,而是从客户端提交事件活动,并添加一些代码来处理您的bot代码中的事件。

要以事件处理程序的形式添加活动,请参阅如何发送问候消息和常见问题


还有另一种解决方案,即存储user-id和session的映射,然后使用session。Send将对话框发送给相应的用户。在大多数情况下,使用conversationUpdate可能是您的最佳选择。

Demo欢迎代码:

bot.on('conversationUpdate', function(message) {
if (message.membersAdded) {
message.membersAdded.forEach(function(identity) {
if (identity.id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.text("Welcome");
bot.send(reply);
}
});
}
});

要调用特定的对话框,请这样做:

bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, '/main');
}
});
}
});
bot.dialog('/main', [
function (session, args, next) {
session.send("Glad you could join.");
session.beginDialog('/next');
}
]);

上面的代码是欢迎消息和启动对话框的组合。你也可以在这里找到一个类似的SO线程。

相关内容

  • 没有找到相关文章

最新更新