Total Access: 7791152

停電事故 発生中




LINE Messaging APIで電柱番号座標計算LINE botを作る

LINE Messaging API が解放されて個人でWebBOTの作成が可能になったのでその技術的なやり方について解説

LINE Messaging APIの準備

例えば,LINE Messaging API をとりあえず試してみた 等を参照.

注意点としては,

Callbackされるスクリプト

ここでは,実装がラクなのでPHPで,例として以下のような電柱番号(12桁)を送ったら座標を計算してレスポンスを返してくるようなBOTを作成する.

以下のソースをcallback.phpとしてアップロード.
パーミッションは特に何でも良くてデフォルトのままでいい.
<?php
require_once('./vendor/autoload.php');
$accessToken = 'LINE Developersで設定されたChannel Access Token';

$json_string = file_get_contents('php://input');
$jsonObj = json_decode($json_string);
$replyToken = $jsonObj->{"events"}[0]->{"replyToken"};


$type = $jsonObj->{"events"}[0]->{"message"}->{"type"};
if($type="text"){
	$query = $jsonObj->{"events"}[0]->{"message"}->{"text"};
	$query = mb_convert_kana($query, "a", "utf-8");
}

$query= str_replace('-', '', $query);
$query= str_replace('.', '', $query);
$query = preg_replace('/[^0-9]/', '', $query);

//■■■■■■■■■■■■ 電柱番号座標計算 ■■■■■■■■■■■■
if (strlen($query) == 12) {

		:
		:
		:

/////////////////////////////////////////////////////////////////////////////////
//ここで座標計算処理を行う(提供している電柱番号座標計算APIなどを利用できます)//
/////////////////////////////////////////////////////////////////////////////////

		:
		:
		:

	$response_format_text = array('type'      => 'location',
							'title'     => $query,
							'address'   => $address,
							'latitude'  => $laW,
							'longitude' => $lnW);
	$post_data = [
	"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);
}

//■■■■■■■■■■■■ 位置情報から電柱番号座標計算 ■■■■■■■■■■■■
if($type="location"){
	$lat = $jsonObj->{"events"}[0]->{"message"}->{"latitude"};
	$lon = $jsonObj->{"events"}[0]->{"message"}->{"longitude"};
	$address = $jsonObj->{"events"}[0]->{"message"}->{"address"};


		:
		:
		:

/////////////////////////////////////////////////////////////////////////////////
//ここで座標計算処理を行う(提供している電柱番号座標計算APIなどを利用できます)//
/////////////////////////////////////////////////////////////////////////////////

		:
		:
		:

	$DNO = strval($Yn0.$Xn0.$Yn1.$Xn1.$Yn2.$Xn2.$Yn3.$Xn3.$Yn4.$Xn4);
	if($DNO<0){
		exit();
	}

	$response = "その地点の電柱番号(概算)は ".$DNO." です";
	$response_format_text = [
	    "type" => "text",
	    "text" => $response
	    ];

	$post_data = [
	    "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);
}
?>

実際に作成したBOTを使ってみる

HOME へ戻る



提供:北海道電力ネットワーク㈱ 北海道電気管理技術者協会 札-459号
長谷川電気管理事務所(長谷川博)