Skip to content

Instantly share code, notes, and snippets.

@dtcMLOps
Last active July 17, 2024 19:15
Show Gist options
  • Save dtcMLOps/81e11acbdbe39db263f18a14b484b8ae to your computer and use it in GitHub Desktop.
Save dtcMLOps/81e11acbdbe39db263f18a14b484b8ae to your computer and use it in GitHub Desktop.
Update 0365 Teams webhook in python to use Teams workflows (power automate flows)

Process

Follow the next steps to update O365 teams webhook connector to user workflows (power automate - flows).

This flow in power automate is going to be triggered by the webhook and post the message in the channel. The "attachments" field in the webhook is an array, so we need to parse the JSON data to get the content of the message. This will get the first attachment content.

  1. Create instant cloud flow

  2. Provide a name

  3. Select trigger: When a Teams webhook request is received and press create.

  4. Press the + New step button and select Add an action.

  5. Search for Post card in a chat or channel action.

  6. Fill in the required fields:

    • Team: Select the team where the webhook is connected.
    • Channel: Select the channel where the webhook is connected.
    • Message: Here is the important part. You must add a function to parse the JSON data from the webhook. To do this, click on the Message field and select Expression.
  7. In the expression editor, add the following expression:

    triggerOutputs()?['body']?['attachments']?[0]?['content']
    

Note

Follow the image below to see how the flow should look like.

Test flow

To test the flow, you can use the Postman tool to send a POST request to the webhook URL genereated in the When a Teams webhook request is received block (click on the block to see the URL). The body of the request should be a JSON object with the following structure:

{
    "type": "message",
    "attachments": [
        {
            "contentType": "application/vnd.microsoft.card.adaptive",
            "contentUrl": null,
            "content": {
                "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
                "type": "AdaptiveCard",
                "version": "1.2",
                "body": [
                    {
                        "type": "TextBlock",
                        "text": "Hello World message from Teams flow 🚨",
                        "weight": "Bolder",
                        "size": "Medium"
                    },
                ]
            }
        }
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment