Skip to content
J2ObjC compilable jSlack (a Java Slack library)
Java Other
  1. Java 99.8%
  2. Other 0.2%
Branch: version/3
Clone or download
Pull request Compare This branch is 6 commits ahead, 218 commits behind seratch:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
slack-api-specs @ fbfd292


Maven Central Build Status

jSlack is a Java library to easily integrate your operations with Slack. The library currently supports the following APIs.

Getting Started

Check the latest version on the Maven Central repository.

The following is an example using Maven. Of course, it's also possible to grab the library via Gradle, sbt and any other build tools.

<version>{latest version}</version>

See also: Getting started with groovysh

If you really don't need classes for building Slack app backend (= you need only Webhook/Web API/RTM API clients), just depending on jslack-api-client would be enough.

<version>{latest version}</version>


Incoming Webhooks

Incoming Webhooks is a simple way to post messages from external sources into Slack via ordinary HTTP requests.

jSlack naturally wraps its interface in Java. After lightly reading the official guide, you should be able to use it immediately.

import com.github.seratch.jslack.*;
import com.github.seratch.jslack.api.webhook.*;

String url = System.getenv("SLACK_WEBHOOK_URL");

Payload payload = Payload.builder()
  .text("Hello World!")

Slack slack = Slack.getInstance();
WebhookResponse response = slack.send(url, payload);
// response.code, response.message, response.body

Here is a small example using Block Kit.

ButtonElement button = ButtonElement.builder()

LayoutBlock block = ActionsBlock.builder().elements(Arrays.asList(button)).build();

List<LayoutBlock> blocks = Arrays.asList(block);

Payload payload = Payload.builder().blocks(blocks).build();

Slack slack = Slack.getInstance();
WebhookResponse response = slack.send(url, payload);
// response.code, response.message, response.body

It's also possible to directly give a raw payload.

String payload = "{\"text\": \"Hello there!\"}";
WebhookResponse response = slack.send(url, payload);

API Methods

There are lots of APIs to integrate external sources into Slack. They follow HTTP RPC-style methods.

When the API call has been completed successfully, its response contains "ok": true and other specific fields.

    "ok": true

In some cases, it may contain warning field too.

    "ok": true,
    "warning": "something_problematic"

When the API call failed or had some warnings, its response contains "ok": false' and also have the error field which holds a string error code.

    "ok": false,
    "error": "something_bad"

jSlack simply wrap API interface. Find more examples in this library's test code.

import com.github.seratch.jslack.*;
import com.github.seratch.jslack.api.methods.request.channels.*;
import com.github.seratch.jslack.api.methods.response.channels.*;

Slack slack = Slack.getInstance();

String token = System.getenv("SLACK_BOT_TEST_API_TOKEN");
ChannelsCreateRequest channelCreation = ChannelsCreateRequest.builder().token(token).name(channelName).build();
ChannelsCreateResponse response = slack.methods().channelsCreate(channelCreation);

Or, using lambda function to build a request could be much simpler. You don't need to type the long class name!

final String token = System.getenv("SLACK_BOT_TEST_API_TOKEN");
ChannelsCreateResponse response =
  slack.methods().channelsCreate(req -> req.token(token).name(channelName));

API Methods Examples

You can find more examples here:

Post a message to a channel
String token = "api-token";
Slack slack = Slack.getInstance();

// find all channels in the team
ChannelsListResponse channelsResponse = slack.methods().channelsList(req -> req.token(token));
assertThat(channelsResponse.isOk(), is(true));
// find #general
Channel general = channelsResponse.getChannels()
  .filter(c -> c.getName().equals("general"))

ChatPostMessageResponse postResponse = slack.methods().chatPostMessage(req -> req
  .text("Hello World!"));
assertThat(postResponse.isOk(), is(true));

// timestamp of the posted message
String messageTimestamp = postResponse.getMessage().getTs();


