Monday, 3 May 2021

Message Centre to Teams using Power Automate

 This post is made to accompany the YouTube tutorial video here: 

Watch the video to see the configuration in real time! All the steps have been documented in black and white here for your reference.

Note: You will need to have Power Automate licencing in order to run this flow. For more info on Power Apps (including Power Automate) trial licences go here: https://docs.microsoft.com/en-us/powerapps/maker/signup-for-powerapps


Configure API Access:

Before creating the Power Automate flow you first need to create an application registration within Azure AD to allow for authentication and access to the Microsoft Message Centre API.

During this process there are specific values that you will need to take note of in order to provide values for the Power Application connection. These are the TenantID, ClientID,  SecretID which will be generated when you create an App Registration within Azure AD.


Step 1: Setup API Access for the Power Automate Flow

a) Log into the Office 365 Admin Center as a Global Administrator, click Admin Centers in the left-hand menu, and click Azure Active Directory. Alternatively, you could log into http://portal.azure.com with your Office 365 administrator account.


b) In the Azure Administration Console, click Azure Active Directory, and under Manage click App Registrations.


c) Click the "+ New Registration" button:


d) Enter any name for your App such as MessageCentreAPI (must be a minimum of 4 characters):


g) Click the Register button at the bottom of the panel.


h) Once the App has been created, several IDs will be shown for your App.  The "Application (Client) ID" represents the ClientID you need for the flow. The Directory (Tenant) ID is the TenantID that you will also need for the flow.  Copy and save that value to use in your Flow.



i) Click the Certificates and Secrets menu item. Then click "New client secret"



j) In the Add Client Secret screen, enter any name for your key (maximum 16 characters) and select a Duration, after which it will expire (This is an added security feature as you don’t want secrets floating around forever).  Then click Save.


Once saved, the key (or SecretID in our case) will be displayed in the value field. 

Important: The Azure portal will only display the SecretID, at the time when it is initially generated.  You cannot navigate back to this page and retrieve the SecretID again later.



Copy and save that value to use in our Power Automate Flow. Be sure to keep your ClientID and SecretID saved privately and securely. 


Step 2: Grant Required Permissions to Your App

Once you have created the App and saved the ClientID and SecretID, you need to grant permissions to the app so it can access the Message Centre API.  You do this in the Azure portal using the following steps:

a) In the Settings page for your App, click the API Permissions menu item:


b) You will see a default Graph API User.Read permission there. Delete this as you don’t need it:


c) Click Add a Permission:

d) Select Office 365 Management APIs and click Select


e) Select Application Permissions


f) Select the “ServiceHealth.Read” permission:


g) Click Add Permissions

h) Once the permissions have been configured you will still see a warning notification in the Status column because admin consent hasn't been granted yet. Click the “Grant admin consent for <tenant name>” button:


i) Once you have granted consent you will see that the Status has been updated to Granted for <your tenant>:




Step 3: Create Flow from Template:

Within the existing Office 365 templates there is an excellent starter flow that does much of what you need to do. This is called “Email me a weekly summary of Office 365 message center notices” (which really rolls off the tongue):



When you initially open this template it will look like this:



This is a great start but you need to make some changes to make this work the way you need for this scenario. Here is an overview of what you are going to do to each of these components:




 

Step 4: Make changes to the existing template

First thing you need to do it edit the Recurrence time to be 1 day instead of 1 week.


Edit the “Get Office 365 messages” node. Select the “Show Advanced Options” to see all the values that you can edit. In here you need to add the Tenant ID, Client ID and Secret that was created in Azure AD in Step 1.



 Parse Subscribed Services

The original "Parse subscribed services" node from the original template is fine to use without editing. However, it doesn’t have the Category row in it that the Message Centre usually displays (plan for change, stay informed, etc), so this needs to be added:


The schema ends up looking like this:

{
    "type": "object",
    "properties": {
        "value": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "AffectedWorkloadDisplayNames": {
                        "type": "array"
                    },
                    "AffectedWorkloadNames": {
                        "type": "array"
                    },
                    "Status": {},
                    "Workload": {},
                    "WorkloadDisplayName": {},
                    "ActionType": {},
                    "AffectedTenantCount": {},
                    "AffectedUserCount": {},
                    "Classification": {},
                    "EndTime": {},
                    "Feature": {},
                    "FeatureDisplayName": {},
                    "Id": {},
                    "ImpactDescription": {},
                    "LastUpdatedTime": {},
                    "MessageType": {},
                    "Messages": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "MessageText": {
                                    "type": "string"
                                },
                                "PublishedTime": {
                                    "type": "string"
                                }
                            },
                            "required": [
                                "MessageText",
                                "PublishedTime"
                            ]
                        }
                    },
                    "PostIncidentDocumentUrl": {},
                    "Severity": {},
                    "StartTime": {},
                    "Category": {},
                    "Title": {}
                },
                "required": []
            }
        }
    }
}

 

