How do I post multiline message using Teams Webhooks?

Viewed 1058

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://docs.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

2 Answers

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://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL#send-adaptive-cards-using-an-incoming-webhook

Related