C# 机器人对话框步骤验证



嗨,我正在使用 .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);
}

}

}

不幸的是,文档对此还远未明确。

Validations-object 用于将对象传递给自定义验证程序函数。当您希望在运行时更改验证时,这很有用。也就是说,验证取决于将提示添加到 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;}
}

最新更新