amazing...
Slack SDK for Javaã¨ã¯ï¼
Slack SDK for Java supports the Slack platform in a Java idiomatic way. The SDK written in Java so developers can use it in any JVM language including Kotlin, Groovy, and Scala.
⧠Slack platformããµãã¼ãã£ã¦è¨ããã¦ãã...
Within the SDK, there are two different modules:
- Bolt for Java, which is a framework with a simple API that makes it easy to write modern Slack apps in Java.
- Slack API Client, for when you need a more customized approach to building a Slack app in Java.
⧠2種é¡ãããããã
If what you want to do is call Slack APIs in your existing services, we recommend using only the Slack API Client. If instead, youâre developing a new modern and interactive Slack app, we recommend Bolt for it. The framework enables developers to focus on the essential parts of their apps without being bothered by trifles.
⧠ã¨ãããã¨ã§ãåç´ã«Slack APIãå¼ã³åºãã ããªãã°ããSlack API Clientãã使ãã®ãæ¨å¥¨ãããã
ãSlack API Clientãã使ãã«ã¯
Javaã§ãSlack API Clientãã使ãã«ã¯ã
⧠Java 8以ä¸ãå¿ è¦ã
ããã«ã
To call a Web API method such as chat.postMessage, a MethodsClient instance needs to be initialized with a token. A token usually begins with xoxb-
 (bot token) or xoxp-
 (user token). You get them from each workspace that an app has been installed. The Slack App configuration pages help you get your first token for your development workspace.
⧠tokenã£ã¦ãã®ãå¿ è¦ããããã ãã©ã
⧠ä¸è¨ã®èª¬æã«ããã¨ã
- Bot tokens
- User tokens
- App-level tokens
ã®3種é¡ããã£ã½ããã ãã©ãã©ãã使ãã°ããã®ãããã¾ãã¡å¤ç¶ã¨ããªãã
⧠ä¸è¨ãµã¤ãæ§ãåèã«å©ç¨ãã¦ã¿ãã
â ã¢ããªã®ä½æ
Slackã®è©²å½ããã¯ã¼ã¯ã¹ãã¼ã¹ã«ãã°ã¤ã³å¾ããã©ã¦ã¶ã§ä»¥ä¸ã®ãã¼ã¸ã«ã¢ã¯ã»ã¹ã
é©å½ãªãApp Nameãã決ãã¦ãSlackã®ã¯ã¼ã¯ã¹ãã¼ã¹ãé¸æãã¦ããCreate Appããã¿ã³ãæ¼ä¸ã
â Scopeã®è¨å®
å¼ãç¶ãããOAuth & Permissionsããé¸æã
ãScopesãã§ãBot Token Scopesãã£ã¦æ¹ãããAdd an OAuth Scopeããæ¼ä¸ã
ãchat:writeããé¸æã
ãUser Token Scopesãã®æ¹ãåæ§ã«é¸æãã¦è¿½å ããã
â ã¢ããªãã¯ã¼ã¯ã¹ãã¼ã¹ã«ã¤ã³ã¹ãã¼ã«
ãInstall to Workspaceããæ¼ä¸ã
ã許å¯ããããæ¼ä¸ã
tokenãä½æããã¾ããã
â ãã£ã³ãã«ã«ã¢ããªã追å
Slackã®UIãå¤ãã£ãã®ãåããããã©ãä½æãã¦ãã¢ããªãé¸æã§ããããã«ãªã£ã¦ãã®ã§ãé¸æãããã«ãã¦ã³ãã¯ãªãã¯ã
ããã£ã³ãã«ã«ãã®ã¢ããªã追å ããããé¸æã
ã¡ãã»ã¼ã¸ãéç¥ããããã£ã³ãã«ãé¸æã
ã追å ããæ¼ä¸ã
ãã£ã³ãã«å´ã§è¿½å ãããã¡ãã»ã¼ã¸ã表示ããã¦ãã°OKã
Â
â Javaããã¡ãã»ã¼ã¸ãéã£ã¦ã¿ã
ã¨ãããããEclipseã§ãSpring ã¹ã¿ã¼ã¿ã¼ã»ããã¸ã§ã¯ãããä½æã
ããã¸ã§ã¯ããä½æã§ãããã°ãbuild.gradleã«ãSlack APIããå©ç¨ããããã®ä¾åé¢ä¿ã追å ã
â slack-api/build.gradle
plugins { id 'org.springframework.boot' version '2.7.4' id 'io.spring.dependency-management' version '1.0.14.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { //implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' implementation("com.slack.api:slack-api-client:1.25.1") testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
ãã¡ã¤ã«ã®è¿½å ã¨ãã¡ã¤ã«ã¸è¿½è¨ã
application.propertiesã¸ã¯ãtokenãè¨å®ã
â slack-api/src/main/java/com/example/demo/service/SlackServiceImpl.java
package com.example.demo.service; import java.io.IOException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; 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 lombok.extern.log4j.Log4j2; @Log4j2 @Service public class SlackServiceImpl { @Value("${slack.token.user}") private String slackApiTokenUser; @Value("${slack.token.bot}") private String slackApiTokenBot; public void sendMessageByBot() { Slack slack = Slack.getInstance(); // Initialize an API Methods client with the given token MethodsClient methods = slack.methods(slackApiTokenBot); // Build a request object ChatPostMessageRequest request = ChatPostMessageRequest.builder() .channel("#slack-api-test") // Use a channel ID .text(":wave: Hi from a bot written in Java!") .build(); // Get a response as a Java object try { ChatPostMessageResponse response = methods.chatPostMessage(request); log.info(response); } catch (IOException | SlackApiException e) { // TODO èªåçæããã catch ããã㯠e.printStackTrace(); } } public void sendMessageByUser() { Slack slack = Slack.getInstance(); // Initialize an API Methods client with the given token MethodsClient methods = slack.methods(slackApiTokenUser); // Build a request object ChatPostMessageRequest request = ChatPostMessageRequest.builder() .channel("#slack-api-test") // Use a channel ID .text(":wave: Hi from a user written in Java!") .build(); // Get a response as a Java object try { ChatPostMessageResponse response = methods.chatPostMessage(request); log.info(response); } catch (IOException | SlackApiException e) { // TODO èªåçæããã catch ããã㯠e.printStackTrace(); } } }
â slack-api/src/main/java/com/example/demo/controller/SlackController.java
package com.example.demo.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import com.example.demo.service.SlackServiceImpl; @RestController public class SlackController { @Autowired private SlackServiceImpl slackServiceImpl; @GetMapping(value="/message/bot") public void sendMessageByBot() { slackServiceImpl.sendMessageByBot(); } @GetMapping(value="/message/user") public void sendMessageByUser() { slackServiceImpl.sendMessageByUser(); } }
⧠ã§ãä¿åãããå®è¡ãã
ãã©ã¦ã¶ã§ã³ã³ããã¼ã©ã¼ã¯ã©ã¹ã®ã¨ã³ããã¤ã³ãã«ã¢ã¯ã»ã¹ããã¨ã
Slackå´ã«éç¥ãé£ã³ã¾ããã
åèãµã¤ãæ§ã®ããã«ãcurlã³ãã³ãã§æ¥æ¬èªã®ããã¹ããSlackã«éç¥ããã¨ãSlackå´ã§å¤ãªé¢¨ã«å¤æããã¦ãã¾ã£ããã©ã...
æ¯åº¦ã¢ã¤ã¢ã¤æãå端ãªã...
ä»åã¯ãã®ã¸ãã§ã
Â
Â