⚡Azure Deployment -> Microsoft Teams

Using Azure functions to hook up Azure app service deployment notifications to a Microsoft Team channel

Mattias Karlsson
5 min readJan 18, 2017

In this post I’ll demonstrate how Azure Functions can be the glue which with code lets you easily transform and adapt data coming from services so they can talk to any service even if they don’t speak the same “language”.
( I’ll be mentioning Kudu a bit in this post, if you don’t know what that is or want to learn more about it then I recommend you give a listen to The Azure Podcast episode 147 where I talk in-depth about it.)

Wouldn’t it be nice if you could get an message to Microsoft Teams when your web or functions has been deployed to Azure?

Or perhaps even more important when it doesn’t get deployed!

Unfortunately there’s no built in support in Teams for Azure app service post deployment webhooks, but as a developer this is where you see opportunity to code right? Fortunately Teams has a “generic” webhook you can create for each channel, which gets you an url you can post messages in a predefined JSON format. You create a webhook by:

  1. Pressing the “…” after a chanel
  2. Choosing “Connectors”
  3. Find “Incoming Webhook” and Press “Add”
  4. Give it a name and optionally upload custom icon
  5. Then you can copy the unique for chosen channel generated url

The format the Teams incoming webhook expects looks something like this:

But this is what Azure app service post deployment webhook delivers via Kudu:

So Kudu essentially provides all the info you need, we just need to massage it a bit so it conforms to what Microsoft Teams expect. In this post won’t go in detail how to get setup functions on Azure(for that I recommend you read my previous post “Going server less with PowerShell”), but rather just jump to some code.

A bare minimum Azure Function would look something like below:

It’ll just return “Hello World”, to any request so not yet very useful, what we first need to do is to parse the incoming JSON payload from the Kudu post deployment http call. Azure Functions has a few assemblies at your disposable out of the box (you can fetch more as needed utilizing NuGet), one of those is Json.Net which is a very popular for serializing/deserializing JSON on .Net. to indicate you want to load an assembly you use the #r directive followed by path to assembly and then you’ll ready to go, as Json.Net is one of the standard Function assemblies you only need to reference it by name. A very basic function that parsed JSON payload and returns a value based on that would look something like this:

This will take the JSON payload, parse it and return a new JSON structure with only one status property ending up looking something like this:

As we will always have the same schema for the incoming messages and are in a statically types language we can easily change from using a dynamic object to an class with types and properties letting Json.Net do the heavy lifting of deserializing the incoming JSON data, such a class for the post deployment payload could be defined something like below

and dynamic changed to var and a generic parameter hinting desired result

Then you can with a few anonymous classes transform the data to what the Teams incoming webhook expects

and then convert it to a JSON string

after that it’s just a matter of posting that data to the Teams webhook

and voilá that’s it out function is ready! (For your convenience I’ve made a complete sample available on GitHub here, if you want go get up and running quickly just fork that and choose “start from source control” on the functions initial quickstart)

Now we just need to hookup our function as a Kudu post deployment webhook. You find the link to each App Service under Development Tools -> Advanced Tools

You can also access it directly my inserting “.scm.” between your app name and “azurewebsites.net” i.e.

https://{my app service app}.scm.azurewebsites.net

You find webhooks under Tools->Web hooks

The url to your function will be something like:

https://{your_function_app}.azurewebsites.net/api/PostToMicrosoftTeams?code={function_token}&teamshookuri={your teams webhook without https://}

Now next time the App Service is deployed you should get a nice notification when it’s done.

There’s non coding solutions for these kind of scenarios, but the coding approach gives you fine grain control and the functions as a service approach that Azure Functions offer - really do make it low ceremony and you chances are high you can pick the language you’re comfortable with as it out of the box supports Batch, C#, F#, JavaScript, Python, Php, PowerShell and Bash.

The pay just for what you use model is a perfect fit for things like webhooks where load very much varies based on activity, i.e. a GitHub webhook will only trigger if something like a commit occurs, not paying for having a service up and running in between makes perfect sense!
You find the complete deployable code on GitHub at:

Feel free to reach out to me if you have any feedback or questions ⚡

--

--

Mattias Karlsson
Mattias Karlsson

Written by Mattias Karlsson

Partner & Technical fellow at WCOM AB. Microsoft Azure & Developer Technologies MVP. Been coding since I 80’s (C128 & Amiga). Father of 2, husband of 1.

Responses (2)