Java Spring + Slack Web Hook API
슬랙으로 메시지를 보내기
1. 슬랙에서 채널을 생성
2. 원하는 채널을 선택하고, 접근API를 생성
- 메뉴 : https://my.slack.com/services/new/incoming-webhook/
- 설명 : api.slack.com -> incoming webhooks
3. 연동(Java) 모듈 생성
String hooksSlack = "https://hooks.slack.com/services/xx/yy/zz"; CloseableHttpClient client = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(hooksSlack); String message = " " ; String json = "{ \"text\": \""+ message.toString() + "\" }"; StringEntity entity = new StringEntity(json); httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json");
CloseableHttpResponse response = client.execute(httpPost); log.error("getStatusCode : " + response.getStatusLine().getStatusCode() ); //assertThat( response.getStatusLine().getStatusCode(), equalTo(200) ); client.close(); |
- 실행 결과 (샘플)
# Spring Slack Integration
http://wooriworld2006.tistory.com/382
# POST with JSON
http://www.baeldung.com/httpclient-post-http-request
# Webhook URL
'Web Tech. > Spring Framework' 카테고리의 다른 글
thread-safe하도록 servlet작성하기 (0) | 2018.07.20 |
---|---|
Tomcat Memory Leak (0) | 2018.07.18 |
페이스북 Graph API 사용하기 (0) | 2017.09.26 |
Bash , Kill $pids (0) | 2017.09.11 |
AWS S3 파일 업로드 연동하기 ( using AWS SDK for Java ) (2) | 2017.08.02 |