Build a Slack Bot

Here we describe how we build our own Slack Bot using LeanIX Webhooks

Our first LeanIX Bot

Slack is a great way to bring all your communication together in one place. We at LeanIX love Slack and use it for communication and real-time messaging within and across our teams. Our use case is, to let everybody know when there are changes in the LeanIX Inventory. Of course we also use LeanIX internally to manage all our Applications and Microservices.

LeanIX and Slack can be integrated easily using Webhooks, you only need 5 min to set everything up.

1202

How does it work

Because Slack has a state of the art architecture, it provides Incoming Webhooks which are a simple way to post messages from external sources into channels. They make use of normal HTTP requests with a JSON payload that includes the message text and some options. We use LeanIX Webhooks to listen to all changes in the LeanIX inventory and then post a message into the Slack Channel.

Two main steps:

  1. Create a new Incoming Webhook in Slack

  2. In the LeanIX Administration create a new Webhook using the URL created in step 1. Set Triggering events to "All events" and use the following JavaScript code as Callback:

var payload = delivery.payload;
var text = payload.factSheet.displayName;

var messages = {
  'FactSheetCreatedEvent' : 'created ',
  'FactSheetUpdatedEvent' : 'updated ',
  'FactSheetDeletedEvent' : 'deleted '
}

if (payload.type in messages) {
  text += messages[payload.type];
} else {
  text += 'undefined ';
}

delivery.payload = {
    text : text,
    username : 'LeanIX',
    icon_url : 'https://app.leanix.net/img/icon.png'
}

React on specific changes

Sometimes you want your SlackBot to send messages only when specific changes happen, e.g. when the Business Criticality has changed. This is possible, because the changed fields are also transmitted as part of the event structure. Just add the following code to the example above.

var payload = delivery.payload;
var text = delivery.payload.type;
var path = delivery.payload.path;

delivery.active = false;
if ( delivery.payload.path == '/businessCriticality' ) {
   delivery.active = true;
}

delivery.payload = {
   text : text,
   path : path,
   username : 'LeanIX'
}

🚧

IMPORTANT:

Please make sure you do the following steps. You are going to need a valid API Token. To create an API token, please see the details in Technical User.