【问题标题】:How to retrieve input data from an adaptive card?如何从自适应卡中检索输入数据?
【发布时间】:2019-04-08 12:23:15
【问题描述】:

我正在尝试从聊天机器人中的自适应卡片中检索数据,但我不知道该怎么做。

我使用自适应卡片设计器创建了一张自适应卡片,并成功地显示在聊天机器人中,但希望能够在选择“提交”后检索用户输入的数据。

我相信这与我没有消息要到达的端点有关,但我不确定如何声明一个。

感谢任何帮助!

C#

 private async Task choiceCAIAResult(IDialogContext context, IAwaitable<string> result)
    {
        string message = await result;
        if (message == "Yes")
        {

        var replyMessage = context.MakeMessage();
        CreateAdaptiveCardApplicationJson(context, result);

        // replyMessage.Attachments = new List<Attachment> { attachment };
        // await context.PostAsync(replyMessage);


    }
        if (message == "No")
        {

            var replyMessage = context.MakeMessage();
            Attachment attachment = GetCAIAHeroCard();
            replyMessage.Attachments = new List<Attachment> { attachment };
            await context.PostAsync(replyMessage);
        }
    }

       private async Task CreateAdaptiveCardApplicationJson(IDialogContext context, IAwaitable<string> result)
    {

        var returnMessage = context.MakeMessage();

        var json = await GetCardText("adaptiveCard");
        var results = AdaptiveCard.FromJson(json);
        var card = results.Card;
        returnMessage.Attachments.Add(new Attachment()
        {
            Content = card,
            ContentType = AdaptiveCard.ContentType,
            Name = "Card",
        });
        card.Actions.Add(new AdaptiveSubmitAction() {
            Title = "Next",
        });


        Debug.WriteLine(returnMessage.Attachments[0].Content);

        await context.PostAsync(returnMessage);

    }

    public async Task<string> GetCardText(string cardName)
    {
        var path = HostingEnvironment.MapPath($"/Dialogs/{cardName}.json");
        if (!File.Exists(path))
            return string.Empty;

        using (var f = File.OpenText(path))
        {
            return await f.ReadToEndAsync();
        }

    }

