Botframework c#:在步骤上下文中重新提示用户输入并将值传递给另一个 class

Botframework c# : reprompt user input within a stepcontext and pass the value into another class

我实际上有两个问题。

首先:我想在 case.intent(在瀑布中)中重新提示用户输入。 我使用 var value = ((string)stepContext.Result); 但它只是存储用户已经提供的输入而不是询问新的输入。

第二:然后我想将 value 传递给另一个 class:cards.cs,然后在自适应卡中呈现。我想在 cards.cs

中使用它而不是 "Mickey Mouse"

如何将变量从一个 class 存储到另一个。我来自 python 背景,我可以在 C# 中使用全局变量吗?

在maindialog.cs中我写了以下内容。

                case FlightBooking.Intent.order:

                    var modifiermessagetext = "What is your order";
                    var value = ((string)stepContext.Result);



                    var modifiermessage = MessageFactory.Text(modifiermessagetext, modifiermessagetext, InputHints.IgnoringInput);
                    var messageText70 = stepContext.Options?.ToString() ?? "you can contact the customer service at 098789876";
                    var promptMessage70 = MessageFactory.Text(messageText70, messageText70, InputHints.ExpectingInput);
                    await stepContext.Context.SendActivityAsync(modifiermessage, cancellationToken);
                    var attachments70 = new List<Attachment>();
                    var reply70 = MessageFactory.Attachment(attachments70);
                    reply70.Attachments.Add(Cards.CardJson());
                    await stepContext.Context.SendActivityAsync(reply70, cancellationToken);
                    return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = promptMessage70 }, cancellationToken);

在Cards.cs中:

        public static Attachment CardJson()
        {
            var templateJson = @"
                                            {
                                                ""type"": ""AdaptiveCard"",
                                                ""version"": ""1.0"",
                                                ""body"": [
                                                    {
                                                        ""type"": ""TextBlock"",
                                                        ""text"": ""Hello {name}""
                                                    }
                                                ]
                                            }";

        var dataJson = @"
                    {
                        ""name"": ""Mickey Mouse""
                    }";

        var transformer = new AdaptiveTransformer();
        var cardJson = transformer.Transform(templateJson, dataJson);
        var adaptiveCardAttachment = new Attachment()
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = JsonConvert.DeserializeObject(cardJson),
        };
            return adaptiveCardAttachment;
        }

我正在使用自适应卡片的新功能:https://docs.microsoft.com/en-us/adaptive-cards/templating/sdk

有没有简单的方法可以做到这一点?欢迎任何建议。谢谢!

首先让我解释一下 waterfall 对话框是如何工作的

根据Microsoft Docs

A waterfall dialog is a specific implementation of a dialog that is commonly used to collect information from the user or guide the user through a series of tasks. Each step of the conversation is implemented as an asynchronous function that takes a waterfall step context (step) parameter. At each step, the bot prompts the user for input (or can begin a child dialog, but that it is often a prompt), waits for a response, and then passes the result to the next step. The result of the first function is passed as an argument into the next function, and so on.

下图显示了一系列瀑布步骤和发生的堆栈操作。

这意味着当您在瀑布式步骤提示一个对话框时,您可以在NEXT中得到该结果瀑布的步骤 与您尝试的步骤不在同一步骤内。

  • stepContext.Result:从Previous瀑布步骤
  • 获取结果
  • stepContext.Options:包含对话框的输入信息。它获取调用瀑布对话框时使用的任何选项。
    例如假设你做了 await context.BeginDialogAsync("yourWaterfallDialog", false);
    false” 是 Options 可以是像这样从你的瀑布步骤中获取
    if (sc.Options != null && sc.Options is bool) { valid = (bool)sc.Options; }
  • stepContext.Values:包含您可以添加到上下文的信息,并被带入后续步骤。它是一个值字典,将 保留在所有瀑布步骤中 示例:stepContext.Values["name"] = "Marc"

看看这个例子:

    private async Task<DialogTurnResult> StepOne(WaterfallStepContext stepContext, CancellationToken cancellationToken)
         {
            return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions Prompt = promptMessage70 }, cancellationToken);
         }



    private async Task<DialogTurnResult> StepTwo(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
          // Get the user response from StepOne: 
          var value = ((string)stepContext.Result);
          // we pass the value to CardJson
          reply70.Attachments.Add(Cards.CardJson(value));
          await stepContext.Context.SendActivityAsync(reply70,cancellationToken);
        }

如您所见,在 StepTwo 中,我们从 StepOne[=58] 中获取了用户输入的 result =].
要在自适应卡中获取 Value 而不是 "Mickey Mouse" ,您只需将其作为 字符串参数 传递,就像这样 Cards.CardJson()

public static Attachment CardJson(string value){
...
            var dataJson = @"{'name': '" + value + "'}";
...
}