我在机器人框架技术上工作,在我当前的一个项目中,我想只允许用户输入“IVR”或“IVR”,否则它会向用户显示一些反馈。
为此,我编写了以下几行代码,但这段代码向用户显示了一些错误的输出。即使用户输入ivr或IVR,它也会第一次向用户显示反馈,但从第二次开始,它会正常工作。
[Serializable]
class Customer
{
//Create Account Template
[Prompt("Please send any of these commands like **IVR** (or) **ivr**.")]
public string StartingWord;
public static IForm<Customer> BuildForm()
{
OnCompletionAsyncDelegate<Customer> accountStatus = async (context, state) =>
{
await Task.Delay(TimeSpan.FromSeconds(5));
await context.PostAsync("We are currently processing your account details. We will message you the status.");
};
var builder = new FormBuilder<Customer>();
return builder
//.Message("Welcome to the BankIVR bot! To start an conversation with this bot send **ivr** or **IVR** command.\r \n if you need help, send the **Help** command")
.Field(nameof(Customer.StartingWord), validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true, Value = response };
string str = (response as string);
if (str.ToLower() != "ivr")
{
result.Feedback = "I'm sorry. I didn't understand you.";
result.IsValid = false;
return result;
}
else if (str.ToLower() == "ivr")
{
result.IsValid = true;
return result;
}
else
{
return result;
}
})
.OnCompletion(accountStatus)
.Build();
}
};请告诉我如何使用表单流概念来解决此问题。
-Pradeep
发布于 2016-07-19 13:34:22
您的代码在我看来是正确的--我只能建议您使用单步调试程序来调试您的代码,并查看逻辑测试失败的地方。
也就是说,如果它不适用于土耳其人,那是因为您不应该使用.ToLower()来规范化文本,例如,.ToLower()方法不适用于包含土耳其语不带圆点的'I'字符的文本:http://archives.miloush.net/michkap/archive/2004/12/02/273619.html
此外,您的else用例永远不会被击中,因为您之前的两个检查(!=和==)涵盖了所有可能的情况( C#编译器目前还不够复杂,无法将else用例标记为无法访问的代码)。
进行不区分大小写的比较的正确方法是使用String.Equals
if( "ivr".Equals( str, StringComparison.InvariantCultureIgnoreCase ) ) {
result.IsValid = true;
return result;
}
else {
result.Feedback = "I'm sorry. I didn't understand you.";
result.IsValid = false;
}发布于 2016-11-18 17:38:21
最后,我得到了没有任何问题的结果。
这是我的更新代码,只允许用户输入"ivr或IVR“单词,开始与机器人的表单流程对话。
.Field(nameof(Customer.StartingWord), validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true, Value = response };
string str = (response as string);
if ("ivr".Equals(str, StringComparison.InvariantCultureIgnoreCase))
{
//result.IsValid = true;
//return result;
}
else
{
result.Feedback = "I'm sorry. I didn't understand you.";
result.IsValid = false;
//return result;
}
return result;
})-Pradeep
https://stackoverflow.com/questions/38450121
复制相似问题