JSON

   {   "type": "AdaptiveCard",   "body": [
    {
      "type": "TextBlock",
      "horizontalAlignment": "Center",
      "size": "Large",
      "text": "Residential Aged Care Online Application Form",
      "wrap": true
    },
    {
      "type": "TextBlock",
      "size": "Medium",
      "text": "Applicant Details"
    },
    {
      "type": "Input.Text",
      "id": "firstName",
      "placeholder": "First Name"
    },
    {
      "type": "Input.Text",
      "id": "lastName",
      "placeholder": "Last Name"
    },
    {
      "type": "ColumnSet",
      "columns": [
        {
          "type": "Column",
          "horizontalAlignment": "Left",
          "verticalContentAlignment": "Center",
          "items": [
            {
              "type": "TextBlock",
              "id": "dobTitle",
              "horizontalAlignment": "Right",
              "size": "Medium",
              "text": "Date of Birth"
            }
          ]
        },
        {
          "type": "Column",
          "items": [
            {
              "type": "Input.Date",
              "id": "dob"
            }
          ],
          "width": "stretch"
        }
      ]
    },
    {
      "type": "Input.ChoiceSet",
      "id": "gender",
      "style": "compact",
      "placeholder": "Gender",
      "choices": [
        {
          "title": "Female",
          "value": "female"
        },
        {
          "title": "Male",
          "value": "male"
        },
        {
          "title": "Intersex",
          "value": "intersex"
        },
        {
          "title": "Indeterminate",
          "value": "indeterminate"
        },
        {
          "title": "Transgender - Female",
          "value": "transFemale"
        },
        {
          "title": "Transgender - Male",
          "value": "transMale"
        },
        {
          "title": "Other",
          "value": "other"
        }
      ]
    },
    {
      "type": "Input.Text",
      "id": "street1",
      "title": "Street1",
      "placeholder": "Street 1"
    },
    {
      "type": "Input.Text",
      "id": "city",
      "title": "city",
      "placeholder": "City"
    },
    {
      "type": "Input.ChoiceSet",
      "id": "state",
      "style": "compact",
      "title": "State",
      "placeholder": "State",
      "choices": [
        {
          "title": "SA",
          "value": "SA"
        },
        {
          "title": "ACT",
          "value": "ACT"
        },
        {
          "title": "NSW",
          "value": "NSW"
        },
        {
          "title": "NT",
          "value": "NT"
        },
        {
          "title": "QLD",
          "value": "QLD"
        },
        {
          "title": "TAS",
          "value": "TAS"
        },
        {
          "title": "VIC",
          "value": "VIC"
        },
        {
          "title": "WA",
          "value": "WA"
        },
        {
          "title": "N/A",
          "value": "N/A"
        }
      ]
    },
    {
      "type": "Input.Text",
      "id": "postCode",
      "title": "PostCode",
      "placeholder": "Post Code"
    },
    {
      "type": "Input.Text",
      "id": "phone",
      "title": "Phone",
      "placeholder": "Phone",
      "style": "Tel"
    },
    {
      "type": "Input.Text",
      "id": "email",
      "title": "Email",
      "placeholder": "Email",
      "style": "Email"
    },
    {
      "type": "Input.ChoiceSet",
      "id": "currentAccommodation",
      "style": "compact",
      "title": "CurrentAccommodation",
      "placeholder": "Current Accommodation",
      "choices": [
        {
          "title": "Home",
          "value": "home"
        },
        {
          "title": "Hospital",
          "value": "hospital"
        },
        {
          "title": "Aged Care",
          "value": "agedCare"
        },
        {
          "title": "Other",
          "value": "other"
        }
      ]
    }   ],     "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",   "version": "1.0"

}

【问题讨论】:

  • I do not have an endpoint for the message - 我会使用微软教程来研究如何做到这一点。
  • 我什么都没找到。

标签: c# json chatbot adaptive-cards


【解决方案1】:

正如您所提到的,您需要有一个消息端点。我已经将自适应卡与 RESTful Web 服务一起使用,但是任何可以执行 HTTP Post 请求的 Web 服务都可以。据我所知,从自适应卡片发送数据的唯一方法是使用 HTTP POST 操作(如果我在这里错了,请随时纠正我)。

您需要查看documentation 的“操作”部分以了解如何操作

这是带有简单提交按钮的 JSON 包的操作部分:

   "actions": [
        {
            "type": "Action.Http",
            "id": "Submit",
            "title": "Submit",
            "method": "POST",
            "url": "webserviceURL",
            "body": "{\n  \"requestId\": \"testapprove\",\n  \"callerId\": \"id\",\n  \"requestContent\": \"content\",\n  \"responseContent\": \"content\"\n}",
            "headers": [
                {
                    "name": "content-type",
                    "value": "\"application/json; charset=utf-8\""
                }
            ]
        }
    ],

这里是 RESTful Web 服务的链接:

教程:http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069

关于什么是 REST 的堆栈过低帖子:What are RESTful web services?

您不必在 Web 服务中使用 REST,但它已变得非常普遍。谷歌搜索将引导您找到许多有关创建自己的 Web 服务的教程。

其他测试选项:

使用旨在接收来自任何人的 POST 请求的网站。

这是您可以用于测试的一个:https://httpbin.org/

另一个:https://docs.postman-echo.com/

如果你只需要在本地发送数据,那么谷歌'设置本地http服务器'。那里有很多教程。您选择使用什么可能取决于您的环境和您熟悉的语言。 Here 是 python 的一种,它将回显 get 和 post 请求。 Here 是另一个python,cmets 介绍了如何访问发布的信息。

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2019-04-20
    • 2020-12-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多