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

时间:2016-05-11 17:20:29

标签: botframework

我正在使用Microsoft bot框架创建机器人,机器人将接收餐馆的订单,我想知道如何处理多个对话框,例如客户发出第一个订单,然后我想要机器人到问你想要别的吗?然后客户说是/否,因为保持第一个的状态再次重复相同的dailog,我现在在文档中看到的只是一个对话和一个对话。

非常感谢

3 个答案:

答案 0 :(得分:14)

要管理多个对话框,您需要使用Dialog Chains。您可以显式管理对话框堆栈(using Call/Done),也可以使用Chain fluent方法隐式管理。 Here是如何使用它的示例。

如果用户可以选择的一组内容已经预定义,那么我建议使用FormFlowPizza& Sandwich样本是如何使用一组预定义选项处理订单的好例子。

答案 1 :(得分:0)

对于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);

答案 2 :(得分:0)

以上答案很好,但我注意到所提供的某些链接不再起作用。这是我设法在不同对话框之间导航的方式

    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>();
相关问题