Send your application Insights Alert Data to Microsoft Teams

When testing out Microsoft Teams and reading up on the documentation I noticed that the webhooks that work in Teams are the same as in an Office 365 group. This is great news because we already know how to connect to a Office 365 group using a webhook and it’s already documented by Microsoft and many others. So lets get started on getting our alerts into our team.

Create a webhook in Microsoft Teams

To create a webhook in Microsoft Teams I used the client. Just login in and select the 3 dots next to the channel.

image

This will take you to the connectors page. It can be that you need to login again. Here select the ‘Incoming Webhook’ connector. Give it a meaningful name and if you want give it another image. Then when you click on Create it will give you a url where to send the information to. Copy this one and keep it somewhere.

You cannot connect this directly into your Application Insights so we need something in between to do the translation. This is where Logic Apps come into play. This is a really cool feature in Azure and it makes it really easy to connect things together or to make simple workflow like processes. Now create a new Logic App in Azure

Create a Logic App

 

image

Give it a name, choose a location, add it to a subscription and place it in a new or existing Resource group.

image

Now you can start editing your logic app. Choose a blank Template. As a trigger we need a Request. It will look like this.

image

The URL will only be generated when we save but first we have to finish our Logic App. In the Request Body JSON Schema fill in the following JSON.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "properties": {
    "context": {
      "properties": {
        "name": {
          "type": "string"
        },
        "portalLink": {
          "type": "string"
        },
        "resourceName": {
          "type": "string"
        }
      },
      "required": [
        "name",
        "portalLink",
        "resourceName"
      ],
      "type": "object"
    },
    "status": {
      "type": "string"
    }
  },
  "required": [
    "status",
    "context"
  ],
  "type": "object"
}

This JSON defines what our request looks like. This data is what a default Application Insights webhook alert sends. You can see it sends things like the Status, ResourceName and a link to the Azure Portal to open up the source of the Alert.

Now we need to translate this into a request for our Teams webhook. Create a new step. We want to add an action that does an HTTP request so select this.

image

image

 

We need to do a HTTP Post to our MS Teams webhook so choose POST as a Method. In the Uri part we now need to fill in the Uri we got when we created the MS Teams webhook connector. In the Headers section we need to specify that it’s Content-Type is “application/json”. As for the body part we can format and style the text we want to appear in our channel to our own liking. As long as we keep to the specifications provided my Microsoft. More info can be found here. I just wanted a simple notification so I used the following JSON.

image

image

As you can see we can even use dynamic parameters. The one specified in the JSON schema from the request automatically show up here. This makes it easy to create our content we want to post to Teams.

The end result will look like this.

image

Now we can save our Logic app. Don’t forget to copy the Request Uri that is filled in now somewhere because we need it later.

Set the webhook we just created in our Application Insights Alert

Now in our Application Insights we can create webtests. These can be simple pings to check if a site or our app is still online but they can also be really complicated multi-step tests created in Visual Studio.

When we go to such a test in the Azure Portal and edit it we can set Alerts on it. These Alerts can be an email will be send but it can also be a webhook. Here we can paste the Uri of the Logic App request that we copied earlier.

image

Test the result

 

Now the only thing remaining is that we test out the result. So stop your website or whatever you need to do to activate the alert. When the alert is send you will also receive a message in Microsoft Teams.

image

It will even notify you when your site is back online.

image


Also published on Medium.