Library icon indicating copy to clipboard operation
Library copied to clipboard

Post to Microsoft Teams is using MessageCard (Legacy) format instead of AdaptiveCard

Open kengibous opened this issue 1 year ago • 8 comments

Step template

Microsoft Teams - Post a message - 110a8b1e-4da4-498a-9209-ef8929c31168

Step version

24

Octopus version

2024.2.9274

Step template parameter inputs

image

What happened

The message does indeed post to the teams channel when use the soon to be deprecated Office 365 connectors - https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/. The successor which is to use Microsoft Teams workflows (Power Automate) - it appears that the messages should be in the AdaptiveCard format - https://learn.microsoft.com/en-us/connectors/teams/?tabs=text1#microsoft-teams-webhook.

Reproduction steps

Complete the necessary parameters and the webhook will use the MessageCard (legacy) format.

More Information

I believe some changes to the Powershell script can be made to adopt the AdaptiveCard format. I am happy to submit a PR as well.

kengibous avatar Jul 09 '24 23:07 kengibous

Instead of using the AdaptiveCard format I left it as MessageCard and was able to get the Teams notification to come through using Workflows with the new webhook URL. The trick in the workflow was to...

  1. Change "Send each adaptive card" to "Post message in a chat or channel"
  2. Post as User for private channels or Flow bot for public ones.
  3. Use text, & themeColor from the json body in the message... Example: <p style="border-top:@{triggerBody()?['themeColor']} solid;border-bottom:grey thin solid;border-left:grey thin solid;border-right:grey thin solid;padding-left:4px;padding-right:4px;">@{triggerBody()?['text']}</p>
  4. Use title from the json body in the subject... Example: @{triggerBody()?['title']}

The problem I see with this approach, and one that I'm not sure AdaptiveCard would solve, is that it doesn't handle markdown properly. In the message body example above using [runbook details](...) it will not turn into a hyperlink... I was thinking about handling this by passing in a URL parameter in the json and then have the Workflow convert it to a hyperlink using html. Example <a href="@{triggerBody()?['url']}">runbook details</a>

delphe avatar Jul 18 '24 20:07 delphe

@delphe valid point, I suppose using the AdaptiveCard format is not absolutely necessary depending on how you configure a PowerAutomate workflow

kengibous avatar Jul 19 '24 16:07 kengibous

@delphe actually i believe the AdaptiveCard format will handle markdown - https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-format?tabs=adaptive-md%2Cdesktop%2Cconnector-html#format-cards-with-markdown

kengibous avatar Jul 19 '24 18:07 kengibous

I was able to just update the webhook url to migrate to the logic app url.

Then, using Post As User, using this json:

{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.4",
    "type": "AdaptiveCard",
    "fallbackText": "@{triggerBody()?['title']}",
    "body": [
        {
            "type": "TextBlock",
            "text": "@{triggerBody()?['text']}",
            "wrap": true
        }
    ]
}

And @{triggerBody()?['title']} as the subject, as suggested before.

tbolon avatar Jul 25 '24 10:07 tbolon

Thanks @tbolon ! I went back to using "Post card in a chat or channel" and tried that JSON, which resolved my issue with using markdown. I played around with using @{triggerBody()?['themeColor']} and came up with the following...

{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.4",
    "type": "AdaptiveCard",
    "fallbackText": "@{triggerBody()?['title']}",
    "body": [
        {
            "type": "TextBlock",
            "text": "@{triggerBody()?['text']}",
            "wrap": true,
             "color": "@{triggerBody()?['themeColor']}"
        }
    ]
}

Although, the only acceptable "colors" found in adaptive-cards schema are: "default", "dark", "light", "accent", "good", "warning", "attention"

delphe avatar Jul 26 '24 21:07 delphe

glad I found this today, thank you all for contributing. I don't like that it posts as "firstname lastname via workflows". I see there's a way to add a bot via copilot, but I don't appear to have a license to use copilot software. Any suggestions?

jasonlmorris avatar Aug 15 '24 16:08 jasonlmorris

I will certainly migrate out of using adaptive cards, as it seems they are extremely limited in number of chars, and my release messages are quite long.

Because they only accept HTML, I have alread switched my changelog generator to output HTML instead of markdown. Now I will try to use the post message action.

tbolon avatar Aug 26 '24 14:08 tbolon

#1572 should cover this.

Zunair avatar Nov 20 '24 23:11 Zunair

Closing issue as new step template was merged ( #1572 )

twerthi avatar Jul 03 '25 20:07 twerthi