これまでは、作ったWebサイトからアクセストークンを入れて、アイボさんのデバイス情報などを取得してみましたが、Webサイトにアクセスは面倒だし、常に確認したいし・・・なので、いつも使うアプリだとすればLINEかな? ということで LINEボットのテストサイトを作ってみました。まだまだテストなので、前回までのスタートとして、アクセストークンを送信すると、保有のアイボさんのデバイス情報を返してくれる簡単なものをですが、こんな感じになりました。

■LINE側の aiboボットサンプル
<?php
$accessToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
//ユーザーからのメッセージ取得
$json_string = file_get_contents('php://input');
$json_object = json_decode($json_string);
//メッセージタイプが「text」以外のときは何も返さず終了
if($message_type != "text") exit;
//aiboアクセストークンからデバイスID取得
$access_token = $message_text;
$aibo_device = "";
$aiboDevices = aiboApiGetDevices($access_token);
$devices = $aiboDevices["devices"];
foreach( $devices as $key => $value ){
$aibo_device .= "ニックネーム:". $value["nickname"] ."\n";
$aibo_device .= "デバイスID :". $value["deviceId"] ."\n\n";
}
//返信メッセージ
$return_message_text = "【aiboボット テストサイトだよ!】\n\n";
$return_message_text .= "保有しているaiboさんは、これだよ\n\n";
$return_message_text .= $aibo_device ;
//返信実行
sending_messages($accessToken, $replyToken, $message_type, $return_message_text);
//-----------------------------------------------------------------------------------
//メッセージの送信
//-----------------------------------------------------------------------------------
function sending_messages($accessToken, $replyToken, $message_type, $return_message_text){
//レスポンスフォーマット
$response_format_text = [
"type" => $message_type,
"text" => $return_message_text
];
//ポストデータ
$post_data = [
"replyToken" => $replyToken,
"messages" => [$response_format_text]
];
//curl実行
$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);
}
//-----------------------------------------------------------------------------------
// アクセストークンからデバイスIDを取得
// aiboApiGetDevices($webApiURL,$accessToken)
//-----------------------------------------------------------------------------------
function aiboApiGetDevices($accessToken){
$authorization = "Authorization:Bearer ". $accessToken;
$headers = array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
$authorization,
);
$webApiURL = "https://public.api.aibo.com/v1/devices";
$webApi = curl_init();
curl_setopt($webApi,CURLOPT_URL,$webApiURL );
curl_setopt($webApi,CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt($webApi,CURLOPT_RETURNTRANSFER, true );
curl_setopt($webApi,CURLOPT_HTTPHEADER, $headers );
$json = curl_exec($webApi);
$response = json_decode($json,true);
$err = curl_error($webApi);
curl_close($webApi);
return $response;
}
?>
One Response on “【LINEでaiboボット】を考えてみようカナ?”