Q&A
実現したいこと
LINEで気象庁の天気予報が毎朝送られてくるシステムを作りたい
前提
初心者です。
GASとLINE Notifyを連携させて通知を送る方法を検索しました。
トークンを取得しネットにあったサンプルコードを記述し実行するとテキストが送信できたためそこはクリアしてると思います。
その後気象庁のAPIを見たのですが、階層が分からなかったためLivedoor天気互換というサイトのAPIを使うことにしました。
発生している問題・エラー
Exception: Request failed for https://notify-api.line.me returned code 400. Truncated server response: {"status":400,"message":"message: must not be empty"} (use muteHttpExceptions option to examine full response) sendMessage @ 無題.gs:25
該当のソースコード
トークンは隠しています。
GAS
1function sendMessage(){ 2// A, LINE Notifyのトークンを登録 3const token = "*******************************************"; 4const lineNotifyApi = "https://notify-api.line.me/api/notify"; 5let jma_url = "https://weather.tsukumijima.net/api/forecast/city/400040"; 6let jma_response = UrlFetchApp.fetch(jma_url).getContentText(); 7let jma_json = JSON.parse(jma_response); 8 9// B, メッセージを作成 10let message = "日時:"+jma_json["publishingOffice"] + "\n" + 11 "天気概況:"+jma_json["description"][4] + "\n" + 12 "今日の天気:" + jma_json.forecasts[0].detail.weather +" 風速"+ jma_json.forecasts[0].detail.wave +"\n" + 13 "降水確率:" + jma_json.forecasts[0].chanceOfRain.T00_06 + "→" + jma_json.forecasts[0].chanceOfRain.T06_12 + "→" + jma_json.forecasts[0].chanceOfRain.T12_18 + "→" + jma_json.forecasts[0].chanceOfRain.T18_24 + "\n" + 14 "気温:" + jma_json.forecasts[2].temperature.min.celsius+"/" +jma_json.forecasts[2].temperature.max.celsius + "\n" + 15 "明日 " +jma_json.forecasts[1].telop + "\n" + 16 "明後日 " +jma_json.forecasts[2].telop ; 17 18// C, メッセージを送信 19if (message) { 20 const options = { 21 "method": "post", // POST送信 22 "payload": "message=" + message, // 送信するメッセージ 23 "headers": { "Authorization": "Bearer " + token } 24 }; 25 UrlFetchApp.fetch(lineNotifyApi, options); 26} 27}
試したこと
気象情報がしっかり受け取れているかの確認でLogに出したところ、しっかりと動いてました。
messageの変数の入力をいろいろな方法で試した。
chatGPTに聞いた
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/03/11 23:49
退会済みユーザー
2023/03/12 01:04
2023/03/12 05:56