Call Office 365 Service Communications API and Return New Service Health Notices – Part 3

Here is how I have managed to return the latest Service Health alert (only) from Office 365 "Office 365 Service Communications API".

Office 365 New Service Alert Email

As any O365 admin will know, Microsoft won't offer an inbuilt alert that will notify you by email when a new Incident arises so here is how I have chosen to get round that and here is a sneak peek of the output:

Firstly, we will need to register an app in Azure AD. Please see Register an App in the Azure Active Directory for instructions on how to do that.

Once we have the TenantID, ClientID and SecretID and our app has the required permissions, we are ready to build our Flow.

Start by locating the pre-made Microsoft template called Email me a weekly summary of Office 365 message center notices.

Configure your connectors as below:

Now name your flow and let's get to modifying it.

Would you like to buy Alan a coffee?

Visit the AlanPs1 Ko-fi page

Just below the Recurrence trigger, hover over the + sign and insert a new step.

Add 3 x Initialize Variable.

Name them Initialize TenantID variableInitialize TenantID variableInitialize TenantID variable respectively.

Now let's set the Recurrence to every 10 minutes and load our variables with the data we generated from the Azure App.

Note: Clearly, I am from the UK and have set my timezone to suit but please set yours to suit.

Also, there is no need for a Start time but having one means the Flow runs like clock work and delivers data at exactly 9:10am, 9:20am, 9:30am etc.

The template will then call Get Access Token to retrieve a bearer token, which will be used in a subsequent call to the services communication endpoint.

https://login.windows.net/@{variables('TenantID')}/oauth2/token

client_id=@{variables('ClientID')}&client_Secret=@{variables('SecretID')}&resource=https://manage.office.com&grant_type=client_credentials

The template will then parse the token, and ultimately call a GET method in the Office 365 Service Communications API endpoint within Get Office 365 messages below.

https://manage.office.com/api/v1.0/yourdomain.com/ServiceComms/Messages/api/v1.0/yourtenant.onmicrosoft.com/ServiceComms/Messages

Just in case you have an issue generating the schema for Parse JSON then it is listed below:

{
    "type": "object",
    "properties": {
        "token_type": {
            "type": "string"
        },
        "expires_in": {
            "type": "string"
        },
        "ext_expires_in": {
            "type": "string"
        },
        "expires_on": {
            "type": "string"
        },
        "not_before": {
            "type": "string"
        },
        "resource": {
            "type": "string"
        },
        "access_token": {
            "type": "string"
        }
    }
}

Parse JSON calls the body of the Get Access Token HTTP request in order to gather the access token.

Get Office 365 messages then using the access token to call your own relevant URI in the following format.

https://manage.office.com/api/v1.0/<domain>/ServiceComms/Messages/api/v1.0/<tenant>.onmicrosoft.com/ServiceComms/Messages

The following  takes the data from Get Office 365 messages, looks for only MessageType not equal to Restoring Service and with a StartDate of 10 minutes ago or less.

No we are starting to work with the data that has been returned by the request.

1a.

body()['Get_Office_365_Messages']

1b. Build schema using "Use sample payload to generate schema" with JSON from previous step.

2a.

body('Parse_Subscribed_Services')['Value']

2b.

@and(not(equals(item()?['Status'], 'Restoring Service')))

3a.

body()['Filter_Array']

3b.

@greaterOrEquals(item()?['StartTime'], addminutes(utcnow(), -10))

Now, before we create an HTML table we will need to know if the previous request and it's filters have any data.

4.

@not(empty(body('Filter_array_2')))

NB: If there is no new incidents in the last 10 minutes this Flow will complete as a success but with no email output.

EDIT (Step 4): 6th February 2019 – https://www.alanps1.io/power-platform/flow/flow-so-where-has-edit-in-advanced-mode-gone-on-condition-actions/

When there has been data returned the following will create an HTML table:

5a.

body()['Filter_Array_2']

5b.

item()['WorkloadDisplayName']

5c.

item()['Title']

5d.

item()['ImpactDescription']

5e.

item()['MessageType']

5f.

item()['Severity']

5g.

item()['Classification']

5h.

item()['Id']

5i.

formatDateTime(item()['LastUpdatedTime'],'dd-MM-yyyy HH:mm:ss')

5j.

formatDateTime(item()['StartTime'],'dd-MM-yyyy HH:mm:ss')

Now time to send the email, but I prefer to link an Office 365 Profile first

And here is what I have added to my HTML Email (See advanced options)

7a.

formatDateTime(utcNow(),'dd/MM/yyyy | HH:mm')

7b.

body()['Create_HTML_Table']

7c.

<html>
   <head>
      <style>
         table {
         border-collapse: collapse;
         width: 100%;
         text-align: center;
         }
         td, th {
         border: 1px solid #ddd;
         padding: 4px;
         }
         th {
         padding-top: 8px;
         padding-bottom: 8px;
         background-color: #7F7D7E;
         color: white;
         font-weight: normal;
         }
      </style>
   </head>
   <body>
      <span style="display:none">
      Attention! You have a live O365 Incident. Just Landed! ...
      <br/><br/>
      </span>
      <span>
      <img src="#" alt="Text" height="100" width="114">
      </span>
      <br/><br/>
      @{body('Create_HTML_table')}
      <br/><br/>
      <span>Click <a href="https://admin.microsoft.com/AdminPortal/Home#/servicehealth">here</a> to visit the Service Health Dashboard</span>
   </body>
</html>

As a foot note, I can spot one or 2 ways that could enhance or improve this Flow. You may be able to also. If you feel you would like to offer an improvement then please do not hesitate to let both myself and the community know by posting below and if you have any questions please ask away and I will do my best to answer.

Alan

Part 4 >>>

Credit:

This blog is an enhancement of this extremely helpful blog post

Credit to Antonio Maio for the content in that post.

Do you need assistance building this Flow?

 

Visit the Get Help section of the Power Automate Community