From e2f030136b24e30b47632a1d123e19ac8186df0a Mon Sep 17 00:00:00 2001 From: "izumi.wang" Date: Tue, 24 Oct 2023 09:26:46 +0800 Subject: [PATCH] added push message to line group --- iotapi/push_earthquake_message.php | 63 ++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/iotapi/push_earthquake_message.php b/iotapi/push_earthquake_message.php index eb69dea7..35e94036 100644 --- a/iotapi/push_earthquake_message.php +++ b/iotapi/push_earthquake_message.php @@ -14,20 +14,77 @@ if ($db_query["secret_key"] == "masada_earthquake_SS300"){ require_once "database.php"; $earthquake_id = $link->real_escape_string($db_query['earthquake_id']); # 測試階段只針對指定對象處理(A000000001) - $sql = "INSERT INTO push_message (accountid, message_detail, pushed_time, creater, create_at) + $sql = "INSERT INTO push_message (accountid, message_detail, pushed_time, creater, create_at, source) SELECT DISTINCT f.customerid AS accountid, CONCAT('地震訊息:', s.earthquake_type, ' ', s.county, ':最大震度', s.county_level, '級') AS message_detail, now() AS pushed_time, 'system' AS creater, - TIMESTAMP(f_return_date(s.issue_date, 1), f_return_time(s.issue_time)) AS create_at + TIMESTAMP(f_return_date(s.issue_date, 1), f_return_time(s.issue_time)) AS create_at, + 'cwa' as source FROM facility as f INNER JOIN ( SELECT DISTINCT earthquake_type, county, county_level, issue_date, issue_time FROM earthquake_list WHERE earthquake_id = '$earthquake_id' - ) AS s ON s.county = CONVERT(f_return_county(f.area) using utf8) COLLATE utf8_general_ci + ) AS s ON s.county = f_return_county(f.area COLLATE utf8_general_ci) WHERE f.customerid = 'A000000001'"; $link->query($sql); + + $sql = "SELECT DISTINCT earthquake_type, issue_date, issue_time, longitude, latitude, earthquake_depth, unit_of_depth, + earthquake_level, direction, direction_distence, distence_unit, distence_unit, source_descript, + county, county_level + FROM earthquake_list WHERE earthquake_id = '$earthquake_id'"; + $result = $link->query($sql); + $datas = $result->fetch_all(MYSQLI_ASSOC); mysqli_close($link); + + $datas = array_reduce($datas, function ($acc, $row) { + if (!$acc) { + $acc = array( + "message" => sprintf("%s震度%d級", trim($row["earthquake_type"]), $row["earthquake_level"]), + "datetime" => sprintf("%s %s", $row["issue_date"], $row["issue_time"]), + "lng" => $row["longitude"], + "lat" => $row["latitude"], + "address" => sprintf("%s%d%s(%s)", $row["direction"], $row["direction_distence"], $row["distence_unit"], $row["source_descript"]), + "depth" => sprintf("%s%s", $row["earthquake_depth"], $row["unit_of_depth"]), + "county" => array(), + ); + } + $acc["county"][] = sprintf("%s最大震度%d級", $row["county"], $row["county_level"]); + return $acc; + }, array()); + // file_put_contents("./test.log", print_r($datas, true)."\r\n", FILE_APPEND); + + //以下推送到客户端消息 + $access_token = array(); + $access_token[]="OMzyxk983YviHTp3SI8BQUidgAguITTmCaLNEbKqnTE"; + // $access_token[]="blymLDSO21gDaCXA6nJ2jf6fEZTNE0wEwANbLAhKMhM"; + $TargetCount = count($access_token); + $Push_Content['message'] = "\n"; + $Push_Content['message'] = $Push_Content['message'] . "地震訊息: " . $datas["message"] . "\n"; + $Push_Content['message'] = $Push_Content['message'] . "地震時間: " . $datas["datetime"] . "\n"; + $Push_Content['message'] = $Push_Content['message'] . "經度: " . $datas["lng"] . "\n"; + $Push_Content['message'] = $Push_Content['message'] . "緯度: " . $datas["lat"] . "\n"; + $Push_Content['message'] = $Push_Content['message'] . "位置: " . $datas["address"] . "\n"; + $Push_Content['message'] = $Push_Content['message'] . "深度: " . $datas["depth"] . "\n"; + $Push_Content['message'] = $Push_Content['message'] . implode("\n", $datas["county"]) . "\n"; + + for ($i=0; $i<$TargetCount; $i++) { + $ch = curl_init("https://notify-api.line.me/api/notify"); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($Push_Content)); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Content-Type: application/x-www-form-urlencoded', + 'Authorization: Bearer '.$access_token[$i] + )); + $response_json_str = curl_exec($ch); + curl_close($ch); + // file_put_contents("./test.log", print_r($response_json_str, true)."\r\n", FILE_APPEND); + } + //以上推送到客户端消息 }