PHP와 simple api로 가장 가까운 역 검색 LINEbot 만들기
14310 단어 LINEmessagingAPIPHPlinebot
소개
LINE MessagingAPI를 사용하여 가장 가까운 역을 검색할 수 있는 LINEbot을 만들었으므로 만드는 방법 등을 써 봅니다. (첫 포스트입니다)
LINE Developers에 등록
먼저 LINE Developers의 사이트에 가서 자신의 LINE 계정으로 로그인합니다.

로그인하면 공급자 작성 화면으로 이동하므로 임의의 이름으로 제공자를 작성하십시오.
공급자를 만든 후 MessagingAPI를 선택합니다.

여기서 LINEbot 아이콘, 표시 이름 등을 입력합니다.
필요한 사항을 입력하면 채널 생성이 완료됩니다.
채널을 만들 수 있으면 MessagingAPI 설정으로 이동하여
- Webhook의 이용・・・한다
- 그룹/여러 사람 채팅에 참여할 수 있도록 허용
- 응답 메시지 ... 무효
- 인사말...무효
로 설정합니다.
그리고, QR코드로부터 먼저 bot과 친구가 되어 두어도 좋다고 생각합니다.
PHP 프로그램 작성
여기에서 PHP 프로그램을 작성합니다.
location.php
<?php
$accessToken = 'YOUR_ACCESS_TOKEN';
$return_message_text_a="";
//ユーザーからのメッセージ取得
$json_string = file_get_contents('php://input');
$json_object = json_decode($json_string);
//取得データ
$replyToken = $json_object->{"events"}[0]->{"replyToken"}; //返信用トークン
$message_type = $json_object->{"events"}[0]->{"message"}->{"type"}; //メッセージタイプ
$message_text = $json_object->{"events"}[0]->{"message"}->{"text"}; //メッセージ内容
$latitude = $json_object->{"events"}[0]->{"message"}->{"latitude"};
$longitude = $json_object->{"events"}[0]->{"message"}->{"longitude"};
if($message_type=="location"){
function sending_locationmessages($accessToken, $replyToken, $message_type, $return_message_text_a){
//レスポンスフォーマット
$response_format_text = [
"type" => 'text',
"text" => $return_message_text_a
];
//ポストデータ
$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);
}
$xml = simplexml_load_file('http://map.simpleapi.net/stationapi?x='.$longitude.'&y='.$latitude.'&output=json');
$json = json_decode(file_get_contents('http://map.simpleapi.net/stationapi?x='.$longitude.'&y='.$latitude.'&output=json'), TRUE);
$return_message_text_a = $json[0]["name"]."です\n"."\n地図はこちら https://www.google.co.jp/maps/search/".$json[0]["name"]."\n";
sending_locationmessages($accessToken, $replyToken, $message_type, $return_message_text_a);
}
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);
}
첫
$accessToken = 'YOUR_ACCESS_TOKEN';
곳은 MessagingAPI 설정 페이지에서 발행해 온 자신의 액세스 토큰을 붙여넣습니다.이번에는 simpleapi을 이용하여 가까운 역 정보를 취득하고 있습니다.
코드를 작성한 후 PHP가 실행되는 서버에 배포합니다.
동작 화면
실제로 어떻게 움직이는지 보여드리겠습니다.
위치 정보를 LINE 토크에서 보내면

이와 같이 위치 정보 안에 포함된 위도, 경도 좌표에서 가장 가까운 역을 찾아 보내 줍니다.
요약
상당히 간단하게 LINEbot은 만들 수있었습니다!
이번에는 이 bot의 pytohn판을 만들고 싶습니다.
Reference
이 문제에 관하여(PHP와 simple api로 가장 가까운 역 검색 LINEbot 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/takuan517/items/49a1b65554afc89fb79f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)