Automating posts to Microsoft Teams using JavaScript

John Sibly
2 min readOct 18, 2019

--

It’s a pretty common in development to want to receive notifications, such as Build/CI completing, or alerts on the health of your software, on your company’s chat platform. At DAZN, we’re using Teams, and I wanted to find a simple way of posting messages to a channel. There didn’t seem to be clear instructions, so I’ve summarised what I found below:

To start with, you need a webhook URL for your Teams channel.

To create this simply right click on the channel, and select Connectors.

Right click on the channel, and select Connectors.

Next select Incoming Webhook, and follow the next steps to get a Webhook URL, specifying a name and icon as required:

Select Incomming Webhook as the type of connector
Select Incoming Webcook
Configure options for webhook
Configure the webhook and click Create

Keep this webhook secret as it allows anyone who knows it to post to this channel.

From this point, I found the absolute simplest method, is via a POST

curl -H “Content-Type: application/json” -d “{\”text\”: \”Hello World\”}” <YOUR WEBHOOK URL>

However, I’m not sure of the exact limit, but I found this would fail with larger messages.

A more robust approach is to construct an object using Microsoft’s MessageCard format.

This allows you to include colours, icons, actionable buttons, and also supports HTML content in the text section. An example of this format in JavaScript is show below:

const card = {‘@type’: ‘MessageCard’,‘@context’: ‘http://schema.org/extensions',‘themeColor’: “0072C6”, // light bluesummary: ‘Summary description’,sections: [{  activityTitle: ‘Read me!’,  text: ‘Hello world’,},],};

From here, you can simply post this object to your webhook, and your message appears in your channel.

I’ve wrapped this up as a simple node script to play around with in the following gist

If you integrate this into your Continuous Integration pipeline, store the webhook as a secret and have the script read it at runtime.

--

--