简体   繁体   English

[BotFramework]:如何在瀑布对话框中捕获/提取通过C#Web聊天bot中呈现的自适应卡提交的值?

[英][BotFramework]: How to capture/extract the values submitted through Adaptive card rendered in C# Web Chat bot in a waterfall dialog?

I have chatbot created in C# using SDK 4 which has multiple dialog's each dialog calls another, in one dialog i am rendering Adaptive card in STEP #1 which is having only 2 inputs be provided with OK button: 1. Date 2. Time 3. OK Button So that i can take the extract/captures the values submitted through OK button in STEP#2 and continue with process. 我已经使用C#使用SDK 4在C#中创建了聊天机器人,该聊天机器人具有多个对话框,每个对话框都调用另一个对话框,在一个对话框中,我正在STEP#1中渲染自适应卡,该卡只有2个输入,并带有“确定”按钮:1.日期2.时间3。 OK按钮,以便我可以提取/捕获通过STEP#2中的OK按钮提交的值,然后继续进行处理。

Issue: How to extract the values that has been submitted in step#1 in STEP#2 in an waterfall dialog in C#? 问题:如何在C#的瀑布对话框中提取在STEP#2的步骤#1中提交的值?

Language: C# 语言:C#

Bot SDK: V4 Bot SDK:V4

Please help as i am new to BOT and coding by providing step by step guide? 请提供逐步指南,以帮助我刚接触BOT和编码?

I have already tried few things like: 1. Putting the card rendered in prompt 2. Try to extract/capture value through: stepContext.Context.Activity.Value 我已经尝试过一些操作,例如:1.将呈现的卡片放在提示中2.尝试通过以下步骤提取/捕获值:stepContext.Context.Activity.Value

All of this did not help. 所有这些都无济于事。

STEP #1: var cardAttachment = CreateAdaptiveCardAttachment(this.cards); 步骤1: var cardAttachment = CreateAdaptiveCardAttachment(this.cards); var reply = stepContext.Context.Activity.CreateReply(); reply.Attachments = new List<Attachment>() { cardAttachment }; return await stepContext.Context.SendActivityAsync(reply); // or return await stepContext.PromptAsync("datetextPrompt", new PromptOptions() { Prompt = reply, }); //或传return await stepContext.PromptAsync("datetextPrompt", new PromptOptions() { Prompt = reply, });

STEP #2: I want to extract or capture value? 步骤#2:我想提取或获取价值? How to do it? 怎么做?

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. 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)
    {
        // Display 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),
        };
        var message = MessageFactory.Text("");
        message.Attachments = new List<Attachment>() { cardAttachment };
        await stepContext.Context.SendActivityAsync(message, cancellationToken);

        // Create the text prompt
        var opts = new PromptOptions
        {
            Prompt = new Activity
            {
                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 ), under OnTurnAsync() , near the beginning of the method, somewhere before await dialogContext.ContinueDialogAsync(cancellationToken) is called (step 3): 在您的主要bot类( <your-bot>.cs )中,在OnTurnAsync() ,在该方法的开头附近,在await dialogContext.ContinueDialogAsync(cancellationToken)之前的某个位置被调用(步骤3):

var activity = turnContext.Activity;

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

Additional Context 附加上下文

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发送的,这意味着提交数据不会作为对话的一部分出现在聊天窗口中,而是保留在自适应卡上。

暂无
暂无

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

相关问题 如何验证自适应卡片机器人框架 v4(瀑布模型)c# 中的输入字段 - How to validate input fields in adaptive card bot framework v4 (waterfall model) c# 是否可以在瀑布对话框中设置带有日期输入的自适应卡片?(在 Web 和 Teams 上使用 Bot Framework) - Is it possible to set an adaptive card with date input in a waterfall dialog?(Using Bot Framework on Web and Teams) MS Chat Bot-如何从我的C#代码访问自定义自适应卡属性 - MS Chat Bot --How to access custom adaptive card properties from my C# code [BotFramework]:在使用C#SDK V4开发的BOT中,是否可以在英雄卡或自适应卡中显示Oauth提示? - [BotFramework]: Is there a way to Display Oauth prompt in hero card or Adaptive card in a BOT Developed using SDK V4 in C#? 如何在 Web 频道聊天机器人中显示自适应卡片中的取消按钮,该聊天机器人在 MS Bot 框架 SDK V4 中的 C# 中开发? - How to have cancel button in Adaptive cards to be displayed in Web Channel chat bot developed in MS Bot framework SDK V4 in C#? Botframework v4:如何简化这个瀑布式对话框? - Botframework v4: How to simplify this waterfall dialog? 如何将数据从网站传递到Botframework网络聊天机器人? - How to pass data from a website to a botframework web chat bot? 瀑布对话框中的C#Bot V4文本提示 - C# bot V4 Text Prompt inside a waterfall dialog 如何在C#[Bot Framework v4]中从QnaBot(qna制造商api)调用瀑布对话框? - How to call waterfall dialog from QnaBot (qna maker api) in C# [Bot Framework v4]? 在 C# 中开发的机器人在微软团队中没有显示自适应卡 - Bot developed in C# not showing Adaptive Card in microsoft teams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM