处理 Microsoft bot 框架中的多个对话框

Handling multiple dialogs in Microsoft bot framework

我正在使用 Microsoft bot 框架创建一个 bot,该 bot 将接收餐厅的订单,我想知道如何处理多个对话,例如客户下了第一个订单,然后我想要机器人问你还想要别的东西吗?然后客户说 yes/no ,以防再次重复相同的 dailog 并保持第一个 dailog 的状态,我现在在文档中看到的只是一个对话和一个对话。

非常感谢

要管理多个对话框,您需要使用 Dialog Chains. You can either manage the stack of dialogs explicitly () or implicitly using the Chain fluent methods. Here 是如何使用它的示例。

如果用户可以 select 设置的东西已经预定义,那么我会推荐使用 FormFlow. The Pizza & Sandwich 样本是如何使用预定义选项集处理订单的很好的例子。

对于 Microsoft Bot Framework V4 版本,FormFlow 需要替换为 Waterfall Dialog。在这里,我们可以使用 stepContext.Values(字典)来维护跨瀑布步骤的状态,并向用户提供是或否响应的选择提示,然后在响应是是的情况下重复瀑布对话框,否则在最后一个瀑布步骤中结束对话框。

在基础的构造函数中添加下面的瀑布 Component dialog 并根据用户选择重复瀑布。

WaterfallStep[] myWaterfallDialog = new WaterfallStep[]
{ 
    this.waterfallStepToGetUserOrder,
    .......
    this.promptUserForChoiceStep,
    this.EndDialogStep
}
AddDialog(new WaterfallDialog("mydialog", myWaterfallDialog);

以上答案很好,但我注意到提供的一些链接不再有效。这是我如何设法在不同的对话框之间导航

    public MakeChoiceDialog() : base (nameof(MakeChoiceDialog))
    {
        AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
        AddDialog(new LoginDialog());
        AddDialog(new SignUpDialog());

        AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                LoginStepAsync,
                LoginSignUpStepAsync,
                //Other Steps here
            }));

        InitialDialogId = nameof(WaterfallDialog);
    }

方法调用将是

    private async Task<DialogTurnResult> LoginSignUpStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        string userChoice = ((FoundChoice)stepContext.Result).Value;
        var msg = string.Empty;
        switch (userChoice)//You can use if statement here
        {
            case "Login":
                return await stepContext.BeginDialogAsync(nameof(LoginDialog), null, cancellationToken);
            default:                  
              return await stepContext.BeginDialogAsync(nameof(SignUpDialog), null, cancellationToken);
        }
        return await stepContext.EndDialogAsync(null, cancellationToken);
    }

Startup.cs中添加以下内容

services.AddSingleton<LoginDialog>();
services.AddSingleton<SignUpDialog>();