FormFlow 中的条件字段

Conditional Fields in FormFlow

我目前正在使用 Azure Bot Framework 开发一个简单的聊天机器人应用程序,使用 C# 和 formflow 实现,并在 Bot Emulator V4 中进行测试。

到目前为止我还不错,主要使用直觉和一些在线资源,但我最近遇到了一些障碍,发现 FormFlow 中关于条件字段的文档相当稀少。

简而言之,场景如下:

实际上,只有当用户对倒数第二个问题的回答是 'yes' 时,后者才应该可见。如果用户回答 'no' 有任何问题,机器人应该忽略 'problem description' 字段。

目前,我有:

  public enum HadProblem
{
    Yes, No
};

选项。

[Prompt("Have you had a problem? {||}")]
      public HadProblem? Problem;

一个提示,'yes' 和 'no' 选项对 select 可见。

最后一题只是简单的字符串输入:

[Prompt("Please give a {&} of the problem.")]
    //[Optional]
        public string description;

目前,如您所见,我一直在使用“[optional]”标签,因为它是条件字段最接近的替代品。我正在努力寻找涵盖如何创建字段的文档,该字段的外观取决于另一个字段值。

有没有办法让问题描述字段成为visible/answerable,只有如果对'HadProblem'的回应是'Yes'?

如果答案是 'no',不得不手动跳过问题,这真的很不整洁。

提前致谢。

您可以对表单本身使用 Contoso Flowers Bot 示例中 Field<T> class, you can find complete samples in the documentation including Multi-Dialog Bot sample and Contoso Flowers Bot sample and Order.csSetActiveSetNext 方法。

namespace ContosoFlowers.Models
{
    using System;
    using BotAssets;
    using Microsoft.Bot.Builder.FormFlow;
    using Microsoft.Bot.Builder.FormFlow.Advanced;
    using Services.Models;

    [Serializable]
    public class Order
    {
        public enum UseSaveInfoResponse
        {
            Yes,
            Edit
        }

        public string OrderID { get; set; }

        [Prompt]
        public string RecipientFirstName { get; set; }

        [Prompt(FieldCase = CaseNormalization.None)]
        public string RecipientLastName { get; set; }

        [Prompt]
        [Pattern(RegexConstants.Phone)]
        public string RecipientPhoneNumber { get; set; }

        [Prompt]
        [Pattern(@"^.{1,200}$")]
        public string Note { get; set; }

        [Prompt]
        [Pattern(RegexConstants.Email)]
        public string SenderEmail { get; set; }

        [Prompt]
        [Pattern(RegexConstants.Phone)]
        public string SenderPhoneNumber { get; set; }

        public string SenderFirstName { get; set; }

        public string SenderLastName { get; set; }

        public bool AskToUseSavedSenderInfo { get; set; }

        [Prompt]
        public UseSaveInfoResponse? UseSavedSenderInfo { get; set; }

        [Prompt]
        public bool SaveSenderInfo { get; set; }

        public string DeliveryAddress { get; set; }

        public string FlowerCategoryName { get; set; }

        public Bouquet Bouquet { get; set; }

        public DateTime DeliveryDate { get; set; }

        public string BillingAddress { get; set; }

        public bool Payed { get; set; }

        public PaymentDetails PaymentDetails { get; set; }

        public static IForm<Order> BuildOrderForm()
        {
            return new FormBuilder<Order>()
                .Field(nameof(RecipientFirstName))
                .Field(nameof(RecipientLastName))
                .Field(nameof(RecipientPhoneNumber))
                .Field(nameof(Note))
                .Field(new FieldReflector<Order>(nameof(UseSavedSenderInfo))
                    .SetActive(state => state.AskToUseSavedSenderInfo)
                    .SetNext((value, state) =>
                    {
                        var selection = (UseSaveInfoResponse)value;

                        if (selection == UseSaveInfoResponse.Edit)
                        {
                            state.SenderEmail = null;
                            state.SenderPhoneNumber = null;
                            return new NextStep(new[] { nameof(SenderEmail) });
                        }
                        else
                        {
                            return new NextStep();
                        }
                    }))
                .Field(new FieldReflector<Order>(nameof(SenderEmail))
                    .SetActive(state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
                    .SetNext(
                        (value, state) => (state.UseSavedSenderInfo == UseSaveInfoResponse.Edit)
                        ? new NextStep(new[] { nameof(SenderPhoneNumber) })
                        : new NextStep()))
                .Field(nameof(SenderPhoneNumber), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
                .Field(nameof(SaveSenderInfo), state => !state.UseSavedSenderInfo.HasValue || state.UseSavedSenderInfo.Value == UseSaveInfoResponse.Edit)
                .Build();
        }
    }
}