[카카오워크] API 리액티브(reactive) 메세지 보내기
여기서 말하는 반응은 사용자와의 반응을 말합니다.
리액티브(반응형) 하려면 콜백 승인이 나야 하는데 안되어 아직 테스트 못했습니다. 다만 그와 관련하여 필요한 부분을 담았습니다. 이 부분만 curl 예제가 약간 부족하여 읽는 시간이 들어 처음 보시는분 도움 될까 싶어 올립니다.
# ----------------------------------------------------------------------------
# [카카오워크] API 리액티브(reactive) / 메세지 보내기 - 이메일 이용 브라우저 띄우는 버튼 클릭용
# ----------------------------------------------------------------------------
# curl -X POST https://api.kakaowork.com/v1/messages.send_by_email -H "Authorization: Bearer {YOUR_APP_KEY}" -H "Content-Type: application/json" -d '{ "email": "{메시지를 수신할 사용자의 인증된 email 주소}", "text": "{전송할 채팅 메시지}" }'
#
# ./api_test.php send_by_emai-button
if( $MODE == 'send_by_emai-button' )
{
$KWORK_API = '01c281';
$USER_AGENT = 'pabburi.co.kr kakaowork bot';
$json_data = '
{
"email": "pa@gmail.com",
"text": "Push alarm message",
"blocks": [
{
"type": "header",
"text": "알림장보기",
"style": "blue"
},
{
"type": "text",
"text": "sample",
"inlines": [
{
"type": "styled",
"text": "링크 ",
"bold": true,
"color": "red"
},
{
"type": "link",
"text": "이 링크 클릭하면 설명 페이지 이동",
"url": "https://docs.kakaoi.ai/kakao_work/blockkit/textblock/"
}
]
},
{
"type": "button",
"text": "알림장 확인",
"style": "default",
"action_type": "open_system_browser",
"action_name": "test1 - action_name",
"value": "https://www.pabburi.co.kr"
}
]
}
';
$aHeader = array();
$aHeader[] = 'Authorization: Bearer ' . $KWORK_API;
$aHeader[] = 'Content-Type: application/json; charset=utf-8';
$aHeader[] = "Accept-Encoding: gzip, deflate";
$aHeader[] = "Connection: keep-alive";
$aHeader[] = "User-Agent: " . $USER_AGENT;
// print_r($aHeader);
#
$url_kwork = 'https://api.kakaowork.com/v1/messages.send_by_email';
# 리턴되는 헤더에 꼭 필요한 정보가 있어 리스폰 헤더를 알아야 한다.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_kwork);
curl_setopt($ch, CURLOPT_HTTPHEADER , $aHeader);
curl_setopt($ch, CURL_HTTP_VERSION_1_1, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 5);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
$aRtnHeader = curl_getinfo($ch);
curl_close($ch);
# 리스폰에서 헤더와 바디 별도 변수에 담아 놓기 - 압축되었으면 풀어 준다.
$rtn_header = substr($response, 0, $aRtnHeader['header_size']);
$rtn_body = substr($response, $aRtnHeader['header_size']);
$http_body = $rtn_body;
if( stristr($rtn_header, 'Content-Encoding: gzip') ) {
$http_body = gzdecode($rtn_body);
}
$aJson = json_decode($http_body, true);
print_r($aJson);
}
