Java - Sending Message to Slack Webhook

If you're using Slack, it can be very useful if your application can send messages to the messaging platform. The reason is it allows important events to be notified to your teams quickly. For example, if something error or doesn't work as expected, it would be better if the application can send what happens so that certain people can know it as soon as possible. This tutorial is about sending message to Slack from Java application.

Setup Slack Webhook

1. Create a Slack app (if you don't have already)

Creating a Slack application is very simple. Open this link. You'll be asked to enter your application name and choose the development workspace. Just follow all the steps.

Create a New Slack App

2. Enable Incoming Webhook

Open your application page and click on Incoming Webhooks in Feature section. You'll be redirected to a page where you can manage incoming webhooks. What you need to do is make incoming webhooks enabled by clicking on the toggle button.

Slack - Manage Incoming Webhooks

3. Add Webhook URL

After that, scroll to the bottom of the page and click on Add New Webhook to Workspace button. You should see a page for confirming your identity on your workspace. For each webhook URL, you can only choose one channel where messages can be post to via the URL.

Slack - Confirm Identity

If successful, you should see a new URL added in your Webhook URL list. It should look something like this:

https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/XxxxXxxxXxxxXxxxXxxxXxxx

Dependencies

There are some dependencies you need to download to your project in order to follow this tutorial. If you're using maven, add the following to pom.xml file.

pom.xml

  <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
  <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.4</version>
      <scope>provided</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
  <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.6</version>
  </dependency>

  <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>3.6</version>
  </dependency>

Or if you're using gradle, add the following.

build.gradle

  compile 'org.projectlombok:lombok:1.18.4'
  compile 'org.apache.httpcomponents:httpclient:4.5.6'
  compile 'com.fasterxml.jackson.core:jackson-databind:3.6'

If you're using any other dependency management, just adjust it yourself.

Code

First, define a class that represents a message that will be sent as response body. To make it simple, in this tutorial, I only use four arguments:

  • username: the user that will appear as the message sender. It will be labeled as APP to distinguish between bot/system and real user.
  • text: the message text.
  • icon_emoji: emoji of the message.

For full list of all supported arguments, read the documentation of Slack's chat.postMessage API.

If your webhook is created using legacy integrations, you can choose the destination channel by adding channel argument. You won't be able to do it if your webhook is created from a Slack app, as exemplified in this tutorial.

SlackMessage.java

  package slack;
  
  import lombok.*;
  
  import java.io.Serializable;
  
  @AllArgsConstructor
  @Builder(builderClassName = "Builder")
  @Getter
  @Setter
  public class SlackMessage implements Serializable {

    private String username;
    private String text;
    private String icon_emoji;
  }

To make the code reusable, it's better to define a utility class with a method for sending message. To send a message to Slack webhook, we need to send a POST method to the webhook URL. The request body which is the message can be in x-www-form-urlencoded or application/json. We use the later in this tutorial.

SlackUtils.java

  package slack;
  
  import com.fasterxml.jackson.databind.ObjectMapper;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.entity.StringEntity;
  import org.apache.http.impl.client.CloseableHttpClient;
  import org.apache.http.impl.client.HttpClients;
  
  import java.io.IOException;
  
  public class SlackUtils {
      private static String slackWebhookUrl = "https://hooks.slack.com/services/TXXXXXXXX/BXXXXXXXX/XxxxXxxxXxxxXxxxXxxxXxxx";
  
      public static void sendMessage(SlackMessage message) {
          CloseableHttpClient client = HttpClients.createDefault();
          HttpPost httpPost = new HttpPost(slackWebhookUrl);
  
          try {
              ObjectMapper objectMapper = new ObjectMapper();
              String json = objectMapper.writeValueAsString(message);
  
              StringEntity entity = new StringEntity(json);
              httpPost.setEntity(entity);
              httpPost.setHeader("Accept", "application/json");
              httpPost.setHeader("Content-type", "application/json");
  
              client.execute(httpPost);
              client.close();
          } catch (IOException e) {
             e.printStackTrace();
          }
      }
  }

Lastly, use the sendMessage method to send a message by passing an instance of SlackMessage. Below is the example of how to use it.

Example.java

  package slack;
  
  public class Example {
    public static void main(String[] args) {
      SlackMessage slackMessage = SlackMessage.builder()
        .channel("the-channel-name")
        .username("user1")
        .text("just testing")
        .icon_emoji(":twice:")
        .build();
      SlackUtils.sendMessage(slackMessage);
    }
  }

Run the example code and check if the message has been delivered successfully.