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
server.servlet.context-path=/
slack.webhook=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXX
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:

<span><dependency></span>
<span><groupId></span>com.github.seratch<span></groupId></span>
<span><artifactId></span>jslack<span></artifactId></span>
<span><version></span>1.5.6<span></version></span>
<span></dependency></span>
<span><dependency></span>
    <span><groupId></span>com.github.seratch<span></groupId></span>
    <span><artifactId></span>jslack<span></artifactId></span>
    <span><version></span>1.5.6<span></version></span>
<span></dependency></span>
<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

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

And Wow! Your message exposed in slack.

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

silviobuss / demo-jslack

Send slack messages with jslack

demo-jslack

Send slack messages with jslack

What is this and how it works?

https://dev.to/silviobuss/send-slack-messages-with-java-in-5-minutes-2lio


View on GitHub

原文链接:Send slack messages with Java in 5 minutes

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
Suffer all the pain can destroy a person, but it also can kill the pain.
一切痛苦能够毁灭人,然而受苦的人也能把痛苦消灭
评论 抢沙发

请登录后发表评论

    暂无评论内容