anoj-cha
anoj-cha

Reputation: 281

How do I post multiline message using Teams Webhooks?

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.

how the result in teams look like at the moment

Upvotes: 4

Views: 5774

Answers (5)

Haseeb Ansari
Haseeb Ansari

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

Steve Wall
Steve Wall

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

Rodney
Rodney

Reputation: 219

I found that, in c#, appending:

Environment.Newline + Environment.Newline

appears to work.

Upvotes: 0

Predrag Jelovac
Predrag Jelovac

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.

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

Upvotes: 0

Saonti-MSFT
Saonti-MSFT

Reputation: 151

Please try using \n\n and check.

Upvotes: 2

Related Questions