【问题标题】:Go to next Waterfall step from current step in bot framework v4从机器人框架 v4 中的当前步骤转到下一个瀑布步骤
【发布时间】:2019-03-16 07:58:03
【问题描述】:

我有一个瀑布对话框,提示用户选择“是”或“否”。用户选择“是”后,机器人不会继续下一个瀑布对话框。

第一个瀑布步骤。

private static async Task<DialogTurnResult> LoginStepAsync(WaterfallStepContext step, CancellationToken cancellationToken)
    {
        var tokenResponse = (TokenResponse)step.Result;
        if (tokenResponse != null)
        {
            await step.Context.SendActivityAsync("You are now logged in.", cancellationToken: cancellationToken);
            return await step.PromptAsync(
                ConfirmPromptName,
                new PromptOptions
                {
                    Prompt = MessageFactory.Text($"Would you like to view your token? {tokenResponse.Token}"),
                    Choices = new List<Choice> { new Choice("Yes"), new Choice("No") },
                },
                cancellationToken);
        }

        await step.Context.SendActivityAsync("Login was not successful please try again.", cancellationToken: cancellationToken);
        return Dialog.EndOfTurn;
    }

第二个瀑布步骤。

    private static async Task<DialogTurnResult> DisplayTokenAsync(WaterfallStepContext step, CancellationToken cancellationToken)
    {
        var result = (bool)step.Result;
        if (result)
        {
            var prompt = await step.BeginDialogAsync(LoginPromptName, cancellationToken: cancellationToken);
            var tokenResponse = (TokenResponse)prompt.Result;
            if (tokenResponse != null)
            {
                await step.Context.SendActivityAsync($"Here is your token {tokenResponse.Token}", cancellationToken: cancellationToken);
            }
        }

        return Dialog.EndOfTurn;
    }

我在这里缺少什么?先感谢您。我关注了this 指南和this 项目。

编辑:我在机器人的构造函数中设置了对话框,将提示和瀑布对话框添加到集合中。

    var waterfallSteps = new WaterfallStep[]
    {
        PromptStepAsync,
        LoginStepAsync,
        DisplayTokenAsync,
    };

    _dialogs.Add(new WaterfallDialog("authDialog", waterfallSteps));

【问题讨论】:

    标签: c# authentication botframework


    【解决方案1】:

    你好尼山查图兰加!!

    我在创建确认机器人时遇到了这个问题!一个小小的改变会让你走上正轨。选择提示返回一个字符串,而不是布尔值,所以你会混淆它。但是,如果您将其更改为确认提示,您将消除此问题。

    在我的对话框集中,我添加了多种提示类型:

    // Add the prompts.
                Add(new ChoicePrompt(Inputs.Choice));
                Add(new NumberPrompt<int>(Inputs.Number));
                Add(new ConfirmPrompt(Inputs.Confirm));
    

    还有我的输入列表:

    private static class Inputs //aka types of prompts
        {
            public const string Choice = "choicePrompt";
            public const string Number = "numberPrompt";
            public const string Confirm = "confirmPrompt";
        }
    

    现在,当我调用这些提示时,如果我调用 Inputs.Choice 来回答需要是(真)或否(假)答案的东西,它就坏了:

    public static async Task<DialogTurnResult> ConfirmPhoneAsync(
                WaterfallStepContext stepContext,
                CancellationToken cancellationToken)
            {
                var phoneNumber = stepContext.Context.Activity.Text;
                stepContext.Values[Outputs.PhoneNumber] = phoneNumber;
    
                return await stepContext.PromptAsync(
                    Inputs.Choice,
                    new PromptOptions
                    {
                        Prompt = MessageFactory.Text($"Is {phoneNumber} your phone number?"),
                        Choices = new List<Choice> { new Choice("yep"), new Choice("nah.") },
                    },
                    cancellationToken);
            }
    

    但是!!如果我将该行更改为 Input.Confirm(限制为“是”或“否”的提示类型,而不是选项列表),它会按预期工作:

    return await stepContext.PromptAsync(
                    Inputs.Confirm,
                    new PromptOptions
    

    如果您需要任何其他帮助,请告诉我!

    【讨论】:

    • 这里的ConfirmPromptName是一个ConfirmPrompt(),是这样定义的。 ` _dialogs.Add(new ConfirmPrompt(ConfirmPromptName));`
    • 在这种情况下,我认为您的确认提示有误。 ConfirmPrompt 不接受选择,只接受对话 ID、验证器和语言环境(用于语言选择)。尝试注释掉“选择”部分并重新运行它。
    【解决方案2】:

    我不知道您是否已经找到了解决方案,但我遇到了同样的问题并找到了解决方案。 问题在于没有在 OnTurnAsync 方法结束时使用访问器的 save changes 方法将 DialogState 保存在 ConversationState 中。这是必要的,下次您的 dialgogContext 将包含一个“ActiveDialog”,即您的“authDialog”。

    // 将更新的对话状态保存到对话状态。 await _accessors.ConversationState.SaveChangesAsync(turnContext, false, cancelToken);

    您可能还需要修改 BotAccessors 类以获取对 ConversationState 的引用。

    请参阅下面的链接以供参考。

    https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-prompts?view=azure-bot-service-4.0&tabs=csharp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多