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

Customize validation message in Bot framework 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;

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

' Userinput 不是 contact/attachment/category 选项'

乔宾, 您想查看 formFlow 的更高级功能。您可以使用字段验证函数围绕您的字段创建业务逻辑。这是从 MS Online 文档中获取的示例。你可以看到 full sample and docs here 我发现这种创建表单的方法比使用属性灵活得多。

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();
}