LINEのMessageAPIを使ってみる
LINE
まあ、あれです。(^^)
実際、あまり友達いないので使うかどうかはわからないけど企業でもLINEのアカウントを使ってなんかしかやっているので使う機会があるかもしれないので勉強して見る。
開発用アカウント
Developer Trialを使用します。
LINE@とかでなんかしたほうがいいかもしれないけど今回はAPIだけなのでちょっとだけ
API
api.php
<?php // リクエスト取得 $receive = json_decode(file_get_contents("php://input")); // type $type = $receive->{"events"}[0]->{"message"}->{"type"}; // ReplyToken取得 $replyToken = $receive->{"events"}[0]->{"replyToken"}; # dump log ob_start(); var_dump($receive); var_dump($type); var_dump($replyToken); $data = ob_get_contents(); ob_end_clean(); file_put_contents("/tmp/aaa.txt", $data); # token $accessToken = 'アクセストークンを取得して設定する'; //返信データ作成 $response_format_text = array( "type" => "text", "text" => $type ); $post_data = array( "replyToken" => $replyToken, "messages" => [$response_format_text] ); $ch = curl_init("https://api.line.me/v2/bot/message/reply"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charser=UTF-8', 'Authorization: Bearer ' . $accessToken )); $result = curl_exec($ch); curl_close($ch);
※手抜き
※本来は署名処理が必要なんですけど設定していません。
結果