如何在对话中调用瀑布对话 - Azure 机器人生成器



在我的 azure bot 中,我有默认的机器人"DialogBot.cs"。在其OnMessageActivityAsync((方法中,我想根据用户输入调用特定的瀑布。

但是,一旦我解析了输入,我就不知道如何触发特定的瀑布。假设瀑布称为"特定对话"。我试过这个:

await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(SpecificDialog)), cancellationToken);

但这行不通。我将如何实现这一点?

我假设您正在使用其中一个示例。我将基于CoreBot的答案。

应将Dialog.RunAsync()调用的对话框视为"根"或"父"对话框,所有其他对话框都从中分支和流出。若要更改由此调用的对话框,请在Startup.cs中查找如下所示的行:

// Create the bot as a transient. In this case the ASP Controller is expecting an IBot.
services.AddTransient<IBot, DialogAndWelcomeBot<MainDialog>>();

要将其更改为MainDialog以外的对话框,您只需将其替换为相应的对话框即可。

进入根或父对话后,您可以使用BeginDialogAsync()调用另一个对话框:

stepContext.BeginDialogAsync(nameof(BookingDialog), new BookingDetails(), cancellationToken);

仅供参考:

这在 Node 中的工作方式略有不同。在 CoreBot 中,MainDialog 以index.js方式传递给机器人:

const dialog = new MainDialog(luisRecognizer, bookingDialog);
const bot = new DialogAndWelcomeBot(conversationState, userState, dialog);
[...]
// Listen for incoming activities and route them to your bot main dialog.
server.post('/api/messages', (req, res) => {
// Route received a request to adapter for processing
adapter.processActivity(req, res, async (turnContext) => {
// route to bot activity handler.
await bot.run(turnContext);

您可以看到它调用DialogAndWelcomeBot,这扩展了 DialogBot,它对每条消息调用MainDialog

this.onMessage(async (context, next) => {
console.log('Running dialog with Message Activity.');
// Run the Dialog with the new message Activity.
await this.dialog.run(context, this.dialogState);
// By calling next() you ensure that the next BotHandler is run.
await next();
});

不必以这种方式设置机器人,但这是当前推荐的设计,如果遵循此操作,你将更轻松地实现我们的文档和示例。

最新更新