크롤링 하는 슬랙봇을 만들고 있었고 크롤링 코드를 파이썬으로 짰기 때문에 당연히 파이썬으로 슬랙에 연동하고 요청 날려야겠다고 생각하고 공부했다. 그런데 크롤링 결과를 DB에 저장하고 CDC로 새로운 글을 감지해야겠다고 정리하니까 DB에서 데이터를 가져오는 부분은 스프링으로 해도 될 것 같았다. 그래서 크롤링해서 DB에 저장하는 부분만 파이썬으로 짜보고 나머지는 스프링으로 하려고 슬랙봇을 다시 연동했다.
1. 슬랙봇 연동하기
먼저 스프링 프로젝트를 만들고 슬랙봇 의존성을 주입해야 한다.
https://slack.dev/java-slack-sdk/guides/getting-started-with-bolt#gradle
Getting Started with Bolt | Slack SDK for Java
SLACK_SIGNING_SECRET The secret value shared only with the Slack Platform. It is used for verifying incoming requests from Slack. Request verification is crucial for security as Slack apps have internet-facing endpoints. To know the value, visit the Slack
slack.dev
여기에 있는 코드를 build.gradle 파일의 dependencies 부분에 작성한다.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.projectlombok:lombok:1.18.22'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation("com.slack.api:bolt:1.38.2")
implementation("com.slack.api:bolt-servlet:1.38.2")
implementation("com.slack.api:bolt-jetty:1.38.2")
implementation("org.slf4j:slf4j-simple:1.7.36")
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
그리고 application.properties 파일에 봇의 토큰을 복붙한다.
나중에 @Value로 가져와서 사용하면 되니까 변수명은 자유롭게 하면 된다.
2. 메시지
일단 테스트용으로 슬랙봇과 잘 연결됐는지 Service 클래스를 만들어서 간단한 메시지를 하나 입력할 거다.
ChatPostMessageRequest로 메시지를 날리는 코드는
https://slack.dev/java-slack-sdk/guides/web-api-basics#call-a-method
여기에 자세히 나와있으니 참고하면 된다.
API Client Basics | Slack SDK for Java
API Client Basics slack-api-client contains simple, easy-to-use, and flexibly configurable HTTP clients for making requests to Slack APIs. Before trying the samples on this page, you need to set up your Java project first. If you haven’t done it yet, che
slack.dev
package com.example.SWSlackBot;
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class BotService {
@Value(value = "${slack.ssuSWtoken}") # 슬랙봇 토큰 가져오기
String ssuSwBotToken;
String channelAddress = "#test"; # 슬랙봇이 메시지를 보낼 채널
public void sendMessage(String message){
try{
MethodsClient methodsClient = Slack.getInstance().methods(ssuSwBotToken);
ChatPostMessageRequest request = ChatPostMessageRequest.builder()
.channel(channelAddress)
.text(message)
.build();
ChatPostMessageResponse response = methodsClient.chatPostMessage(request);
}catch (SlackApiException | IOException e) {
throw new RuntimeException(e);
}
}
}
BotController 클래스를 만들어서 요청을 하나 날려봤다.
@Controller
public class BotController {
@Autowired
BotService botService;
@GetMapping("/slack/test")
@ResponseBody
public void test(){
botService.sendMessage("Hi");
}
}
localhost:8080/slack/test 사이트에 접속하면
메시지가 전송된다!!
'심심해서' 카테고리의 다른 글
파이썬으로 슬랙 봇 만들기 (2) - Beautifulsoup4로 크롤링하기 (0) | 2024.03.04 |
---|---|
파이썬으로 슬랙 봇 만들기 (1) - Hello World! 전송 (0) | 2024.03.03 |