java, slack, spring, bot

Stop building UI’s, build a Slack bot using JBot

The problem

So you just build a great backend tool and of course you need to build some kind of UI to interact with it.

That’s where the nightmare starts.

Your design will suck,

You will have to choose a front end stack,

Résultat de recherche d'images pour "list of front end technologies"

You will get laughed at by the React crowd and the Angular people will mock you telling you it only takes 20 minutes to build a nice front end,

You need to be mobile friendly, you will need authentication,

And the list goes on and on ….

The Solution

So why not get rid of all the boring issues building frontend and use a Bot. Since we are all using Slack, this should be a no brainer right ?

So what I want, is to be able to interact with my backend application using a simple slack bot.

A few Google searches, I came across JBot, a Java library to build just that !

Make Slack and Facebook Bots in Java

Let’s give it a try.

JBot : The 10 minute test

Objective

Here is the challenge, I want to be able to get a simple bot running that can do the following :

If paste a URL in a conversation, if the URL is of a given domain, our bot should catch the message and reply to us with a nice response.

Preparation (5 min)

The first rule of 10 minute tests is to read the docs .. and in this case, it seems to be very simple to get running

How to write your first SlackBot in Java

Step 1 : Let’s clone the project

git clone https://github.com/rampatra/jbot.git

Step 2 : Let’s create a slack bot

To create a bot on Slack, go to the App Directory and search for “bot” and create a new configuration

and let’s give it a nice name houlahop-bot

Slack will then give you an API token that can be used by the back end application to interact with the bot. We’ll certainly have to use this somewhere in JBot.

Save your API token to use in JBot

Optionally, you can also configure your bot’s icon and help message to explain what it does.

Step 3 : Edit application.properties

You only need to change the line with the slackBotToken property and use the token we got at the previous step.

slackBotToken=xoxb-xxxxxxxxx

We can already run the application and see if this works.

mvn clean package -DskipTests
cd jbot-example
mvn spring-boot:run

You should now have a Spring Boot app running and the jbot sample bot listening. But listening to what ?

Well to the channels the bot has been invited to. So let’s invite him to a channel.

Create a channel called #jbot and invite @houlahop-bot to it by typing

/invite @houlahop-bot

You should see this :

Now, everything that you type can been seen by our bot. You can check the logs of your spring boot application and you will see the bot code noticed it was invited to a channel and responded by a “Hi, I am houlahop-bot” :-)

Implementation (5 min)

Now let’s see how we can customize the bot’s behaviour based on our use case.

Just as a side note, JBot allows you to handle the 3 possible interactions with Slack

  • Incoming Web hooks
  • Slash Commands
  • Bots

We are only focusing on bot interaction for the moment, but I invite to test the others as they might better fit your specific use case.

JBot is built around annotation, so to implement the bot’s behaviour it’s just a matter creating your custom Bot class annotated with the JBot annotation

@JBot
public class SlackBot extends Bot {
...
}

And add handlers method annotated with the Controller annotation

@Controller
public void onMessage(WebSocketSession session, Event event) {
...
}

This method will be called every time a message is sent on the channel. We only want to catch message that match a URL pattern and from a specific domain.

In JBot, we can use the pattern parameter of the annotation to filter on message matching a specific pattern.

@Controller(pattern = "https://medium\\.com/")
public void onMessage(WebSocketSession session, Event event) {
    reply(session, event, "Hi, it seems you pasted a medium URL");
    reply(session, event, "The url was : " + event.getText());
}

Let’s restart our application and paste a medium url in the channel …

Bingo !

We now have a two way communication channel between our slack channel and our back end application. Possibilities are endless.

Building Slack Bots in Java with JBot

What’s next

I invite you to investigate JBot further as it can also help you handle conversation (a sequence of multiple messages), slash commands, implement Facebook bots as well …

Feel free to comment on this first article, or tell me if you have used other techniques to solve the same problem, or used JBot for other purposes !