0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GASで毎日Discordに天気予報を送る

Last updated at Posted at 2023-12-16

本記事は、めんどい太郎の Advent Calendar 2023 17日目の記事です。

はじめに

この記事は初心者が書いています。

皆さんはDisocrd使ってますか?

今回は毎朝Disocrdに天気予報を送信するスクリプトをGoogleAppsScriptで作成しましたので、紹介いたします。

やり方

DiscordのWebhookURL取得

Disocrdで天気予報を送りたいテキストチャンネルの設定を開きます。

image.png

連携サービスのウェブフック欄にあるウェブフックを作成を押します。

image.png

作成されたウェブフックの設定をします。

image.png

名前を天気予報などわかりやすい名前に変えて、設定を保存します。

image.png

その後、ウェブフックURLをコピーします。

URLは以下のようなものです。このURLをメモしておいてください。

https://discord.com/api/webhooks/~~~~~~~~~~/~~~~~~~~~~

Google Apps Script

まずはGoogle Apps Scriptにアクセスします。

image.png

新しくプロジェクトを作成します。

image.png

エリアコードを取得

以下のURLのJSONファイルを取得し、自分のエリアコードを検索します。

https://www.jma.go.jp/bosai/common/const/area.json

かなり見ずらいですが、ブラウザで上記のURLにアクセスし検索機能を使えばOKです。

image.png

一部を切り取ります。

