C#聊天机器人动态重试提示消息

时间:2019-11-04 19:02:20

标签: c# botframework chatbot

c#聊天机器人:有什么方法可以动态控制选择提示的RetryPrompt消息?我正在使用Bot Framework 4.0。

1 个答案:

答案 0 :(得分:0)

根据我对您问题的解释方式,有两种不同的处理方法。

最简单的方法是仅添加一个单独的RetryPrompt。例如,如果要对Multi-Turn-Prompt sample执行此操作,则只需添加RetryPrompt属性:

private static async Task<DialogTurnResult> TransportStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            // WaterfallStep always finishes with the end of the Waterfall or with another dialog; here it is a Prompt Dialog.
            // Running a prompt here means the next WaterfallStep will be run when the users response is received.
            return await stepContext.PromptAsync(nameof(ChoicePrompt),
                new PromptOptions
                {
                    Prompt = MessageFactory.Text("Please enter your mode of transport."),
                    Choices = ChoiceFactory.ToChoices(new List<string> { "Car", "Bus", "Bicycle" }),
                    RetryPrompt = MessageFactory.Text("That wasn't a valid option. Try again.")
                }, cancellationToken);
        }

这将产生:

enter image description here

另一种选择是做类似@ pkr2000所说的(尽管有所不同),并使用自定义验证器动态添加RetryPrompt。像这样:

AddDialog(new ChoicePrompt(nameof(ChoicePrompt), ValidateChoicesAsync));

[...]

private static Task<bool> ValidateChoicesAsync(PromptValidatorContext<FoundChoice> promptContext, CancellationToken cancellationToken)
{
    if (!promptContext.Recognized.Succeeded)
    {
        promptContext.Options.RetryPrompt = MessageFactory.Text($"You said \"{ promptContext.Context.Activity.Text},\" which is invalid. Please try again.");
        return Task.FromResult(false);
    }
    return Task.FromResult(true);
}

这将产生:

enter image description here

您几乎可以在验证器中做任何您想做的事情。无需使用MessageFactory.Text(),您可以传递完全不同的Activity,例如自适应卡或类似的东西。您也无法设置RetryPrompt,而是将Prompt更改为所需的任何文本/活动,返回false,然后使用新的提示来再次提示用户。您可以使用自定义验证器来进行无限制的操作。