FormFlow定义字符串字段

时间:2018-04-13 12:59:31

标签: botframework formflow

我正在使用bot框架,我正在尝试使用FormFlow动态定义表单。我有一个特定领域的问题:

.Field(new FieldReflector<IssueFormModel>(nameof(IssueResultModel.ProductPlatform))
                .SetType(typeof(string))
                .SetDefine(async (issue, field) =>
                {
                    if (issue.ProductName != null)
                    {
                        foreach (var platform in productsWithPlatforms[issue.ProductName])
                        {
                            field.AddDescription(platform, platform).AddTerms(platform, platform);
                        }
                    }

                    return await Task.FromResult(true);
                })
                .SetPrompt(new PromptAttribute("Which platform of {ProductName}{||}?"))
                .SetAllowsMultiple(false)
                .SetValidate(async (state, value) => await ValidateProductPlatform(value, state)))

问题是 ProductPlatform 依赖于 ProductName ,因此它是一个字符串。这样可以正常工作,但是,这样,机器人不会显示可能平台的选项(尽管 SetPrompt 中有{||})。

当我将type设置为null SetType(null)时,僵尸程序现在将可能的平台显示为按钮,但是,当用户决定键入错误的平台而不是单击时,它永远不会转到 ValidateProductPlatform 正确的(我猜验证本身已经在 SetDefine 级别完成)。我需要通过 ValidateProductPlatform 验证用户输入的唯一原因是我想在3次尝试失败后取消表单。

那么,有没有办法实现这个目标?: 用户具有基于ProductName的ProductPlaftorm选项(作为按钮),但不是单击,而是(可能)键入错误的平台,并且在3次错误尝试后,表单结束。

PS:我看到Microsoft Bot : How to capture Too Many Attempts in Form Flow?但是我无法使用它,因为在我的情况下(SetType(null)

时,似乎忽略了 SetValidate

1 个答案:

答案 0 :(得分:0)

创建使用按钮和自定义验证方法(在文本输入和按下按钮上)的提示器的一种技术是在表单上插入提示符覆盖。 对于您的特定用例,这看起来类似于以下代码块(此实现将覆盖ProductPlatform字段上的原始提示,以便向卡添加按钮菜单):

form.Prompter(async (context, prompt, state, field) =>
{
    //this part is added in on top of the original implementation of form.Prompter
    if (field.Name == nameof(IssueFormModel.ProductPlatform) && prompt.Buttons.Count == 0)
    {
        foreach (var fieldValue in field.Values)
        {
            var asString = fieldValue as string;
            prompt.Buttons.Add(new DescribeAttribute(asString, title: asString));
        }
    }
    //this check prevents prompts that are sent through from ValidateResult without a FeedbackCard from crashing the bot.
    if (prompt.Description != null) {
        var preamble = context.MakeMessage();
        var promptMessage = context.MakeMessage();
        if (prompt.GenerateMessages(preamble, promptMessage))
        {
            await context.PostAsync(preamble);
        }
        await context.PostAsync(promptMessage);
    }
    return prompt;
});

这意味着您构建表单的整个方法应该如下所示:

private IForm<IssueFormModel> BuildProductForm()
{
    var form = new FormBuilder<IssueFormModel>()
       .Field(new FieldReflector<IssueFormModel>(nameof(IssueFormModel.ProductName))
             .SetPrompt(new PromptAttribute("Type either product1 or product2"))
             .SetValidate(async (state, value) => await ValidateProductName(value, state)))
       .Field(new FieldReflector<IssueFormModel>(nameof(IssueFormModel.ProductPlatform))
             .SetType(typeof(string))
             .SetDefine(async (issue, field) =>
             {
                 if (issue.ProductName != null)
                 {
                     foreach (var platform in productsWithPlatforms[issue.ProductName])
                     {
                         field.AddDescription(platform, platform).AddTerms(platform, platform);
                     }
                 }

                 return await Task.FromResult(true);
             })
             .SetPrompt(new PromptAttribute("Which platform of {ProductName}{||}?"))
             .SetAllowsMultiple(false)
             .SetValidate(async (state, value) => await ValidateProductPlatform(value, state)))
       .AddRemainingFields()
       .Confirm(prompt: "Is this your issue? {*}{||}");

    form.Prompter(async (context, prompt, state, field) =>
    {
        if (field.Name == nameof(IssueFormModel.ProductPlatform) && prompt.Buttons.Count == 0)
        {
            foreach (var fieldValue in field.Values)
            {
                var asString = fieldValue as string;
                prompt.Buttons.Add(new DescribeAttribute(asString, title: asString));
            }
        }
        if (prompt.Description != null) {
            var preamble = context.MakeMessage();
            var promptMessage = context.MakeMessage();
            if (prompt.GenerateMessages(preamble, promptMessage))
            {
                await context.PostAsync(preamble);
            }
            await context.PostAsync(promptMessage);
        }
        return prompt;
    });

    return form.Build();

}

这里的主要变化是在最后一次调用FormBuilder之后插入form.Prompter而不是立即返回新表单。重写的提示使用类型&#34;字符串&#34;它会调用您的自定义验证,并且您应该可以在其他任何内容中执行您不喜欢的条目。