ChatDeleteResponse deleteResponse = slack.methods().chatDelete(req -> req
assertThat(deleteResponse.isOk(), is(true));
Open a dialog modal
final String token = "api-token";
// Required.  See
final String triggerId = "trigger-id";

Slack slack = Slack.getInstance();

DialogTextElement quanityTextElement = DialogTextElement.builder()
  .hint("The number you need")
  .placeholder("Required quantity")

DialogSelectElement colourSelectElement = DialogSelectElement.builder()
  .placeholder("Choose your preferred colour")

Dialog dialog = Dialog.builder()
  .title("Request pens")
  .elements(Arrays.asList(quanityTextElement, colourSelectElement))

DialogOpenResponse openDialogResponse = slack.methods().dialogOpen(req -> req

Events API

The Events API is a streamlined, easy way to build apps and bots that respond to activities in Slack. All you need is a Slack app and a secure place for us to send your events.

AppUninstalledHandler appUninstalledEventHandler = new AppUninstalledHandler {

  public void handle(AppUninstalledPayload event) {
    // do something here

public class SampleServlet extends SlackEventsApiServlet {

  protected void setupDispatcher(EventsDispatcher dispatcher) {

Real Time Messaging API

Real Time Messaging API is a WebSocket-based API that allows you to receive events from Slack in real time and send messages as user.

When you use this API through jSlack library, you need adding additional WebSocket libraries:


The following example shows you a simple usage of RTM API.

import com.github.seratch.jslack.*;
import com.github.seratch.jslack.api.rtm.*;

JsonParser jsonParser = new JsonParser();
String token = System.getenv("SLACK_BOT_API_TOKEN");

try (RTMClient rtm = new Slack().rtm(token)) {

  rtm.addMessageHandler((message) -> {
    JsonObject json = jsonParser.parse(message).getAsJsonObject();
    if (json.get("type") != null) {"Handled type: {}", json.get("type").getAsString());

  RTMMessageHandler handler2 = (message) -> {"Hello!");


  // must connect within 30 seconds after issuing wss endpoint




} // #close method does #disconnect

The message, argument of messageHandler, is a string value in JSON format. You need to deserialize it with your favorite JSON library.

jSlack already depends on google-gson library. So you can use Gson as above example shows. If you prefer Jackson or other libraries, it's also possible.

(jSlack's Original) Shortcut APIs

Slack slack = Slack.getInstance();
ApiToken token = ApiToken.of(System.getenv("SLACK_OAUTH_ACCESS_TOKEN"));

Shortcut shortcut = slack.shortcut(token);

List<Message> messages = shortcut.findRecentMessagesByName(ChannelName.of("general"));
ReactionsAddResponse addReaction = shortcut.addReaction(messages.get(0), ReactionName.of("smile"));

ChatPostMessageResponse response ="general"), "hello, hello!");

// Good old attachments
Attachment attachment = Attachment.builder().text("text").footer("footer").build();
List<Attachment> attachments = Arrays.asList(attachment);
ChatPostMessageResponse response = shortcut.postAsBot(ChannelName.of("general"), "hello, hello!");

// Block Kit
SectionBlock section = SectionBlock.builder()
  .text(MarkdownTextObject.builder().text("Some rich text").build())
  .accessory(ImageElement.builder().imageUrl("").altText("This is an image").build())

DividerBlock divider = new DividerBlock();

ChatPostMessageResponse response ="general"),
  Arrays.asList(section, divider));

Preparations for running this library's unit tests.

Creating a Slack app

Setting up "OAuth & Permissions" for it

You need to select all permission scopes except for identity.*. After that, you also need to run "Reinstall App".

Setting up OAuth access tokens (both the normal one and the one as a bot)

Set the OAuth access tokens as env variables.

export SLACK_TEST_OAUTH_ACCESS_TOKEN=xoxp-*******************************************************
export SLACK_BOT_USER_TEST_OAUTH_ACCESS_TOKEN=xoxb-************************************

Creating at least one Slack user who has its email

Manually create a Slack user which has an email address for a unit test.

Create an incoming webhook url

export SLACK_WEBHOOK_TEST_CHANNEL=C12345678 (or #random)

Add to App Unfurl Domains

  • Features > Event Subscriptions > App Unfurl Domains
    • Add to the domain list
    • Re-install your Slack app

(Optional) Have a shared channel


(Optional) Have a multi-channel guest and a single-channel guest

Run the tests

mvn test


mvn deploy -P release-sign-artifacts -D maven.test.skip=true


(The MIT License)

Copyright (c) Kazuhiro Sera

Stargazers over time

Stargazers over time

You can’t perform that action at this time.