c# - 发布到 Teams webhook 时 AdaptiveCard 400 错误请求 C#
问题描述
使用以下代码,我得到StatusCode: 400, ReasonPhrase: 'Bad Request'
但没有更多信息:
response = {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
Cache-Control: no-cache
Pragma: no-cache
Server: Microsoft-IIS/10.0
request-id: 5a296706-4079-47ed-aa57-e50eb3dd78fb...
}
我已经在 Teams 中设置了 webhook,感谢指向正确方向的指针。
var card = new AdaptiveCard(new AdaptiveSchemaVersion(1,0));
var columnSet = new AdaptiveColumnSet();
var column = new AdaptiveColumn();
var text = new AdaptiveTextBlock("one");
column.Items.Add(text);
columnSet.Columns.Add(column);
card.Body.Add(columnSet);
try
{
var httpContent = new StringContent(card.ToJson(), Encoding.UTF8, AdaptiveCard.ContentType);
var response = await httpClient
.PostAsync(settings.TeamsWebhook,httpContent)
.ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.OK)
{
// Do something with response. Example get content:
// var responseContent = await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
}
}
生成的 JSON 如下:
{
"type": "AdaptiveCard",
"version": "1.0",
"title": "test",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"text": "one"
}
]
}
]
}
]
}
我还尝试通过添加容器类型(因为我找不到下面的附件元素)并得到相同的错误。
var card = new AdaptiveCard(new AdaptiveSchemaVersion(1,2));
var columnSet = new AdaptiveColumnSet();
var column = new AdaptiveColumn();
var text = new AdaptiveTextBlock("one");
var container = new AdaptiveContainer();
column.Items.Add(text);
columnSet.Columns.Add(column);
container.Items.Add(columnSet);
card.Body.Add(container);
给出以下 json:
{
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "Container",
"items": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"text": "one"
}
]
}
]
}
]
}
]
}
解决方案
我自己没有尝试过,但从我在文档中看到的内容来看,自适应卡需要包装为容器内的“附件”元素。请参阅通过传入 webhook 发送自适应卡片的流程如下,步骤 2
推荐阅读
- javascript - 组件中的 Vue.js 动态值
- python - 如何有效地从 h5py 数据集中随机选择数据子集
- html - 如何在网络服务器上托管 mhtml 文件?
- python-3.x - ImportError:没有名为 tkinter 的模块。Mac OS 终端
- spring - 在 Spring Rest API 中配置 LocaldateTime
- c# - 透明重叠圆形进度条(自定义控件)
- javascript -
- php - 从数据库中获取用户注册日期并发送到 android
- javascript - 将标签加载为休眠状态
- android - 如何解决:“始终显示屏幕”关闭,导致 android wear 上的位置更新不规则