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

本記事は、[めんどい太郎の Advent Calendar 2023](https://qiita.com/advent-calendar/2023/mendoitarou_first_calendar) 17日目の記事です。

# はじめに

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

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

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

# やり方

## DiscordのWebhookURL取得

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/e5eb4be4-488d-ec53-1c63-564a5085f71f.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/6440e399-a806-7e99-1d65-c56f485dae9c.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/446a3671-24b0-bf6f-4c07-4e5c3f2dba14.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/a6315e3f-ddcf-e5bd-383b-92f919c0fda4.png)

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

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

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

## Google Apps Script

まずは[Google Apps Script](https://script.google.com/)にアクセスします。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/07f9f652-af71-50e9-9dff-80e4895b5e2b.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/2132a70a-b74b-9b76-a757-929f20268ff5.png)

## エリアコードを取得

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

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

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/3b3647df-2080-01fb-eb87-b4d2ce72453b.png)

一部を切り取ります。

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

こんな感じですかね。

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

## 試しにJSONを取得してみる

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

```Shell
curl https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json
```

```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":["北の風","北西の風　後　南の風　２３区西部　では　後　南西の風　やや強く","南西の風　やや強く"],"waves":["０．５メートル","０．５メートル　後　１．５メートル","１．５メートル"]},{"area":{"name":"伊豆諸島北部","code":"130020"},"weatherCodes":["111","202","202"],"weathers":["晴れ　夜　くもり　所により　雨","くもり　朝　から　昼前　雨","くもり　一時　雨"],"winds":["南西の風　後　北の風","北の風　後　南西の風　強く","南西の風　強く　後　西の風　強く"],"waves":["１．５メートル　うねり　を伴う","１．５メートル　後　３メートル","３メートル"]},{"area":{"name":"伊豆諸島南部","code":"130030"},"weatherCodes":["200","210","202"],"weathers":["くもり　所により　雨","くもり　昼過ぎ　から　時々　晴れ　三宅島　では　昼前　まで　雨","くもり　一時　雨"],"winds":["南の風","南の風　やや強く　後　南西の風　強く","南西の風　強く　後　西の風　強く"],"waves":["２メートル　うねり　を伴う","２メートル　後　３メートル　うねり　を伴う","３メートル"]},{"area":{"name":"小笠原諸島","code":"130040"},"weatherCodes":["201","101","101"],"weathers":["くもり　夜のはじめ頃　晴れ","晴れ　時々　くもり","晴れ　時々　くもり"],"winds":["東の風　やや強く","東の風　はじめ　やや強く","南の風"],"waves":["４メートル　うねり　を伴う","４メートル　後　３メートル　うねり　を伴う","３メートル　後　２メートル　うねり　を伴う"]}]},{"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。

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

``areas``の``area``を見ます。

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

東京地方であれば、

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

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

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

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

## ライブラリ読み込み

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/46b3cf86-8e5f-d72c-7546-00d9d4407c46.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/7d6e9c34-0fc1-e77c-5f0c-03eed4246311.png)

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

**追記**

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

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

```
12Q9V-asnmjCFh4zcpqxSFMFD9Lu9sFDHDkplqXLmeX-Shj8H6eo5kmQx
```

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/f6050d7f-af65-9d3f-c159-a7e8d4f696c4.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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/39f2aa2f-996f-2ba0-ec3c-68ea6e736e4a.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/bd510e3e-5358-8c32-073e-71afbba3ef4d.png)

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

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/c4074c0d-a2cd-c737-8fd4-fe40121c7998.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/244843eb-5883-274a-e1f1-092d7e33bbd7.png)

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

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/69951d23-dced-0cf5-cab5-5f13b25ab667.png)

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

## 定期実行の設定

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

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

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/6976588b-8513-bea8-c259-910766879c7a.png)

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/afe60988-69a2-f0d9-60ba-cdabb548818d.png)

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

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

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/388444/3150c52a-f4cd-cf5a-7c88-b2187e4d61d6.png)

これでOKです！

# 終わりに

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

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

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

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

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