简体   繁体   English

验证FormFlow中的字段

[英]Validating a field in a FormFlow

Code

 [Serializable]
    public class FAQConversation
    {
        [Prompt("What product is your concern? {||}")]
        public VASetting.SupportedProducts Products { get; set; }
        [Prompt("Okay, tell me what is your question. Enter \"back\" to go back to Products Selection.")]
        public string Inquiry { get; set; }
        public static IForm<FAQConversation> BuildForm()
        {
            return new FormBuilder<FAQConversation>()
                .AddRemainingFields()
                .Field(new FieldReflector<FAQConversation>(nameof(Inquiry)).SetValidate(AnswerInquiry))
                .Message("Hi this is Test Virtual Assistant")
                .Build();
        }

        private static async Task<ValidateResult> AnswerInquiry(FAQConversation state, object value)
        {
            var asString = value as String;
            var vaConfig = new SmartCareSetting(state.Products);
            var result = new ValidateResult() { IsValid = false, Value = value };
            if (!string.IsNullOrEmpty(asString))
            {
                var luisService = new LuisService(new LuisModelAttribute(vaConfig.AppID, vaConfig.SubscriptionKey, domain: vaConfig.HostName));
                var luisResult = await luisService.QueryAsync(asString, CancellationToken.None);
                result.Feedback = luisResult.TopScoringIntent.Intent.ToString();
            }
            return result;
        }
    }

My bot code above shows the conversation below. 我上面的机器人代码显示了下面的对话。

机器人对话

I am creating a simple inquiry bot using FormFlow and Bot Framework. 我正在使用FormFlow和Bot Framework创建一个简单的查询机器人。 I am validating the Inquiry field through LUIS and returning the intent for processing. 我正在通过LUIS验证“ Inquiry字段,并返回处理意图。 I am getting the correct intent, in this case is EXC01 . 我得到正确的意图,在这种情况下是EXC01 Afterwards, Im wondering why am I still getting prompted the Inquiry prompt. 之后,我想知道为什么我仍然得到Inquiry提示。

Questions: 问题:
1. How can I finish the FormFlow after validating the Intent of the Inquiry? 1.确认查询意图后如何完成FormFlow
2. I want to handle the returned Intent but not show it to the user. 2.我想处理返回的Intent,但不显示给用户。 I'll be using the Intent string for querying to a Database. 我将使用Intent字符串查询数据库。 Can I do this inside the BuildForm() ? 我可以在BuildForm()执行此操作吗?

How can I finish the FormFlow after validating the Intent of the Inquiry? 确认查询意图后如何完成FormFlow?

In validation function AnswerInquiry , we can find that IsValid property of ValidateResult would be always false , which cause the issue. 在验证函数AnswerInquiry ,我们可以发现ValidateResult的 IsValid属性始终为false ,这会导致问题。 You can set IsValid property to true after you assign the returned intent as feedback. 在将返回的意图分配为反馈后,可以将IsValid属性设置为true The following code snippet works for me, you can refer to it. 以下代码段对我有用,您可以参考它。

private static async Task<ValidateResult> AnswerInquiry(FAQConversation state, object value)
{
    var asString = value as String;
    var vaConfig = new SmartCareSetting(state.Products);

    var result = new ValidateResult() { IsValid = false, Value = value };

    if (!string.IsNullOrEmpty(asString))
    {
        var luisService = new LuisService(new LuisModelAttribute(vaConfig.AppID, vaConfig.SubscriptionKey, domain: vaConfig.HostName));
        var luisResult = await luisService.QueryAsync(asString, CancellationToken.None);
        result.Feedback = luisResult.TopScoringIntent.Intent.ToString();

        //set IsValid to true
        result.IsValid = true;
    }
    return result;
}

I want to handle the returned Intent but not show it to the user. 我想处理返回的Intent,但不显示给用户。 I'll be using the Intent string for querying to a Database. 我将使用Intent字符串查询数据库。 Can I do this inside the BuildForm()? 我可以在BuildForm()中执行此操作吗?

After you can get the returned intent, if you'd like to query records from your database based on the returned intent, you can do it in your validation function. 获得返回的意图后,如果您想基于返回的意图从数据库中查询记录,则可以在验证函数中进行操作。

Test result: 测试结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM