0

我正在研究 bot 框架技术,在我当前的一个项目中,我只想在用户键入“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();
    }
};

请告诉我如何使用表单流概念解决此问题。

-普拉迪普

4

2 回答 2

0

最后,我得到了没有任何问题的结果。

这是我更新的代码,仅允许用户输入“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;
                   })

-普拉迪普

于 2016-11-18T09:38:20.900 回答
0

您的代码在我看来是正确的 - 我只能建议您使用逐步调试器调试您的代码,并查看逻辑测试在哪里失败。

也就是说,如果它不适用于土耳其人,那是因为您不应该使用.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-07-19T05:34:21.507 回答