https://api.slack.com/ 에서 Slack Bot 만들기
Review Scopes to Add 후, 원하는 Scope들을 선택!
Bot Token Scopes
에 chat:write
만 설정
https://slack.dev/java-slack-sdk/guides/getting-started-with-bolt#gradle 를 참고하여
Gradle 환경 세팅
dependencies {
implementation("com.slack.api:bolt:1.18.0")
implementation("com.slack.api:bolt-servlet:1.18.0")
implementation("com.slack.api:bolt-jetty:1.18.0")
}
slack:
token: 'xoxb-발급받은 토큰'
channel: '#채널명'
import org.springframework.beans.factory.annotation.Value;
@Value(value = "${slack.token}")
String token;
@Value(value = "${slack.channel}")
String channel;
private void sendSlackMessage(String username, Boolean activate) {
String message = (activate)? "> :writing_hand: *["+username+"]* 님이 업무 집중을 시작했어요!"
: "> :raised_hands: *["+username+"]* 님이 쉬고 있어요!";
try{
MethodsClient methods = Slack.getInstance().methods(token);
ChatPostMessageRequest request = ChatPostMessageRequest.builder()
.channel(channel)
.text(message)
.build();
methods.chatPostMessage(request);
}catch (Exception e){
throw new CustomException(ErrorCode.FAIL_SEND_SLACK);
}
}