"130000":{"name":"東京都",

こんな感じですかね。

ここの130000がエリアコードです。

試しにJSONを取得してみる

Google Apps Scriptを使わずにcurlで取得してみます。

curl https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json
130000.json
[{"publishingOffice":"気象庁","reportDatetime":"2023-12-14T17:00:00+09:00","timeSeries":[{"timeDefines":["2023-12-14T17:00:00+09:00","2023-12-15T00:00:00+09:00","2023-12-16T00:00:00+09:00"],"areas":[{"area":{"name":"東京地方","code":"130010"},"weatherCodes":["111","202","200"],"weathers":["晴れ 夜 くもり","くもり 朝 から 昼前 雨","くもり"],"winds":["北の風","北西の風 後 南の風 23区西部 では 後 南西の風 やや強く","南西の風 やや強く"],"waves":["0.5メートル","0.5メートル 後 1.5メートル","1.5メートル"]},{"area":{"name":"伊豆諸島北部","code":"130020"},"weatherCodes":["111","202","202"],"weathers":["晴れ 夜 くもり 所により 雨","くもり 朝 から 昼前 雨","くもり 一時 雨"],"winds":["南西の風 後 北の風","北の風 後 南西の風 強く","南西の風 強く 後 西の風 強く"],"waves":["1.5メートル うねり を伴う","1.5メートル 後 3メートル","3メートル"]},{"area":{"name":"伊豆諸島南部","code":"130030"},"weatherCodes":["200","210","202"],"weathers":["くもり 所により 雨","くもり 昼過ぎ から 時々 晴れ 三宅島 では 昼前 まで 雨","くもり 一時 雨"],"winds":["南の風","南の風 やや強く 後 南西の風 強く","南西の風 強く 後 西の風 強く"],"waves":["2メートル うねり を伴う","2メートル 後 3メートル うねり を伴う","3メートル"]},{"area":{"name":"小笠原諸島","code":"130040"},"weatherCodes":["201","101","101"],"weathers":["くもり 夜のはじめ頃 晴れ","晴れ 時々 くもり","晴れ 時々 くもり"],"winds":["東の風 やや強く","東の風 はじめ やや強く","南の風"],"waves":["4メートル うねり を伴う","4メートル 後 3メートル うねり を伴う","3メートル 後 2メートル うねり を伴う"]}]},{"timeDefines":["2023-12-14T18:00:00+09:00","2023-12-15T00:00:00+09:00","2023-12-15T06:00:00+09:00","2023-12-15T12:00:00+09:00","2023-12-15T18:00:00+09:00"],"areas":[{"area":{"name":"東京地方","code":"130010"},"pops":["0","30","60","30","10"]},{"area":{"name":"伊豆諸島北部","code":"130020"},"pops":["20","30","50","30","20"]},{"area":{"name":"伊豆諸島南部","code":"130030"},"pops":["30","40","40","20","20"]},{"area":{"name":"小笠原諸島","code":"130040"},"pops":["10","10","10","10","10"]}]},{"timeDefines":["2023-12-15T00:00:00+09:00","2023-12-15T09:00:00+09:00"],"areas":[{"area":{"name":"東京","code":"44132"},"temps":["6","11"]},{"area":{"name":"大島","code":"44172"},"temps":["11","20"]},{"area":{"name":"八丈島","code":"44263"},"temps":["16","22"]},{"area":{"name":"父島","code":"44301"},"temps":["21","25"]}]}]},{"publishingOffice":"気象庁","reportDatetime":"2023-12-14T17:00:00+09:00","timeSeries":[{"timeDefines":["2023-12-15T00:00:00+09:00","2023-12-16T00:00:00+09:00","2023-12-17T00:00:00+09:00","2023-12-18T00:00:00+09:00","2023-12-19T00:00:00+09:00","2023-12-20T00:00:00+09:00","2023-12-21T00:00:00+09:00"],"areas":[{"area":{"name":"東京地方","code":"130010"},"weatherCodes":["202","200","101","101","200","201","101"],"pops":["","40","20","20","40","30","20"],"reliabilities":["","","A","A","C","A","A"]},{"area":{"name":"伊豆諸島北部","code":"130020"},"weatherCodes":["202","202","201","101","202","200","101"],"pops":["","60","30","20","60","40","20"],"reliabilities":["","","A","A","B","B","A"]},{"area":{"name":"伊豆諸島南部","code":"130030"},"weatherCodes":["210","202","200","201","202","202","201"],"pops":["","60","40","30","60","60","30"],"reliabilities":["","","B","A","B","B","A"]},{"area":{"name":"小笠原諸島","code":"130040"},"weatherCodes":["101","101","202","201","201","200","202"],"pops":["","20","60","30","30","40","50"],"reliabilities":["","","B","A","B","C","C"]}]},{"timeDefines":["2023-12-15T00:00:00+09:00","2023-12-16T00:00:00+09:00","2023-12-17T00:00:00+09:00","2023-12-18T00:00:00+09:00","2023-12-19T00:00:00+09:00","2023-12-20T00:00:00+09:00","2023-12-21T00:00:00+09:00"],"areas":[{"area":{"name":"東京","code":"44132"},"tempsMin":["","14","7","4","4","4","2"],"tempsMinUpper":["","15","8","6","7","5","4"],"tempsMinLower":["","13","6","3","3","1","1"],"tempsMax":["","20","17","10","9","12","9"],"tempsMaxUpper":["","23","18","12","12","14","12"],"tempsMaxLower":["","18","15","8","7","9","7"]},{"area":{"name":"大島","code":"44172"},"tempsMin":["","15","7","6","6","6","4"],"tempsMinUpper":["","16","9","8","8","8","6"],"tempsMinLower":["","13","6","4","4","4","2"],"tempsMax":["","21","17","12","11","13","9"],"tempsMaxUpper":["","22","18","13","14","15","12"],"tempsMaxLower":["","20","15","10","10","12","8"]},{"area":{"name":"八丈島","code":"44263"},"tempsMin":["","17","10","9","10","10","7"],"tempsMinUpper":["","18","11","11","11","12","9"],"tempsMinLower":["","16","8","7","8","8","5"],"tempsMax":["","23","19","13","16","15","12"],"tempsMaxUpper":["","24","20","15","18","19","15"],"tempsMaxLower":["","22","17","12","14","14","10"]},{"area":{"name":"父島","code":"44301"},"tempsMin":["","21","19","19","19","19","17"],"tempsMinUpper":["","23","21","20","20","21","19"],"tempsMinLower":["","20","18","18","18","18","16"],"tempsMax":["","26","26","22","24","26","22"],"tempsMaxUpper":["","27","27","23","25","27","25"],"tempsMaxLower":["","25","25","21","23","24","21"]}]}],"tempAverage":{"areas":[{"area":{"name":"東京","code":"44132"},"min":"3.4","max":"11.7"},{"area":{"name":"大島","code":"44172"},"min":"6.2","max":"13.1"},{"area":{"name":"八丈島","code":"44263"},"min":"9.6","max":"15.1"},{"area":{"name":"父島","code":"44301"},"min":"17.9","max":"22.5"}]},"precipAverage":{"areas":[{"area":{"name":"東京","code":"44132"},"min":"0.5","max":"10.4"},{"area":{"name":"大島","code":"44172"},"min":"3.8","max":"24.1"},{"area":{"name":"八丈島","code":"44263"},"min":"18.5","max":"44.2"},{"area":{"name":"父島","code":"44301"},"min":"5.0","max":"25.8"}]}}]

ものすごい量のデータが送られてきました。

これが最新の天気予報です。

ちゃんと取得できればOK。

さらにここから場所を絞り込みます。

areasareaを見ます。

nameに東京地方や伊豆諸島北部などあるので、取得する地域のcodeをメモします。

東京地方であれば、

{"area":{"name":"東京地方","code":"130010"},

のようになっており、130010が地域コードです。

これをメモしておきます。

それではGoogle Apps Scriptに戻ります。

ライブラリ読み込み

今回は私が作成したライブラリを読み込みます。

image.png

先ほど開いたページの右にライブラリという欄があるのでプラスアイコンを押してライブラリを追加します。

image.png

スクリプトIDというやつがあるので、ここに下記のIDを入力します。

追記

公開するのを忘れていました。

公開しましたので、ご利用いただけます。

12Q9V-asnmjCFh4zcpqxSFMFD9Lu9sFDHDkplqXLmeX-Shj8H6eo5kmQx

入力後、検索すると...

image.png

こんな感じで出てくると思うので、一番下のIDをDiscord_Weatherにして追加を押します。

これでライブラリの読み込みは完了です。

送信スクリプト

コピペしましょう

function myFunction() {
  webhook_url = 'ここをDiscordのWebhook用URLに置き換え';
  role_id = 'none';
  set_area_code = 'ここをエリアコードに置き換え';
  set_area_number = 'ここをメモした地域コード';
  Discord_Weather.SendWeather(webhook_url,role_id,set_area_code,set_area_number);
}

あとは置き換えればOK。

role_idにロールIDを指定すれば、天気予報投稿時にメンションします。

権限付与

それでは一度手動で実行してみましょう。

実行ボタンを押します。

image.png

すると、権限を求められるので権限の確認を押します。

image.png

Googleアカウントでのログインを求められるのでログインします。

このアプリはGoogleで確認されていませんと表示された場合は、「詳細を表示」を押して「〜〜(安全ではないページ)へ移動」を押します。

image.png

許可を求められるので許可します。

image.png

そうすると実行されます。

テキストチャンネルを見てみると...

image.png

ちゃんと送られてますね!

定期実行の設定

定期実行の設定をします。

(このライブラリは6:00〜7:00に実行されるのを想定しています。(特に6時10分ぐらい))

横に目覚まし時計のようなマークがあるので、そこを押してトリガーの設定を開きます。

image.png

右下にトリガーの追加ボタンがあるので押します。

image.png

実行する関数などを設定します。

そして、イベントのソースを時間主導型に時間ベースのトリガーのタイプを日付ベースのタイマーに時刻を午前6時〜7時にします。

image.png

これでOKです!

終わりに

GASでJSONを受け取り、処理し、POST送信をするというのはいい経験になりました。

このライブラリ、かなり実装が雑なので不具合が起きる可能性が十分にあります。

動かない地域もあるかもしれません...

報告いただけましたら、改良するかもしれません。

GASは無料&定期実行可能なので、かなりいいサービスですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?