Bot框架v4。如何在瀑布步骤对话框中获取其他值并将其传递到下一步?

时间:2019-01-03 09:55:49

标签: c# botframework waterfall

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

更新:选择“靠近我”可以正常工作,但是选择“其他地方”时会出现错误。

 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

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用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 …
   });