2020.02.19
STAFF BLOG
スタッフブログ
TECHNICAL
テクログ

初めまして。
1月からお世話になっておりますJGです。
最近LINE Notify APIを使い、毎朝自分のLINEに通知させました。
それについて触れたいと思います。
下記URLからLineIDでログインし、必要事項を入力します。
その後、アクセストークン発行をクリックし、発行されたトークンをコピーして保存してください。
https://notify-bot.line.me/ja/
天気予報を取得する処理は下記のlivedoorを使用しました。
http://weather.livedoor.com/weather_hacks/webservice
PHPで処理を書き、作成したものをHerokuに上げ、毎朝通知するようにHerokuのスケジューラを設定しました。
下記ソースのTOKENに、LINEのアクセストークンを指定すれば動きます。
自分の住んでいる東京と出身地の神奈川の天気を通知するために、$city_listを下記のように指定しています。
興味がある方は、是非試してみてください。
<?php
define("TOKEN", "");
define("MESSAGE_NOTIFY_API_URL", "https://notify-api.line.me/api/notify");
define("WEATHER_API_URL", "http://weather.livedoor.com/forecast/webservice/json/v1?city=");
$city_list = [
130010,
140010,
140020
];
foreach ($city_list as $city) {
//天気予報を取得
$weather_data = get_weather_data($city);
if (empty($weather_data)) {
$msg = "天気予報取得失敗です。";
} else {
//通知メッセージを作成
$msg = create_message($weather_data);
}
//通知
send_msg($msg);
}
function get_weather_data($city) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, WEATHER_API_URL.$city);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($http_code != 200) {
return false;
}
if (curl_error($ch)) {
return false;
}
$weather_data = json_decode($response, true);
curl_close($ch);
return $weather_data;
}
function create_message($arr) {
$msg = "";
$title = $arr["title"] ?? "";
$msg .= $title."??";
for ($i = 0; $i < 2; $i++) {
$date = $arr["forecasts"][$i]["date"] ?? "";
$date_label = $arr["forecasts"][$i]["dateLabel"] ?? "";
$weather = $arr["forecasts"][$i]["image"]["title"] ?? "";
$min = $arr["forecasts"][$i]["temperature"]["min"]["celsius"] ?? "";
$max = $arr["forecasts"][$i]["temperature"]["max"]["celsius"] ?? "";
$msg .= $date_label."(".$date.")"."?";
$msg .= "天気: ".$weather."?";
$msg .= "最高気温: ".$max."?";
$msg .= "最低気温: ".$min;
if ($i < 1) {
$msg .= "??";
}
}
return $msg;
}
function send_msg($msg) {
$post_data = http_build_query(["message" => $msg]);
$ch = curl_init(MESSAGE_NOTIFY_API_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Bearer ".TOKEN,
"Content-Length: ".strlen($post_data)
]);
curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($http_code != 200) {
return false;
}
if (curl_error($ch)) {
return false;
}
curl_close($ch);
return true;
}