首页 > 解决方案 > Botframework V4:用户键入响应而不是单击选择提示按钮

问题描述

我有一个选择提示,我想这样做,即使用户键入与选择同义的其他内容,对话框仍然可以继续。我试过这样做,但它不起作用。

public class InitialQuestions : WaterfallDialog
{
    public InitialQuestions(string dialogId, IEnumerable<WaterfallStep> steps = null)
        : base(dialogId, steps)
    { 

        AddStep(async (stepContext, cancellationToken) =>
        {
            var choices = new[] { "Agree" };
            return await stepContext.PromptAsync(
                "choicePrompt",
                new PromptOptions
                {
                    Prompt = MessageFactory.Text(string.Empty),
                    Choices = ChoiceFactory.ToChoices(choices),
                    RetryPrompt = MessageFactory.Text("Click Agree to proceed."),
                });
        });

        AddStep(async (stepContext, cancellationToken) =>
        {
            var response = (stepContext.Result as FoundChoice).Value.ToLower();
            var textResponse = (stepContext.Result as FoundChoice).ToString().ToLower();

            if (response == "agree" || textResponse == "okay" || textResponse == "ok")
            {
                return await stepContext.NextAsync();
            }
            else
            {
                return await stepContext.ReplaceDialogAsync(InitialQuestions.Id);
            }
        });
    }

    public static string Id => "initialQuestions";

    public static InitialQuestions Instance { get; } = new InitialQuestions(Id);
}

标签: c#botframework

解决方案


选择提示必须通过将用户输入与选择列表进行比较来验证用户输入,并且在提供有效输入之前对话框不会继续。您正在尝试在下一步中验证输入,但在输入已经验证之前不会到达下一步,这就是为什么textResponse永远不会“好的”或“好的”。

幸运的是,选择提示具有为每个选择提供同义词的内置方式。代替

Choices = ChoiceFactory.ToChoices(choices),

你可以做类似的事情

Choices = new List<Choice>
{
    new Choice
    {
        Value = "Agree",
        Synonyms = new List<string>
        {
            "Okay",
            "OK",
        },
    },
},

推荐阅读