验证 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.
我正在使用 FormFlow
和 Bot Framework 创建一个简单的查询机器人。
我正在通过 LUIS 验证 Inquiry
字段并返回处理意图。我得到了正确的意图,在这种情况下是 EXC01
。之后,我想知道为什么我仍然收到 Inquiry
提示。
问题:
1. 询价意向确认后如何完成FormFlow
?
2. 我想处理返回的Intent 但不想显示给用户。我将使用 Intent 字符串查询数据库。我可以在 BuildForm()
中执行此操作吗?
How can I finish the FormFlow after validating the Intent of the Inquiry?
在验证函数AnswerInquiry
中,我们可以发现IsValid 属性 of ValidateResult 总是false
,这导致了问题。将返回的意图分配为反馈后,您可以将 IsValid 属性 设置为 true
。以下代码片段适合我,你可以参考它。
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. I'll be using the Intent string for querying to a Database. Can I do this inside the BuildForm()?
获取返回的意图后,如果您想根据返回的意图从数据库中查询记录,可以在验证函数中执行。
测试结果:
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.
我正在使用 FormFlow
和 Bot Framework 创建一个简单的查询机器人。
我正在通过 LUIS 验证 Inquiry
字段并返回处理意图。我得到了正确的意图,在这种情况下是 EXC01
。之后,我想知道为什么我仍然收到 Inquiry
提示。
问题:
1. 询价意向确认后如何完成FormFlow
?
2. 我想处理返回的Intent 但不想显示给用户。我将使用 Intent 字符串查询数据库。我可以在 BuildForm()
中执行此操作吗?
How can I finish the FormFlow after validating the Intent of the Inquiry?
在验证函数AnswerInquiry
中,我们可以发现IsValid 属性 of ValidateResult 总是false
,这导致了问题。将返回的意图分配为反馈后,您可以将 IsValid 属性 设置为 true
。以下代码片段适合我,你可以参考它。
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. I'll be using the Intent string for querying to a Database. Can I do this inside the BuildForm()?
获取返回的意图后,如果您想根据返回的意图从数据库中查询记录,可以在验证函数中执行。
测试结果: