如何在 Dialog - Azure Bot builder 中调用瀑布对话

How to call a waterfall dialog in a Dialog - Azure Bot builder

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

然而,一旦我解析了输入,我就不知道如何触发特定的瀑布。假设瀑布被称为 'SpecificDialog'。我试过这个:

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

但这不起作用。我将如何做到这一点?

我假设您正在使用其中一个样本。我的回答基于 CoreBot.

您应该将 Dialog.RunAsync() 调用的对话视为 "root" 或 "parent" 对话,所有其他对话都从该对话分支和流动。要更改由此调用的对话框,请查看 Startup.cs for a line that looks like this:

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

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

进入根对话框或父对话框后,您 call another dialog with BeginDialogAsync():

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

仅供参考:

这在 Node.js 中的工作方式略有不同。在 CoreBot 中,the MainDialog is passed to the bot in 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, which extends DialogBot, which calls MainDialog on every message:

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();
    });

没有以这种方式设置您的机器人,但这是目前推荐的设计,如果您遵循,您将更容易实施我们的文档和示例这个。