GASでLINEにメッセージを送る方法
Google Apps ScriptでLINE Notifyを使ってメッセージを送る方法を調べました。
参考
LINE側の説明
LINE Notify
Google Apps Script側の説明
Class UrlFetchApp | Apps Script | Google Developers
トークンの取得
LINE Notifyのマイページに行きます。
LINE Notify
「トークンを発行する」をクリックします。
「トークンを発行する」ダイアログでトークン名とトークルームをセットします。
トークン名は通知の最初に表示される見出しのようなものです。短い方がいいです。
セットした後「発行する」をクリックします。
トークンが表示されるのでコピーします。
Google Apps Script
新しいプロジェクトを準備し、コードを次のように書きます。
{token}に先程、取得したトークンを貼り付けます。
function line_notify(message) {
url = "https://notify-api.line.me/api/notify";
token = "{token}";
data = {
"message": message
};
options = {
"method": "post",
"contentType": "application/x-www-form-urlencoded",
"headers": {
"Authorization": "Bearer " + token
},
"payload": data
};
UrlFetchApp.fetch(url, options);
}
function myFunction() {
message = "GASからこんにちは";
line_notify(message);
}
url = "https://notify-api.line.me/api/notify";
token = "{token}";
data = {
"message": message
};
options = {
"method": "post",
"contentType": "application/x-www-form-urlencoded",
"headers": {
"Authorization": "Bearer " + token
},
"payload": data
};
UrlFetchApp.fetch(url, options);
}
function myFunction() {
message = "GASからこんにちは";
line_notify(message);
}
myFunctionを実行します。
LINEの指定したトークルームに「GASからこんにちは」というメッセージが送信されます。
PHPの場合
おまけとしてPHPの場合のコードを書いておきます。
line_notify("PHPからこんにちは");
function line_notify($message){
$url = "https://notify-api.line.me/api/notify";
$token = "{token}";
$data = http_build_query(array("message" => $message), "", "&");
$options = array(
"http" => array(
"method" => "POST",
"header" => "Authorization: Bearer " . $token . "\n" .
"Content-Type: application/x-www-form-urlencoded\n" .
"Content-Length: " . strlen($data) . "\n" ,
"content" => $data,
)
);
$context = stream_context_create($options);
$json = file_get_contents($url, false, $context);
$results = json_decode($json, true);
if($results["status"] != 200){
return false;
}
return true;
}
function line_notify($message){
$url = "https://notify-api.line.me/api/notify";
$token = "{token}";
$data = http_build_query(array("message" => $message), "", "&");
$options = array(
"http" => array(
"method" => "POST",
"header" => "Authorization: Bearer " . $token . "\n" .
"Content-Type: application/x-www-form-urlencoded\n" .
"Content-Length: " . strlen($data) . "\n" ,
"content" => $data,
)
);
$context = stream_context_create($options);
$json = file_get_contents($url, false, $context);
$results = json_decode($json, true);
if($results["status"] != 200){
return false;
}
return true;
}
[ 2019年10月27日 | カテゴリー: デジタル | タグ: Google Apps Script , LINE ]
« JSONとCSVを相互に変換する「JSON⇄CSV」 | VSCodeでスニペットを使う方法 »
コメントを残す