DEV Community

Cover image for Send slack messages with Java in 5 minutes
Silvio Buss
Silvio Buss

Posted on • Edited on

Send slack messages with Java in 5 minutes

Introduction

In this tutorial, we will see how easy it is to send messages to a Slack channel in our Java application with jslack framework.

jSlack

jSlack is a Java library to easily integrate your operations with Slack. This library supports all the APIs listed in Slack platform features and APIs.

Incoming Webhook with Slack

Webhook is a simple way to post messages from external sources into Slack via ordinary HTTP requests. See the Slack Incoming Webhooks guide for more details.

Getting started

Getting the Webhook URL

The first thing we need to do is find out from Slack the correct URL it will use to post the messages.

Create Demo App

Go to https://start.spring.io/ and create a demo app with the Web dependency.

Import application in your IDE and follow steps below:

1 - Edit application.properties file.



server.servlet.context-path=/
slack.webhook=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXX


Enter fullscreen mode Exit fullscreen mode

2 - Edit pom.xml file and add jslack:



<dependency>
    <groupId>com.github.seratch</groupId>
    <artifactId>jslack</artifactId>
    <version>1.5.6</version>
</dependency>


Enter fullscreen mode Exit fullscreen mode

3 - Add controller and service classes:

Controller
controller

Service
service

Run Demo App

  • Start demo app just running a main class.

  • Using any rest client, make the POST request:
    http://localhost:8080/apps/my simple message here

request

And Wow! Your message exposed in slack.

slack_result

Conclusion

In this post, we can see how its works slack webhook integration implemented in Spring Boot 2 with jslack. This framework also provides support to real-time Messaging API, events API, among other resources.

Sources

GitHub logo silviobuss / demo-jslack

Send slack messages with jslack






Top comments (0)