2

考虑到 MS 机器人有一个动作卡响应,它在 Skype 中如下所示: 在此处输入图像描述

当这种类似的响应出现在 REST API 中时,即使用 Direct Line API。以下是 JSON 响应的相关部分。

{
  "id": "1t90Ym3PEry|000000000000000014",
  "conversationId": "1t90Ym3PEry",
  "created": "2016-12-06T09:34:55.6280699Z",
  "from": "rich3cards",
  "images": [
    "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/320px-Seattlenighttimequeenanne.jpg"
  ],
  "attachments": [],
  "eTag": "W/\"datetime'2016-12-06T09%3A34%3A54.94083Z'\""
},
{
  "id": "1t90Ym3PEry|000000000000000014",
  "conversationId": "1t90Ym3PEry",
  "created": "2016-12-06T09:34:55.6280699Z",
  "from": "rich3cards",
  "text": "Hero Card\n\nSpace Needle\n\nThe <b>Space Needle</b> is an observation tower in Seattle, Washington, a landmark of the Pacific Northwest, and an icon of Seattle.\n\n(Current Weather) action?weather=Seattle, WA",
  "images": [],
  "attachments": [],
  "eTag": "W/\"datetime'2016-12-06T09%3A34%3A54.94083Z'\""
}

现在,问题是我们如何解析这个 json 以从属性中获取按钮数据 [ (Current Weather) action?weather=Seattle, WA"] ?text唯一的方法是模式匹配吗?

有没有人遇到或知道解决方案,请在这里也放一些光;)


更新:如果它的不同渠道,如 skype/webchat/etc.. JSON 响应看起来非常适合使用,以下是示例 JSON。

{
  "type": "message",
  "id": "5AdoK89rtSc|000000000000000018",
  "timestamp": "2016-12-06T09:53:20.4777291Z",
  "channelId": "webchat",
  "from": {
    "id": "rich3cards",
    "name": "RichCards"
  },
  "conversation": {
    "id": "5AdoK89rtSc"
  },
  "attachments": [
    {
      "contentType": "application/vnd.microsoft.card.hero",
      "content": {
        "title": "Hero Card",
        "subtitle": "Space Needle",
        "text": "The <b>Space Needle</b> is an observation tower in Seattle, Washington, a landmark of the Pacific Northwest, and an icon of Seattle.",
        "images": [
          {
            "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/Seattlenighttimequeenanne.jpg/320px-Seattlenighttimequeenanne.jpg"
          }
        ],
        "buttons": [
          {
            "type": "postBack",
            "value": "action?weather=Seattle, WA",
            "title": "Current Weather"
          }
        ]
      }
    }
  ]
4

1 回答 1

2

如评论中所述,您使用的是 DirectLine v1.1。不幸的是,v1.1 不支持附件/卡片,因此没有很好的方法来理解/解析卡片。

您可能需要考虑迁移到完全支持附件的DirectLine v3 。

在此处输入图像描述

或者,如果您想支持卡片,您可能必须执行一些自定义操作,如DirectLine示例中所示。在那里,机器人通过 ChannelData 字段发送英雄卡,客户端相应地对其进行解析。但是,您可能必须添加逻辑来检测谁在与机器人交谈,以便仅当呼叫者是 DirectLine 而不是其他客户端之一(例如Skype)时才将卡片作为 ChannelData 发送

于 2016-12-06T09:56:20.243 回答