노출되는 이미지가 불편하시겠지만 양해를 구합니다. 노출, 클릭등에 관한 자료로 활용 중입니다.

부제 : , Java + Slack Web Hook API

 

# 채널 설정

1. 슬랙(slack.com)에서 Workspace에 로그인(또는 신규 생성)후, 채널(=그룹채팅)을 생성

 

2. 아래 메뉴에서,

1) 원하는 채널을 선택(또는 신규생성)하고,

2) 접근API를 생성(Add Incoming WebHooks Integration)

3) 생성된 Webhook URL을 복사/저장

 

- 메뉴 : https://my.slack.com/services/new/incoming-webhook/ , [MyWorkspace].slack.com으로 연결

- 설명 : api.slack.com -> incoming webhooks

 

3. 메시지 전송이 가능해지면, 채널로 초대하기(Invite People)

# 파라미터 옵션들

[POST]

2 Options

1.send a JSON string as the payload parameter in a POST request

2.send a JSON string as the body of a POST request

Sending Messages

payload={"text": "This is a line of text in a channel.\nAnd this is another line of text."}

Adding links

payload={"text": "{A very important thing has occurred! <https://alert-system.com/alerts/1234|Click here> for details!"}

Customized Appearance

"username":"new-bot-name"

"icon_url": "https://slack.com/img/icons/app-57.png"

"icon_emoji": ":ghost"

Channel Override

선택된 기본 채널을, JSON 에서 변경 가능하다. "channel" : "#other-channel"

그리고, 직접메시지는 "channel": "@username"

Example

curl -X POST --data-urlencode "payload={\"channel\": \"#dev~~channel\", \"username\": \"webhookbot\", \"text\": \"This is posted to #devpostchannel and comes from a bot named webhookbot.\", \"icon_emoji\": \":ghost:\"}" https://hooks.slack.com/services/T1~~~~~~F6495

 

# 구현 예제

1) 연동(Java Native) 모듈 생성

https://github.com/sketchout/ChannelNotifySlack

 

2) 연동(Java Spring) 모듈 생성

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 : https://stackoverflow.com/questions/37423129/slack-incoming-web-hook-and-getting-it-to-work-with-httppost

블로그 이미지

StartGuide

I want to share the basic to programming of each category and how to solve the error. This basic instruction can be extended further. And I have been worked in southeast Asia more than 3 years. And I want to have the chance to work another country.

,