forked from qus0in/slack_incoming_hook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.java
More file actions
33 lines (30 loc) · 1.23 KB
/
Bot.java
File metadata and controls
33 lines (30 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.net.*;
import java.net.http.*;
import java.time.*;
import java.util.*;
public class Bot {
public static void main(String[] args) { // 진입부분
// 이게 있어야 이 클래스를 실행했을 때 작동을 함
// 웹훅을 만들 거임 -> URL 필요함
// 환경변수로 받아올 것임 -> yml 파일에서 전달하게
String webhookUrl = System.getenv("SLACK_WEBHOOK_URL");
String message = System.getenv("SLACK_WEBHOOK_MSG");
// Java 11 -> fetch
HttpClient client = HttpClient.newHttpClient();
// 요청을 얹힐 거다
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(webhookUrl))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"text\":\" + " + message + "\"}"))
.build();
try {
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString()
);
System.out.println("요청 코드: " + response.statusCode());
System.out.println("응답 결과: " + response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}