如何在随后的瀑布步骤中检索自适应卡的表单提交 [英] How to retrieve Adaptive Card's form submission in subsequent waterfall step

查看:60
本文介绍了如何在随后的瀑布步骤中检索自适应卡的表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bot Framework(V4),并且有一个分为两个步骤的WaterfallDialog;第一步是捕获表单数据,第二步是处理表单数据.

I'm using the Bot Framework (V4) and I've got a WaterfallDialog with two steps; first step is to capture form data and the second step is to process form data.

第一步发送答复:

private async Task<DialogTurnResult> CaptureFormStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    var reply = await stepContext.Context.Activity.GetReplyFromCardAsync("BusinessyForm");
    await stepContext.Context.SendActivityAsync(reply, cancellationToken);
}

GetReplyFromCardAsync是一种扩展方法,该方法采用自适应卡作为附件的Activity的JSON表示形式.自适应卡的格式类似于.自适应卡还具有提交动作.

The GetReplyFromCardAsync is an extension method which takes a JSON representation of an Activity with an adaptive card as an attachnent. The adaptive card is a form similiar to this. The adaptive card also has a submit action.

我的问题是如何将这些信息传递到下一步?

My question is how should this information be passed to the next step?

在先前的尝试中,我返回了提示:

In previous attempts, I've returned a Prompt:

return await stepContext.PromptAsync(
    "custom-prompt-id", 
    new PromptOptions { Prompt = MessageFactory.Text("Hello!") }, 
    cancellationToken);

但是在这种情况下,我不想提示用户其他信息.理想情况下,我想保持"对话框状态以进行响应,当检测到响应时,下一步将处理提交结果.

But in this context I do not want to prompt the user for additional info. Ideally, I'd like to "hold" the dialog state for a response and when one is detected the proceeding next step will process the submission result.

我尝试过的事情:

  • 已查看示例此处这里.两者都显示了如何呈现卡片,但没有显示如何在后续步骤中处理输入.
  • 此处.
  • 已审查GitHub的AdaptiveCard存储库中类似问题的讨论,此处.
  • Reviewed examples here and here. Both shows how cards can be rendered but not how to handle input in subsequent steps.
  • Reviewed card schema documentation for inputs here.
  • Reviewed discussion of similar issue in GitHub's AdaptiveCard repo here.

这可行吗?任何帮助将不胜感激!

Is this feasible? Any help would be much appreciated!

推荐答案

自适应卡的提交结果与常规用户文本略有不同.当用户键入聊天并发送普通消息时,它最终以Context.Activity.Text结尾.当用户填写自适应卡上的输入时,它会以Context.Activity.Value结尾,该对象的键名是卡中的id,值是自适应卡中的字段值.

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. 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.

例如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:

如果用户在文本框中输入"Testing Testing 123",然后单击Submit,则Context.Activity将类似于:

If a user enters "Testing Testing 123" in the text box and hits Submit, Context.Activity will look something like:

{ 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' }

可以在Context.Activity.Value.userText中看到用户提交的内容.

The user submission can be seen in Context.Activity.Value.userText.

请注意,自适应卡的提交是作为postBack发送的,这意味着提交数据不会作为对话的一部分出现在聊天窗口中,而是保留在自适应卡上.

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.

使用自适应卡和

Using Adaptive Cards with Waterfall Dialogs

从本质上讲,自适应卡不能像提示一样工作.带有提示,提示将显示并等待用户输入,然后继续.但是对于自适应卡(即使它包含一个输入框和一个提交按钮),自适应卡中也没有代码,这会使瀑布对话框在继续对话框之前先等待用户输入.

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. 显示自适应卡
  2. 显示文字提示
  3. 将用户的自适应卡输入转换为文本提示输入

在瀑布对话框"类中(步骤1和2):

In your Waterfall Dialog class (steps 1 and 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();
}

在您的主要bot类(<your-bot>.cs)中的OnTurnAsync()下,靠近方法开头的位置,称为await dialogContext.ContinueDialogAsync(cancellationToken)之前的某个位置(第3步):

In your main bot class (<your-bot>.cs), under OnTurnAsync(), near the beginning of the method, somewhere before await dialogContext.ContinueDialogAsync(cancellationToken) is called (step 3):

var activity = turnContext.Activity;

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

这篇关于如何在随后的瀑布步骤中检索自适应卡的表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