Filter Array:

The filter array provided in the template has one filter for looking for the Message Type equaling Message Center. For this daily digest we don’t want to see all the messages but instead we want to see only those from the last day. To do this we will add another filter that only allows messages from the previous day. In this case I am using the last 25 hours to ensure that there is no gap between when a message was added and when the daily reoccurrence fires. To do this click on the Edit in Advanced Mode button and paste in one of the filter options below:


There are two choices on this filter. The first is to only list new items to the list and the second is to list new and updated items.

The filter for new items only for the previous 25 hours looks like this:

@and(equals(item()?['MessageType'], 'MessageCenter'),greater(item()?['StartTime'], addHours(utcNow(), -25)))

The filter for new and updated items for the previous 25 hours looks like this:

@and(equals(item()?['MessageType'], 'MessageCenter'),greater(item()?['LastUpdatedTime'], addHours(utcNow(), -25)))

Choose which ever one suits your needs better.


Conditional – If Statement

After we have filtered the array down it could be that we find that there are no messages. In this case rather than post an empty table we can instead have some text to tell us that there was not messages. To do this create a Condition and check the length of the array. If the array is 0 in length then send the message and if it’s not then continue processing the array.



The expression used here is to check the length of the Filter_array output:

length(body('Filter_array'))

 

If YES

If Yes then send a message “You can relax, there were no new Message Centre message today!”:



If NO

This section will contain the rest of the steps to generate the table and post it. The "If no" section is going to end up looking like the screenshot below. Follow the next steps to create this part of the flow:


 

Create HTML Table

Edit the Create HTML table from the original template to make the values equal those shown below (or you can select the table items that interest you):



In order to get a text list of the Affected Workloads for each Message Centre post you need to split the array and convert it to a string. You do this with the “join” expression:

join(item()?['AffectedWorkloadDisplayNames'],', ')

 

 Compose

Add a new Compose node. This will be used to make some edits to the HTML used in the table.


The expression is expanded in the Expression editor window:


Note: The way the expression pop over is displayed depends on how big your window is. So if it doesn't look like this then expand your window to see all the goodness.


In this compose block we give the HTML table a border to make it look better in Teams. To do this you create an expression where you replace the table tag with a table tag that has a border.

replace(body('Create_HTML_table'),'<table>','<table border="2">')

 

Compose 2


In order to make the table more pretty we give each of the cells 10 pixel padding around it.

replace(outputs('Compose'),'<td>','<td style="padding:10px;">')

 

Compose 3


Finally we make the table super pretty by making the header purple and have white text.

replace(outputs('Compose_2'),'<th>','<th style="padding:10px; background-color:#464EB8; color:white;">')

 

Teams Message

When editing the Teams message text ensure that you're in HTML editing mode and not the plain text editing mode. After doing that you want to enter the following HTML and under the Advanced section add a title (e.g. Daily Message Centre Update):


The HTML:

<html>

<body>

<p>&nbsp</p>

@{outputs('Compose_3')}

</body>

</html>


After doing this you will be done. Delete any remaining nodes that were left over from the initial template and test the flow. 


Daily Digest:

Here is an example of what the output should look like in the designated Teams channel: 



If there are no messages for that day you should see the following message posted in the channel:




Logic Apps


If you have Azure with consumption billing in place then you can also build this same flow and run it for under 1 cent per day using Logic Apps! 




This is a great option if you don't already have Power Automate licencing in Office 365. The one difference is that Logic Apps do not have the template that we used to build the Power Automate version from so there are a couple of extra steps you need to do to build some of the steps. It should be pretty straight forward to figure out based on the information already provided in the Power Automate flow. Here is an example that I built and tested in Logic Apps:




The Wrap Up


Taaa Daaaa, there you have it. Fun times with Power Automate. The great bonus here is that you and your team can now discuss and add conversations around these daily updates to further dig into areas that might be of interest to your business or customers. This can lead to valuable discussions about these changes that are persisted for future review. Enjoy!



0 comments to “Message Centre to Teams using Power Automate”

Post a comment

Popular Posts