How to build a Hangouts Chat bot for MS Azure DevOps alerts

Ok, so we’re gonna start by saying how to programmatically post incoming messages from Azure DevOps onto a Hangouts chat room using a bot.

Chat bots are really useful when you want to send notifications to team members in a chat room. A typical use case would be to show alerts triggered by an external event, in this case an event that came out from Azure DevOps, e.g. : a successful build, a release configured previously on your deploy pipeline, and also new pull requests that are active waiting for approval, pull requests that are being merged to a particular branch or just changes made to a particular work item. The possibilities are endless!

So here are the required steps to send the events that came out from Azure to your configured bot on a particular Hangouts chat room.

Step 1: Create and Configure the bot

We’re gonna start by adding the bot to the chat room. You can do so by right-clicking on the chat room name and hit the “Configure webhooks” button.

That opens up a new dialog window in which you select a name for the bot and optionally an image url for the icon. You could just use an image url that you find doing a web search in Google, Bing or any other provider. In our case we just uploaded an image to our AWS S3 bucket and set the image permissions to be public. Then, we paste that url in the box.

Step 2: Configure the trigger events on Azure DevOps

Then we’re going to configure the notification events on the Azure DevOps Portal for our project.

We click on the “Service Hooks” button which will allow us to add new events, but also allow us to edit or remove them.

  • Here’s an example list of configured events: Build Completed, PR created, updated, Release deployment completed and Work Item updated.

  • To create a new one, we hit the green plus sign, and we select on the left panel the “Web Hooks” option. Then, we hit “Next”

  • Here, we select the event trigger like discussed before, and hit “Next”

  • Here’s an example of the trigger configuration. We could just leave default values or select a particular repository, a target branch and a combination of users/members that makes the PR request and/or approval. He hit “Next” again to go to the next tab.

  • Here we can indicate parameter configuration for the POST request, for example authentication, which type of message details we want to post and the most important value is the actual URL which will receive the JSON message. We’re gonna use the URL value of a configured AWS API Gateway in the upcoming steps. Hang on 😉

 

Step 3: Create and configure an AWS Lambda Function

Now it’s time to create an AWS Python 2.7 Lambda function.

Log in to your AWS console and go to your Lambda function section.

Then hit the orange “Create Function” button and select one of the existing blueprints so we don’t have to configure all the stuff from scratch and avoid boilerplate.

On the Blueprints section just type “gateway” and select the one on the right which just takes care of creating the API Gateway linked to your lambda function but also the required permissions and roles.

  • Next, you name your function and where it says: “Pick an existing API, or create a new one” you just select the one AWS created for you.

Now, you can safely confirm and create the new function.

  • On the new screen you can see (among other stuff) your function code clicking on the name, and also clicking on the API Gateway button you can see on the API endpoint the public URL that links the gateway to the function. This is the address that we were talking about in previous steps. You have to copy and paste this one on Azure DevOps Service Hook trigger to connect the pieces.

  • Finally, the Python lambda code itself. It uses a “bot_url”, which is the one obtained from the Hangouts Chat Room when creating the bot on the Incoming Webhooks option. We paste that URL here.

Optional: you can use the query string  parameter called “threadKey” to be anything you want. This allows you to group messages in the same thread on the Hangouts Chat room, for example grouping this type of events in the same bubble on the room, and leave other threads for talking between team members of a project. You get the idea, right?

2018-10-16T21:11:05+00:00By |