Microsoft Teams Webhook 为自适应卡片生成 400

Microsoft Teams Webhook Generating 400 for Adaptive Card

我有一个可以正常运行的 Teams 频道网络钩子,我可以向该频道成功发送 post 消息。我现在正在尝试 post 将自适应卡连接到 webhook。使用 Postman 并执行 Post 到 https://outlook.office.com/webhook/xyz,在 header 中将 Content-Type 设置为 application/json,并在 header 中设置以下自适应卡body.

{
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "type": "AdaptiveCard",
  "version": "1.0",
  "speak": "Nothing to say.",
  "body": [
    {
      "type": "TextBlock",
      "text": "Hello Teams' user"
    }
  ]
}

有了这个,我收到了 HTTP 400 Bad Request 和 Summary or Text is required 消息。有谁知道 Teams webhooks 是否支持 Adaptive Cards 或者这是否是当前不受支持的任务?

下面的答案现已弃用。请参考 and .

Webhook 尚不支持自适应卡片!我们计划在为机器人发布自适应卡片后不久添加对它们的支持。

对于简单的用例 POST 这个到 webhook url:

{
  "title": "Action News",
  "text": "not **much** happend (markdown)"
}

对于高级用例,请尝试使用 MessageCard:https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using

示例:

{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": "Larry Bryant created a new task",
"sections": [{
    "activityTitle": "![TestImage](https://47a92947.ngrok.io/Content/Images/default.png)Larry Bryant created a new task",
    "activitySubtitle": "On Project Tango",
    "activityImage": "https://teamsnodesample.azurewebsites.net/static/img/image5.png",
    "facts": [{
        "name": "Assigned to",
        "value": "Unassigned"
    }, {
        "name": "Due date",
        "value": "Mon May 01 2017 17:07:18 GMT-0700 (Pacific Daylight Time)"
    }, {
        "name": "Status",
        "value": "Not started"
    }],
    "markdown": true
}],
"potentialAction": [{
    "@type": "ActionCard",
    "name": "Add a comment",
    "inputs": [{
        "@type": "TextInput",
        "id": "comment",
        "isMultiline": false,
        "title": "Add a comment here for this task"
    }],
    "actions": [{
        "@type": "HttpPOST",
        "name": "Add comment",
        "target": "http://..."
    }]
}, {
    "@type": "ActionCard",
    "name": "Set due date",
    "inputs": [{
        "@type": "DateInput",
        "id": "dueDate",
        "title": "Enter a due date for this task"
    }],
    "actions": [{
        "@type": "HttpPOST",
        "name": "Save",
        "target": "http://..."
    }]
}, {
    "@type": "ActionCard",
    "name": "Change status",
    "inputs": [{
        "@type": "MultichoiceInput",
        "id": "list",
        "title": "Select a status",
        "isMultiSelect": "false",
        "choices": [{
            "display": "In Progress",
            "value": "1"
        }, {
            "display": "Active",
            "value": "2"
        }, {
            "display": "Closed",
            "value": "3"
        }]
    }],
    "actions": [{
        "@type": "HttpPOST",
        "name": "Save",
        "target": "http://..."
    }]
}]
}

最近我遇到了同样的问题,正在寻找解决方案。好的部分是 MS Teams 现在支持自适应卡 youtube video to explain how it can be implemented

Github link to track the progress on the issue

我成功地将消息发送到 Teams 频道,没有任何失败。

我正在使用 axios 将自适应卡发送到 Teams 连接器,但我遇到了同样的错误。就我而言,我能够通过将卡片包装为此 link 中显示的消息协议的“附件”来解决问题(此处复制的语法以供参考)。

https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL#send-adaptive-cards-using-an-incoming-webhook

{
   "type":"message",
   "attachments":[
      {
         "contentType":"application/vnd.microsoft.card.adaptive",
         "contentUrl":null,
         "content":{
            "$schema":"http://adaptivecards.io/schemas/adaptive-card.json",
            "type":"AdaptiveCard",
            "version":"1.4",
            "body":[
                {
                "type": "TextBlock",
                "text": "For Samples and Templates, see [https://adaptivecards.io/samples](https://adaptivecards.io/samples)"
                }
            ]
         }
      }
   ]
}

通过发送上述 JSON 作为请求正文(axios 的 data 参数),我成功地让自适应卡显示在我的 Teams 频道中。

可以看到,"content"的值为Adaptive Card结构。自适应卡遵循记录的语法,可在此处找到:

https://docs.microsoft.com/en-us/adaptive-cards/authoring-cards/getting-started

https://docs.microsoft.com/en-us/answers/questions/400502/adaptive-cards-with-incoming-webhooks-in-microsoft.html

但最终,我发现使用这个“设计师”更容易 https://www.adaptivecards.io/designer/ 它提供了一个所见即所得的界面。

我按照此处的说明将请求发送到我在 Teams 中创建的连接器:

https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook#create-incoming-webhook-1

现在它响应 200 OK 并显示在频道中!

你实际上可以将你的自适应卡体发送到这个结构体数组中:

{
"type": "message",
"attachments": [
    {
        "contentType": "application/vnd.microsoft.card.adaptive",
        "contentUrl": null,
        "content": {
            "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
            "type": "AdaptiveCard",
            "version": "1.4",
            "body": [
                
            ]
        }
    }
]

}

参考:Microsoft