{{announcement.body}}
{{announcement.title}}

Integration Between Java and Slack With Webhooks

DZone 's Guide to

Integration Between Java and Slack With Webhooks

I will explain how to create simple integration between Java and Slack with use of Slack Incoming Webhooks and Spring Framework.

· Java Zone ·
Free Resource

As you probably know, Slack is one of the most common company communicators nowadays. All possible plugins, apps, and bots, which can extend its functionalities, are a hot topic recently, especially as of now when more and more companies are forced to work remotely. Through the course of this article, I will try to show how to create a simple, yet useful, integration between Java and Slack. I will use links to Slack documentation and tutorials which provide detailed description of Slack features.

What in the World Are Slack Incoming Webhooks?

Here the answer is simple but meaningful. Webhooks are an easy (probably the easiest) way to post messages from custom apps to Slack users and channels. They use JSON as messages data format, and they also allow us to use the standard Slack formatting and layout blocks.

To use them, you have to create a Slack app. I did not manage to find a properly descriptive tutorial.

I recommend to open this link https://api.slack.com/apps and click the big green button Create an AppInemodal, you will have to choose the name and workspace for the app. After creating the app, you have to enable Incoming Webhooks. On the bottom of the screen, with Webhooks enabling checkbox, you will see a button Add New Webhook to Workspace, which will allow you to get the Webhook of particular channel. Now you have to copy the last part of new Webhook. In the example below the part, which should be copied is marked in red: https://hooks.slack.com/service/T016M7S1PK4/B0187LJ9FFZ/Nw1hTMVJldSQQaXfpFYf2Y2e

If you have any troubles with configuring app Webhooks or simply want to know more about them, I recommend you to read the article from the link below. The article comes from official Slack documentation and concerns sending messages with Webhooks. 

https://api.slack.com/messaging/webhooks

Slack theory done. It is time for some coding

Before we start – quick disclaimer.

I know that the code quality and design of provided examples can be considered questionable. Please remember that it is a more PoC level code rather than something that's ready for production.

Let’s create our app step by step

Step 1

We will need a new spring boot project as a base. chose Spring Boot as it provides plenty of configurations out of the box and has a number of modules which allow us to set up our app faster and add new features to it later on. It is also very well integrated with IntelliJ. If you do not have IntelliJ installed I recommend to use Spring Initializr from this link here https://start.spring.io/. We will need only one artifact, namely, Spring Web so project configuration should not be hard for anyone.

Step 2

After the previous step, we have fully functioning Spring Boot app. Now, iis time to add something useful there. We start with creating a POJO named Message which will represent our message from the HTTP request. 

Then, we create simple REST controller called MessageInterceptorController. We will also create a service called MessageSender, which we will further inject into the controller. A code after this step should be more or less like this:

Java


Java


Java

Step 3

Now, when we have the base of our middleware, we will add some functionalities. In the controller, we add single endpoint with @PostMapping and path /messages/{userName}The endpoint will get the value of variable userName with usage of @PathVariable and our message with help of @RequestBody

Both values will be further used to call newly created method sendMessage in MessageSenderwhich will be the most vital method of this app. After this step our code should be similar to the one from below. I did not paste the content of Message class because it remains unchanged from the previous step.

Java


Java


Step 4

In this step, we will add a Map, which will store the relation between a user name and the particular user channel Webhook, so the user name will be Map key and the channel Webhook will we Map value. We will also add a constant field which will store a common part of Webhooks URL. Your code after this step should be similar to the one below. Of course your user name and channel Webhook should be different then my.

Java


Step 5

Here, we will implement sendMessage itself. Firstly, we will get the channel Webhook from our Map and create a full Webhook URL with it. Secondly we instantiate RestTemplate which will be our HTTP client. Thirdly we have to create JSON from our message. We will do it using ObjectMapper . The next thing to create HTTP Header for ContentType with value aplication/json . Then we create HttpEntity of type String and use it to call exchange method from RestTemplate which will send our request to the Slack.

Java


Java

Step 6

With the main functionality done, it is time for short refactor. We will focus on improving exception handling. We will also change our endpoint to return ResponseEntity object with the correct HTTP code depending on how the request will be processed.

Java


Et voila - integration done

Now it is time to do some tests

For such simple tests, I recommend curl, but it can be also done with Postman, depending on which one you prefer. I will try to describe an example for both of them.

I will start with curl.

Here the only thing that you need is one line of script from below

Shell


In Postman you have to choose POST as request method then copy this URL: http://localhost:8080/messages/Pask into proper field. You also have to specify the body content type with value JSON. To do so, you have to copy this string into the body tab located below URL field. As you can see i provided a screen-shot with the Postman configuration.

GET request in Postman


The last thing to do is to click the Send button. You should get a desired message on a selected Slack channel.

Notifier in Slack

Now our integration has been tested.

Some ideas how to make it even better

1. As for now the list of channels’ Webhooks is static and requires a new deployment to update. Here it is the first place to make our middleware better. For example we can use the properties file, a simple text file or even a database. H2 will be extremely useful here (remember to encode Webhooks).

2. Add an endpoint for sending a message to many users from one request

3. Put the common part of Webhook URL in Spring Boot application.properties file

Summarizing

That is all for today. Thanks for your time. I hope that you find this example useful and it will give you some new ideas on how to make Slack integrations.

Topics:
integration, java, opinion, rest, slack, spring, tutorial, webhooks

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}