Build a Slack Bot

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

Update needed

The following information is related to LeanIX 3.4. The documentation will need to be updated to the changes in the new 4.0 release.

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 Microservicese.

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

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 links = payload.body.links.filter(function(obj) {
  return obj.rel == 'selfHtml';
});

var text = payload.meta.label;
if (links.length > 0) {
  text += ' "<' + links[0].href + '|' + payload.body.displayName + '>" ';
} else {
  text += ' "' + payload.body.displayName+'" ';
}

var messages = {
  'FACT_SHEET_CREATE' : 'created',
  'FACT_SHEET_UPDATE' : 'updated',
  'FACT_SHEET_DELETE' : 'deleted'
}

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

text +=' by <mailto:' + payload.user.email + '|' + payload.user.fullName + '>';

if (payload.event.isReverse) {
  delivery.active = false;
}

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

Just 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.

delivery.active = false;
for (var i = 0; i < payload.event.changes.length; i++) {
  if (payload.event.changes[i].key == 'businessCriticalityID') {
    delivery.active = true;
    break;
  }
}