Creating a TFS Build Status Bot for Microsoft Teams

2 minute read Published:

Taming webhooks
Table of Contents

At work we are using Microsoft Teams and on-premises Team Foundation Server. I thought it would be nice to receive notifications from the failed CI builds into the channel on Teams, but unfortunally there was no such feature in TFS, so I decided to whip one up using Webhooks.

Here’s how you can do the same.

Step 1

Near your channel name in Teams, press the three dots ‘…’ and click ‘Connectors’. There, find ‘Webhooks’ and press ‘Add’.

Step 2

You’ll be presented with the following dialog:

Add connector

Fill in the name of your ‘bot’, I called it TFSBot, select an Icon and press Create. You’ll be given a URL that we’ll use later on. It’ll look something like https://outlook.office.com/webhook/ZZZZZZZ/IncomingWebhook/ZZZZZZZZ

Step 3

Create an outgoing Webhook in TFS.

Open TFS settings for your project and go to Service Hooks:

Service hooks

Press ‘+’ and create new Service Hook with the type ‘Web Hook’ and event ‘Build Completed’. Then fill in the details of the host where you will run your bot.

Step 4

Write the code! Here’s mine:

import * as restify from 'restify';
// Setup Restify Server
var server = restify.createServer();
var client = restify.createJsonClient({
    url: "https://outlook.office365.com",
    version: '*'
})
server.listen(process.env.port || process.env.PORT || 3978, function () {
   console.log('%s listening to %s', server.name, server.url);
});
server.use(restify.bodyParser());

server.post('/api/notify', function(req, res){
    console.log(JSON.stringify(req.body));
    var text = req.body.message.markdown;
    var result: any = {text};
    if (req.body.resource.requests && req.body.resource.requests.length > 0){
     result["title"] = `Build requested by @${req.body.resource.requests[0].requestedFor.displayName} failed!`;
    }
    console.log(result);
    client.post("/webhook/ZZZZZZZZ/IncomingWebhook/ZZZZZZZ", result);
    res.json(result);
});

And that’s it! Now, each time a build fails on TFS, the build system will send a POST request to your script at the API endpoint you specified with build details, then your code will forward those details to Microsoft Teams API to be posted in the channel. Pretty nifty.

Enjoy!

comments powered by Disqus