在Bot框架C#中自定义验证消息



如何在Bot框架表单流中自定义验证消息?下面是我正在使用的示例代码,如果用户键入所提供选项以外的任何文本,我需要给他们返回选项列表,说明所选选项不正确。

[Prompt("Please choose # category... {||}", ChoiceStyle = ChoiceStyleOptions.Buttons)]
public string Category;
[Prompt("Please choose  # sub category... {||}", ChoiceStyle = ChoiceStyleOptions.Buttons)]
public string Subcategory;
[Prompt("We need some more details to create the request, provide me your **contact number**...")]
[Pattern(Validations.Phone)]
public string ContactNumber;
[Prompt("Please provide **Attachement** if you don't have attachemnt please enter ? {||}")]
public ChoiceOptions? Attachment;

目前,如果该选项不在列表中,它只会抛出一条类似的消息

">用户输入不是联系人/附件/类别选项">

Jobin,您想了解formFlow的更高级功能。通过使用字段验证函数,可以围绕字段创建业务逻辑。这是一个从MS Online文档中截取的示例。您可以在此处查看完整的样本和文档我发现这种创建表单的方法比使用属性更灵活。

public static IForm<SandwichOrder> BuildForm()
{
...
return new FormBuilder<SandwichOrder>()
.Message("Welcome to the sandwich order bot!")
.Field(nameof(Sandwich))
.Field(nameof(Bread))
.Field(nameof(Toppings),
validate: async (state, value) =>
{
var values = ((List<object>)value).OfType<ToppingOptions>();
var result = new ValidateResult { IsValid = true, Value = values };
if (values != null && values.Contains(ToppingOptions.Everything))
{
result.Value = (from ToppingOptions topping in Enum.GetValues(typeof(ToppingOptions))
where topping != ToppingOptions.Everything && !values.Contains(topping)
select topping).ToList();
}
return result;
})
.Message("For sandwich toppings you have selected {Toppings}.")
...
.Build();
}

相关内容

  • 没有找到相关文章

最新更新