Posting automated messages to Microsoft Teams

How to use Microsoft Power Automate to automaticity post a Microsoft Teams message to a channel.

April 05, 2021

Microsoft Power Automate

Microsoft Teams

Microsoft Teams is something I've become very familiar with over the past year or so due to a still-ongoing global pandemic, and it looks to be the collaboration tool which I think will continue to be used for many years.

Mostly because it's bundled with business Office 365 plans and anyone who has things like Outlook, or any of the Microsoft Office tools on their work machine, will also have Microsoft Teams as well - you may as well use that instead of paying extra for something else... Just my opinion...

And Teams is honestly great. I use it a lot for work - and sometimes, for some people, some tasks are a bit repetitive. You might need to send the same message week in week out, or you might want to receive a message whenever someone sends you a response on a Microsoft Form, or even post a tweet in a channel. You can use Microsoft Power Automate to do that for you!

Microsoft Power Automate uses Flows to automate many tasks - the flows are triggered by uh.... Triggers... and perform actions with... Actions 🙃.

These are part of connectors, products or services which can be used within Power Automate. They include a lot of Microsoft apps and services - as well as other tools including Salesforce, WordPress and Jira.

What are we making?

An automated message being posted to a teams channel every workday morning with a random fact.

For the random fact - I'm going to be using an API. The API is Random Useless Facts - their /today endpoint is only updated once a day, and we can use /today.json to retrieve a JSON response which we can parse.

Creating our Flow

First - go to http://flow.microsoft.com/ and log in - make sure you're logged into the same Microsoft Account you would usually use for Microsoft Teams, this is because Power Automate authenticates you this way for accessing your Team data.

Next - select "My flows" in the sidebar, and then click "New Flow". This will show a dropdown, in our case, we'd need to select "Scheduled Cloud Flow".

Screenshot showing the New Flow dropdown

In the modal popup - give your flow a name, I've gone with "Post Random Fact of the Day". Set the start date and time when it would first be executed, and then set the repeat value to be on a weekly basis, but set it to only run on all days but Saturday and Sunday.

Screenshot of the flow settings

Making the API call

To make an API call, we will need to use the HTTP action - To do this, select "+ New Step", and then search for HTTP and select "HTTP" in the actions list.

Screenshot of the actions list with the HTTP value in the search

We'll need to define a few things in order to contact our API, we need to set the Method to "GET" and the URI to our Random Useless Facts endpoint.

Screenshot of the HTTP action settings

From here - we can actually test our Flow to see if it works. Click "Save" in the top right, and then click "Test". In the sidebar that appears - select "Manually" (Because we're going to trigger it). Then select "Test", and then finally "Run flow".

Click "Done" and you should see a message about our Flow being successful! And you can also see the body of our API response too!

Screenshot of the API response body

Getting/Parsing the JSON response

In order for us to use the data from the API properly, we'd need to parse the data we've just received, so we need to use the "Parse JSON" action. Add it just like we did with the "HTTP" action, and then for the content value - select "Body" in the "Dynamic content" popup that has appeared. This is how Flow can communicate between triggers and actions.

Screenshot of the Parse JSON action and the Dynamic content popup

For the schema - the best way I've found is to copy the entire response from the API, and then select "Generate from sample" to create the schema - do that, and you should see it all entered correctly.

Posting a message to Microsoft Teams

Finally - we can use the parsed data from our API - and use it to be a part of our Teams message.

Add a third and final action, named "Post message in chat or channel". In my case, I'm happy for it to appear as a flow bot, and not a user, and I need it to post in a channel. From there - two additional options will appear, in our case - Team and channel. Select your Team first - Power Automate will use your Microsoft Account to list what teams you're part of, after that you can select which channel to post in.

For the message, you'd enter it as a formatted Teams message, and just like how we defined the body on the JSON Parse action - we're going to select "text" from the dynamic content list. This is why the schema is required, it's so that Flow can determine what properties and types to use.

Screenshot of the Teams message settings

And that's pretty much it! Test it again, and you should get a Teams notification from the Flow bot!

What else can you do with Power Automate?

I think Power Automate really comes in handy if you need to do repetitive tasks over and over. While it may take longer to make than doing something manually - in the future your saving yourself a lot of time. It might take a minute or so to go to the website, get the fact, write the message out, and then send it. Whereas it would take a second or two for our flow.

The actual combinations are pretty much endless. While they don't have every single app on the planet as a connector - if they have an API you can automate it. Think IFTTT - but more control over what you can use and how to do it.

Final Thoughts/Wrapping Up

Power Automate is a tool which I've gotten to use a little bit over the past year, and it has really helped me in terms of making sure I don't forget to post certain messages - or making it easier for me to manage things.

It's something which while isn't as basic as IFTTT, it does allow you more control - and can be as basic or as advanced as you like from it. The example I showed today could have been just the final part of posting the Teams message - but I decided to be a bit more fun and include making an API request to show that you can do that too.

Power automate can do lots of things that involves Teams or not. They have a ton of templates which you can use yourself, or customise. Some ideas off the top of my head include emailing a user when a file has been uploaded to OneDrive, or post a teams message when it's someone's birthday.

But Power Automate can really help you out in taking the weight of doing boring and repetitive tasks off your hands - and pass them over to a robot 🤖...