Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
matrix:
showcase:
- "block-kit"
- "methods"
steps:
- name: Checkout code
uses: actions/checkout@v7
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ This collections of examples highlights features of a Slack app in the language
## Available demonstration

- **[Block Kit](./block-kit)**: The framework of visual components arranged to create app layouts.
- **[Methods](./methods)**: Individual Slack Web API method calls with the `slack-api-client`.
4 changes: 4 additions & 0 deletions methods/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.classpath
.project
.settings
target
21 changes: 21 additions & 0 deletions methods/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Methods

Individual Slack Web API method calls with the `slack-api-client`.

Read the [docs](https://docs.slack.dev/reference/methods) to explore every method, or explore implementations of specific families.

## What's on display

### chat

- **[chat.postMessage](https://docs.slack.dev/reference/methods/chat.postmessage)**: Sends a message to a channel. [Implementation](./src/main/java/chat/ChatPostMessage.java). Scopes: `chat:write`.

## Running an example

Set a bot token and run an example class directly:

```sh
export SLACK_TOKEN="xoxb-your-token"
mvn compile exec:java -Dexec.mainClass=chat.ChatPostMessage
```

81 changes: 81 additions & 0 deletions methods/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.slack.api</groupId>
<artifactId>methods-examples</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.15.0</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<formats>
<format>
<includes>
<include>.gitignore</include>
</includes>
<trimTrailingWhitespace/>
<endWithNewline/>
<indent>
<tabs>true</tabs>
<spacesPerTab>4</spacesPerTab>
</indent>
</format>
</formats>
<java>
<palantirJavaFormat/>
</java>
<pom/>
<markdown>
<includes>
<include>**/*.md</include>
</includes>
<flexmark/>
</markdown>
</configuration>
<executions>
<execution>
<goals>
<goal>apply</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>slack-api-client</artifactId>
<version>1.49.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>6.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>mockwebserver</artifactId>
<version>4.12.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
38 changes: 38 additions & 0 deletions methods/src/main/java/chat/ChatPostMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package chat;

import com.slack.api.Slack;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import java.io.IOException;

/**
* Sends a message to a channel with the chat.postMessage method.
* {@link https://docs.slack.dev/reference/methods/chat.postmessage}
*/
public class ChatPostMessage {

public static ChatPostMessageResponse example01(Slack slack) throws IOException, SlackApiException {
// Read a token from the environment variables
String token = System.getenv("SLACK_TOKEN");

// Initialize an API Methods client with the given token
MethodsClient methods = slack.methods(token);

// Build a request object
ChatPostMessageRequest request = ChatPostMessageRequest.builder()
.channel("C123ABC456")
.text("Here's a message for you")
.build();

// Get a response as a Java object
ChatPostMessageResponse response = methods.chatPostMessage(request);

return response;
}

public static void main(String[] args) throws IOException, SlackApiException {
System.out.println(example01(Slack.getInstance()));
}
}
51 changes: 51 additions & 0 deletions methods/src/test/java/chat/ChatPostMessageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package chat;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.slack.api.Slack;
import com.slack.api.SlackConfig;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class ChatPostMessageTest {

MockWebServer server;
Slack slack;

@BeforeEach
public void setup() throws Exception {
server = new MockWebServer();
server.start();
SlackConfig config = new SlackConfig();
config.setMethodsEndpointUrlPrefix(server.url("/api/").toString());
slack = Slack.getInstance(config);
}

@AfterEach
public void tearDown() throws Exception {
server.shutdown();
}

@Test
public void sends() throws Exception {
server.enqueue(new MockResponse().setBody("{\"ok\":true}"));

ChatPostMessageResponse response = ChatPostMessage.example01(slack);
assertTrue(response.isOk());

RecordedRequest request = server.takeRequest();
assertEquals("/api/chat.postMessage", request.getPath());

String body = URLDecoder.decode(request.getBody().readUtf8(), StandardCharsets.UTF_8);
assertTrue(body.contains("channel=C123ABC456"));
assertTrue(body.contains("text=Here's a message for you"));
}
}
Loading