我如何解释/感知直线 api 响应中按钮的存在?
How can I interpret /perceive the presence of buttons in the response of directline api?
假设有一个来自 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 以从 text
属性中获取按钮数据 [(Current Weather) action?weather=Seattle, WA"
]?唯一的办法是模式匹配吗?
有没有人遇到过或知道解决方案,也请在这里补充一下;)
更新:如果它的不同渠道如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"
}
]
}
}
]
如评论中所述,您使用的是 DirectLine v1.1。不幸的是,v1.1 不支持 attachments/cards,因此没有很好的方法来 understand/parse 卡。
您可能要考虑转向 DirectLine v3,它完全支持附件。
或者,如果您想支持卡片,您可能必须相应地执行一些自定义操作,如 DirectLine sample. There, the bot is sending the hero card through the ChannelData field and the client is parsing that 中所示。但是,您可能必须添加逻辑来检测谁在与机器人通话,以便仅当呼叫者是 DirectLine 而不是其他客户端之一(例如 skype)时,您才将卡片作为 ChannelData 发送
假设有一个来自 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 以从 text
属性中获取按钮数据 [(Current Weather) action?weather=Seattle, WA"
]?唯一的办法是模式匹配吗?
有没有人遇到过或知道解决方案,也请在这里补充一下;)
更新:如果它的不同渠道如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"
}
]
}
}
]
如评论中所述,您使用的是 DirectLine v1.1。不幸的是,v1.1 不支持 attachments/cards,因此没有很好的方法来 understand/parse 卡。
您可能要考虑转向 DirectLine v3,它完全支持附件。
或者,如果您想支持卡片,您可能必须相应地执行一些自定义操作,如 DirectLine sample. There, the bot is sending the hero card through the ChannelData field and the client is parsing that 中所示。但是,您可能必须添加逻辑来检测谁在与机器人通话,以便仅当呼叫者是 DirectLine 而不是其他客户端之一(例如 skype)时,您才将卡片作为 ChannelData 发送