Reputation: 281
I have a webhook setup to post a message to one of our Teams Team channels. I'm using one of the teams webhook examples given here: https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-reference#hero-card .
The problem I have is that I am unable to display multiple lines. In the example below, I'd like to separate out 'Test1' and 'Test2' on separate lines. However, using \n or \n in the JSON didn't translate to multi line format in Teams. Screenshot outcome attached below.
"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": ""
}
]
}
}
]
}
What is the way to send a multi-line message to Teams using webhook? Any guidance here is appreciated.
Upvotes: 4
Views: 5774
Reputation: 21
If you're using type as MessageCard,
"@type": "MessageCard"
you should use \n\n
If type is AdaptiveCard, you can use:
<br />
Upvotes: 0
Reputation: 1932
This worked in a bash script.
# 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}"
Upvotes: 1
Reputation: 219
I found that, in c#, appending:
Environment.Newline + Environment.Newline
appears to work.
Upvotes: 0
Reputation: 1
I know this is an old post, but if anyone still needs this you can use AdaptiveCards
$ using 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);
You can use line breaks within a string with "Wrap = true", or you can add multiple cards to a single message. Or both.
Upvotes: 0