Skip to content

Commit abec2aa

Browse files
zimegClaude
andauthored
feat(methods): add chat.postMessage example (#48)
Co-authored-by: Claude <svc-devxp-claude@slack-corp.com>
1 parent 5455176 commit abec2aa

7 files changed

Lines changed: 156 additions & 0 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
matrix:
1313
showcase:
1414
- "block-kit"
15+
- "methods"
1516
steps:
1617
- name: Checkout code
1718
uses: actions/checkout@v7

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ This collections of examples highlights features of a Slack app in the language
55
## Available demonstration
66

77
- **[Block Kit](./block-kit)**: The framework of visual components arranged to create app layouts.
8+
- **[Methods](./methods)**: An interface for querying information from and enacting change in a Slack workspace.

‎methods/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.classpath
2+
.project
3+
.settings
4+
target

‎methods/README.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Methods
2+
3+
An interface for querying information from and enacting change in a Slack workspace.
4+
5+
Read the [docs](https://docs.slack.dev/apis/web-api/) for explanations of concepts, or explore [reference](https://docs.slack.dev/reference/methods) pages for specific functionalities.
6+
7+
## Making a request
8+
9+
```sh
10+
$ cd methods # Navigate to the project root
11+
$ slack app settings # Create an app
12+
$ vim src/main/java/chat/ChatPostMessage.java # Edit arguments
13+
$ export SLACK_TOKEN=xoxb-example # Set if unchanged
14+
$ mvn compile exec:java -Dexec.mainClass=chat.ChatPostMessage # Make the request
15+
```
16+
17+
## What's on call
18+
19+
### chat
20+
21+
- **[chat.postMessage](https://docs.slack.dev/reference/methods/chat.postmessage)**: Sends a message to a channel. [Implementation](./src/main/java/chat/ChatPostMessage.java).
22+

‎methods/pom.xml‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.slack.api</groupId>
6+
<artifactId>methods-examples</artifactId>
7+
<version>0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
</properties>
12+
<build>
13+
<plugins>
14+
<plugin>
15+
<groupId>org.apache.maven.plugins</groupId>
16+
<artifactId>maven-compiler-plugin</artifactId>
17+
<version>3.15.0</version>
18+
<configuration>
19+
<release>17</release>
20+
</configuration>
21+
</plugin>
22+
<plugin>
23+
<groupId>org.codehaus.mojo</groupId>
24+
<artifactId>exec-maven-plugin</artifactId>
25+
<version>3.5.1</version>
26+
<configuration>
27+
<cleanupDaemonThreads>false</cleanupDaemonThreads>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>com.diffplug.spotless</groupId>
32+
<artifactId>spotless-maven-plugin</artifactId>
33+
<version>3.7.0</version>
34+
<configuration>
35+
<formats>
36+
<format>
37+
<includes>
38+
<include>.gitignore</include>
39+
</includes>
40+
<trimTrailingWhitespace/>
41+
<endWithNewline/>
42+
<indent>
43+
<tabs>true</tabs>
44+
<spacesPerTab>4</spacesPerTab>
45+
</indent>
46+
</format>
47+
</formats>
48+
<java>
49+
<palantirJavaFormat/>
50+
</java>
51+
<pom/>
52+
<markdown>
53+
<includes>
54+
<include>**/*.md</include>
55+
</includes>
56+
<flexmark/>
57+
</markdown>
58+
</configuration>
59+
<executions>
60+
<execution>
61+
<goals>
62+
<goal>apply</goal>
63+
</goals>
64+
<phase>compile</phase>
65+
</execution>
66+
</executions>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
<dependencies>
71+
<dependency>
72+
<groupId>com.slack.api</groupId>
73+
<artifactId>slack-api-client</artifactId>
74+
<version>1.49.0</version>
75+
</dependency>
76+
</dependencies>
77+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package chat;
2+
3+
import com.slack.api.Slack;
4+
import com.slack.api.methods.MethodsClient;
5+
import com.slack.api.methods.SlackApiException;
6+
import com.slack.api.methods.request.chat.ChatPostMessageRequest;
7+
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
8+
import java.io.IOException;
9+
10+
public class ChatPostMessage {
11+
12+
public static void main(String[] args) throws IOException, SlackApiException {
13+
// Read a token from an environment variable
14+
String token = System.getenv("SLACK_TOKEN");
15+
16+
// Initialize
17+
MethodsClient methods = Slack.getInstance().methods(token);
18+
19+
// Call the chat.postMessage method
20+
ChatPostMessageRequest request = ChatPostMessageRequest.builder()
21+
.channel("C123ABC456")
22+
.text("Here's a message for you")
23+
.build();
24+
ChatPostMessageResponse response = methods.chatPostMessage(request);
25+
26+
// Inspect the response
27+
System.out.println(response);
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"display_information": {
3+
"name": "Slack API Methods",
4+
"description": "Example implementations to call \"chat\" methods"
5+
},
6+
"features": {
7+
"bot_user": {
8+
"display_name": "Slack API Methods",
9+
"always_online": true
10+
}
11+
},
12+
"oauth_config": {
13+
"scopes": {
14+
"bot": ["chat:write"]
15+
}
16+
},
17+
"settings": {
18+
"org_deploy_enabled": true,
19+
"socket_mode_enabled": false,
20+
"token_rotation_enabled": false
21+
}
22+
}

0 commit comments

Comments
 (0)