[카카오워크] API 채팅방 만들기
채팅방을 만들려면 유저의 ID를 알아야 합니다. 이전글에서 userlist를 통해서도 유저의 id를 알 수 있습니다.
그리고 메일주소를 통해서 직접 메시지 보내는 방법은 채팅방을 만들고 보내는것이 아니라 즉시 보낼 수 있기 때문에 이 과정은 없어도 됩니다.
# ----------------------------------------------------------------------------
# [카카오워크] API 채팅방 만들기 / 채팅방 하나 만들기
# ----------------------------------------------------------------------------
# curl -X POST https://api.kakaowork.com/v1/conversations.open -H "Authorization: Bearer {YOUR_APP_KEY}" -H "Content-Type: application/json" -d '{ "user_id": {채팅 상대방의 사용자 ID} }'
#
# ./api_test.php conversations
if( $MODE == 'conversations' )
{
$KWORK_API = '01c281';
$USER_AGENT = 'pabburi.co.kr kakaowork bot';
$aSendInfo = array();
$aSendInfo['user_id'] = 123;
$json_data = json_encode($aSendInfo);
$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/conversations.open';
# 리턴되는 헤더에 꼭 필요한 정보가 있어 리스폰 헤더를 알아야 한다.
$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);
// echo $rtn_header;
print_r($aJson);
echo '-----------------------------------------------' . "\n";
$aRtnInfo = array();
$aTemp = explode("\n", $rtn_header);
foreach ( $aTemp as $key => $val )
{
$aTmp = explode(': ', $val);
$hname = $aTmp[0];
$hval = $aTmp[1];
$aRtnInfo[$hname] = trim($hval);
}
echo 'ratelimit-limit: ' . $aRtnInfo['ratelimit-limit'] . "\n";
echo 'ratelimit-remaining: ' . $aRtnInfo['ratelimit-remaining'] . "\n";
echo 'ratelimit-reset: ' . $aRtnInfo['ratelimit-reset'] . "\n";
}