简体   繁体   English

我如何获得自适应卡的结果?

[英]How do i get the result of a adaptive card?

I'm trying to get the result of my AdaptiveCard. 我正在尝试获取AdaptiveCard的结果。 My bot uses waterfalldialogs. 我的机器人使用Waterfalldialogs。 In one Waterfallstep i present the user a number of Rooms with the time and date. 在一个Waterfallstep中,我向用户显示了一些带有时间和日期的房间。 The user then can choose a room. 然后,用户可以选择一个房间。 I tried it like shown below. 我尝试如下所示。 Sadly the activity stays null. 遗憾的是,该activity保持为空。 How do I get the result of the adaptive card 如何获得自适应卡的结果

private async Task<DialogTurnResult> AfterChoice(WaterfallStepContext step, CancellationToken cancellationToken)
{
    if (step.Result is Activity activity && activity.Value != null && ((dynamic)activity.Value).chosenRoom is JValue chosenRoom)
    {
        dynamic requestedBooking = JsonConvert.DeserializeObject<ExpandoObject>((string)chosenRoom.Value);
        this.roomemail = requestedBooking.roomEmail;
        return await step.EndDialogAsync();
    }
    else
    {
        return await step.BeginDialogAsync(whatever, cancellationToken: cancellationToken);
    }
}

How do I get the users choice? 如何获得用户选择?

Adaptive Cards send their Submit results a little different than regular user text. 自适应卡发送的“提交”结果与常规用户文本略有不同。 When a user types in the chat and sends a normal message, it ends up in Context.Activity.Text . 当用户键入聊天并发送正常消息时,它最终出现在Context.Activity.Text When a user fills out an input on an Adaptive Card, it ends up in Context.Activity.Value , which is an object where the key names are the id in your card and the values are the field values in the adaptive card. 当用户填写自适应卡上的输入时,它会以Context.Activity.Value结尾,该对象是键名称是您卡中的id ,而值是自适应卡中的字段值的对象。

For example, the json: 例如,json:

{
    "type": "AdaptiveCard",
    "body": [
        {
            "type": "TextBlock",
            "text": "Test Adaptive Card"
        },
        {
            "type": "ColumnSet",
            "columns": [
                {
                    "type": "Column",
                    "items": [
                        {
                            "type": "TextBlock",
                            "text": "Text:"
                        }
                    ],
                    "width": 20
                },
                {
                    "type": "Column",
                    "items": [
                        {
                            "type": "Input.Text",
                            "id": "userText",
                            "placeholder": "Enter Some Text"
                        }
                    ],
                    "width": 80
                }
            ]
        }
    ],
    "actions": [
        {
            "type": "Action.Submit",
            "title": "Submit"
        }
    ],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.0"
}

.. creates a card that looks like: ..创建一张看起来像这样的卡片:

测试自适应卡

If a user enters "Testing Testing 123" in the text box and hits Submit, Context.Activity will look something like: 如果用户在文本框中输入“ Testing Testing 123”,然后单击Submit,则Context.Activity类似于:

{ type: 'message',
  value: { userText: 'Testing Testing 123' },
  from: { id: 'xxxxxxxx-05d4-478a-9daa-9b18c79bb66b', name: 'User' },
  locale: '',
  channelData: { postback: true },
  channelId: 'emulator',
  conversation: { id: 'xxxxxxxx-182b-11e9-be61-091ac0e3a4ac|livechat' },
  id: 'xxxxxxxx-182b-11e9-ad8e-63b45e3ebfa7',
  localTimestamp: 2019-01-14T18:39:21.000Z,
  recipient: { id: '1', name: 'Bot', role: 'bot' },
  timestamp: 2019-01-14T18:39:21.773Z,
  serviceUrl: 'http://localhost:58453' }

The user submission can be seen in Context.Activity.Value.userText . 用户提交可以在Context.Activity.Value.userText看到。

Note that adaptive card submissions are sent as a postBack, which means that the submission data doesn't appear in the chat window as part of the conversation--it stays on the Adaptive Card. 请注意,自适应卡提交是作为postBack发送的,这意味着提交数据不会作为对话的一部分出现在聊天窗口中,而是保留在自适应卡上。

Using Adaptive Cards with Waterfall Dialogs 瀑布对话框中使用自适应卡

Your question doesn't quite relate to this, but since you may end up attempting this, I thought it might be important to include in my answer. 您的问题并不完全与此相关,但是由于您可能最终尝试这样做,因此我认为将其包含在答案中可能很重要。

Natively, Adaptive Cards don't work like prompts. 本地,自适应卡不能像提示一样工作。 With a prompt, the prompt will display and wait for user input before continuing. 带有提示,提示将显示并等待用户输入,然后继续。 But with Adaptive Cards (even if it contains an input box and a submit button), there is no code in an Adaptive Card that will cause a Waterfall Dialog to wait for user input before continuing the dialog. 但是对于自适应卡(即使它包含一个输入框和一个提交按钮),自适应卡中也没有代码,这会使瀑布对话框在继续对话框之前先等待用户输入。

So, if you're using an Adaptive Card that takes user input, you generally want to handle whatever the user submits outside of the context of a Waterfall Dialog. 因此,如果您正在使用需要用户输入的自适应卡,则通常需要处理用户在“瀑布对话框”上下文之外提交的所有内容。

That being said, if you want to use an Adaptive Card as part of a Waterfall Dialog, there is a workaround. 话虽如此,如果您想在瀑布对话框中使用自适应卡,则有一种解决方法。 Basically, you: 基本上,您:

  1. Display the Adaptive Card 显示自适应卡
  2. Display a Text Prompt 显示文字提示
  3. Convert the user's Adaptive Card input into the input of a Text Prompt 将用户的自适应卡输入转换为文本提示的输入

In your Waterfall Dialog class (steps 1 and 2): 在“瀑布对话框”类中(步骤1和2):

private async Task<DialogTurnResult> DisplayCardAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Create the Adaptive Card
    var cardPath = Path.Combine(".", "AdaptiveCard.json");
    var cardJson = File.ReadAllText(cardPath);
    var cardAttachment = new Attachment()
    {
        ContentType = "application/vnd.microsoft.card.adaptive",
        Content = JsonConvert.DeserializeObject(cardJson),
    };

    // Create the text prompt
    var opts = new PromptOptions
    {
        Prompt = new Activity
        {
            Attachments = new List<Attachment>() { cardAttachment },
            Type = ActivityTypes.Message,
            Text = "waiting for user input...", // You can comment this out if you don't want to display any text. Still works.
        }
    };

    // Display a Text Prompt and wait for input
    return await stepContext.PromptAsync(nameof(TextPrompt), opts);
}

private async Task<DialogTurnResult> HandleResponseAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Do something with step.result
    // Adaptive Card submissions are objects, so you likely need to JObject.Parse(step.result)
    await stepContext.Context.SendActivityAsync($"INPUT: {stepContext.Result}");
    return await stepContext.NextAsync();
}

In your main bot class ( <your-bot>.cs ) (step 3): 在您的主要机器人类( <your-bot>.cs )中(步骤3):

var activity = turnContext.Activity;

if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
{
    activity.Text = JsonConvert.SerializeObject(activity.Value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM