机器人框架 v4.如何在瀑布步骤对话框中获取 if else 中的值并将其传递给下一步?

Bot framework v4. How to get the value inside if else in a waterfall step dialog and pass it to the next step?

如何在瀑布步骤对话框中获取if else里面的值并传递给下一步? 请参考下面的代码谢谢。感谢您的帮助。

更新:选择 "Near me" 工作正常,但选择 "Somewhere Else" 时出现错误。

 AddStep(async (stepContext, cancellationToken) =>
        {
            var realEstateType = stepContext.Result as FoundChoice;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.RealEstateType = realEstateType.Value;

            return await stepContext.PromptAsync("choicePrompt",
               new PromptOptions
               {
                   Prompt = stepContext.Context.Activity.CreateReply("Which location are you considering?"),
                   Choices = new[] {new Choice {Value = "Near me"},
                                    new Choice {Value = "Somewhere else"}
                   }.ToList()
               });

        });

        AddStep(async (stepContext, cancellationToken) => 
        {
            var nearOrSomewhereElse = stepContext.Result as FoundChoice;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.NearOrSomewhereElse = nearOrSomewhereElse.Value;

            var value = "";

            if (state.NearOrSomewhereElse == "Somewhere else")
            {
                await stepContext.PromptAsync("textPrompt",
                new PromptOptions
                {
                    Prompt = stepContext.Context.Activity.CreateReply("Which location are you considering?")
                });

                value = stepContext.Result as string; // i think this is the error. How can i get the result of the block of code inside this if block?
            }
            else if (state.NearOrSomewhereElse == "Near me")
            {
                value = "Near me";
            }

            return await stepContext.NextAsync(value, cancellationToken);

        });



        AddStep(async (stepContext, cancellationToken) =>
        {
            var nearOrSomewhereElse = stepContext.Result as string;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.NearOrSomewhereElse = nearOrSomewhereElse;

            return await stepContext.PromptAsync("choicePrompt",
             new PromptOptions
                 {
                   Prompt = stepContext.Context.Activity.CreateReply($"Please indicate the size of {state.RealEstateType}"),
                   Choices = new[] {new Choice {Value = "Size 1"},
                                    new Choice {Value = "Size 2"},
                                    new Choice {Value = "Size 3"}
                   }.ToList()
             });                    

        });
enter code here

最简单的方法是使用 WaterfallStepContext::NextAsync API 将您想要通过的值传递给下一步,然后可以通过 WaterfallStepContext::Result 属性。

看起来像这样:

   // Your first step elided for brevity

   AddStep(async (stepContext, cancellationToken) =>
   {
            var nearOrSomewhereElse = stepContext.Result as FoundChoice;
            var state = await (stepContext.Context.TurnState["FPBotAccessors"] as FPBotAccessors).FPBotStateAccessor.GetAsync(stepContext.Context);
            state.NearOrSomewhereElse = nearOrSomewhereElse.Value;

            if (state.NearOrSomewhereElse == "Near me")
            {
               value = "Near me";
            }
            else if (state.NearOrSomewhereElse == "Somewhere else")
            {
               //prompt user. user's answer will be stored to value.
                value = "User input";
            }

            // Call NextAsync passing on the value
            return await stepContext.NextAsync(value, cancellationToken);
   });

   AddStep(async (stepContext, cancellationToken) =>
   {
         // Retrieve the result of the previous step
         var x = stepContext.Result as string;

         // … use the value here …
   });