如何使用 Teams Webhooks 发布多行消息?

问题描述 投票:0回答:5

我有一个 webhook 设置,可以将消息发布到我们的 Teams 团队频道之一。我正在使用此处给出的团队 Webhook 示例之一:https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-reference#hero-card .

我遇到的问题是无法显示多行。在下面的示例中,我想将“Test1”和“Test2”分开放在不同的行上。然而,使用 或者 JSON 中的内容未转换为 Teams 中的多行格式。截图结果附在下面。

    "type": "message",
    "attachments": [
        {
            "contentType": "application/vnd.microsoft.card.hero",
            "content": {
                "title": "Alerts",
                "text": "*Test1 \n *Test\n",
                "buttons": [
                    {
                        "type": "openUrl",
                        "title": "Open in SumoLogic",
                        "value": ""
                    }
                ]
            }
        }
    ]
}

如何使用 webhook 向 Teams 发送多行消息?感谢这里的任何指导。

json microsoft-teams
5个回答
2
投票

请尝试使用

\n\n
并检查。


0
投票

我知道这是一篇旧文章,但如果有人仍然需要这个,你可以使用 AdaptiveCards

$ 使用 AdaptiveCards;

            AdaptiveCard card = new AdaptiveCard("1.5");

            card.AdditionalProperties.Add("$schema", "http://adaptivecards.io/schemas/adaptive-card.json");
            var msTeamsWidthOption = new { width = "Full" };
            card.AdditionalProperties.Add("msteams", msTeamsWidthOption);

            AdaptiveTextBlock titleBlock = new AdaptiveTextBlock
            {
                Size = AdaptiveTextSize.Large,
                Weight = AdaptiveTextWeight.Bolder,
                Wrap = true,
                Text = title
            };

            AdaptiveTextBlock messageBlock = new AdaptiveTextBlock
            {
                Wrap = true,
                Text = message
            };

            card.Body.Add(titleBlock);
            card.Body.Add(messageBlock);

            AdaptiveCardWrapper adaptiveCardWrapper = new AdaptiveCardWrapper 
            { 
                attachments = new List<Attachments> { new Attachments { content = card} }
            };

            var address = Environment.GetEnvironmentVariable("WebhookUrl");
            var content = new StringContent(JsonConvert.SerializeObject(adaptiveCardWrapper), Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync(address, content);

您可以在带有“Wrap = true”的字符串中使用换行符,也可以将多张卡片添加到一条消息中。或者两者兼而有之。

https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL#send-adaptive-cards-using-an-incoming-网络钩子


0
投票

我发现,在c#中,附加:

Environment.Newline + Environment.Newline

似乎可以工作。


0
投票

这在 bash 脚本中有效。

# Replace newline with <br>
MULTILINE_STRING_BR=$(echo "$MULTILINE_STRING" | sed 's/$/<br>/g')
cat <<EOF >/tmp/msg.txt
{
    "text": "Some introductory text:<br>
${MULTILINE_STRING_BR}"
}
EOF

# Send to teams channel
curl --request POST \
    --silent \
    --header "Content-Type: application/json" \
    --data @/tmp/msg.txt \
    "${TEAMS_WEBHOOK}"

0
投票

如果您使用类型为 MessageCard,

"@type": "MessageCard"

你应该使用

\n\n
如果类型是 AdaptiveCard,您可以使用:

<br />
© www.soinside.com 2019 - 2024. All rights reserved.