C#Bot对话框步骤验证

时间:2020-01-30 19:44:33

标签: .net-core botframework waterfall

嗨,我正在使用.net核心实现一个机器人。我正在使用瀑布对话框进行对话。问题是,在一个步骤和另一个步骤之间,我需要验证通过数据库接收到的信息。我尝试在提示选项中使用验证,但是我无法做到这一点。有人能帮我吗。这是我的代码。

public class DetectProblemStep : ComponentDialog
{
    // Define a "done" response for the company selection prompt.
    private const string DoneOption = "done";

    // Define value names for values tracked inside the dialogs.
    private const string UserInfo = "value-userInfo";

    public DetectProblemStep()
        : base(nameof(DetectProblemStep))
    {
        AddDialog(new TextPrompt(nameof(TextPrompt)));
        AddDialog(new NumberPrompt<int>(nameof(NumberPrompt<int>)));

        AddDialog(new ReviewSelectionDialog());

        AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
        {
            NameStepAsync,
            EmailStepAsync
        }));

        InitialDialogId = nameof(WaterfallDialog);
    }

    private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        // Create an object in which to collect the user's information within the dialog.
        stepContext.Values[UserInfo] = new UserProfile();

        var promptOptions = new PromptOptions { Prompt = MessageFactory.Text("Please enter your name."),
            Validations = nameof(NameValidation)
        };

        // Ask the user to enter their name.
        return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
    }

    private static async Task<DialogTurnResult> EmailStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        // Create an object in which to collect the user's information within the dialog.
        stepContext.Values[UserInfo] = new UserProfile();

        var promptOptions = new PromptOptions
        {
            Prompt = MessageFactory.Text("Please enter your Email."),
        };

        // Ask the user to enter their name.
        return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
    }



    public Task<bool> NameValidation(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
    {

        return Task.FromResult(false);
    }


}

}

1 个答案:

答案 0 :(得分:0)

不幸的是,文档尚不清楚。

Validations对象用于将对象传递到自定义验证器函数。当您想在运行时更改验证时,这很有用。也就是说,验证取决于将提示添加到DialogSet时没有的信息(在构造函数中进行此操作)。

如果在将提示添加到DialogSet时可以确定您的验证,则只需执行以下操作:

AddDialog(new TextPrompt(nameof(TextPrompt), NameValidation));

但是,如果您想在实际调用PromptAsync()时在提示中添加其他验证,则可以将任何内容传递给验证器,如下所示:

private static async Task<DialogTurnResult> NameStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Create an object in which to collect the user's information within the dialog.
    stepContext.Values[UserInfo] = new UserProfile();

    var validations = new AdditionalValidationOptions { MinLength = 3 };

    var promptOptions = new PromptOptions { Prompt = MessageFactory.Text("Please enter your name."),
        Validations = validations 
    };

    // Ask the user to enter their name.
    return await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken);
}

public Task<bool> NameValidation(PromptValidatorContext<string> promptContext, CancellationToken cancellationToken)
{
    var validations = (AdditionalValidationOptions)promptContext.Options.Validations;

    return Task.FromResult(promptContext.Recognized.Value >= validations.MinLength);
}

并定义一个类

public class AdditionalValidationOptions 
{
    public int MinLength {get; set;}
}