4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

買い物・洗濯日和をお知らせしてほしい

Posted at

背景・目的

平日仕事で忙しいことを言い訳に家事や買い出しを後回しにしてしまう。
休日にまとめて買い物、洗濯をしようと思い立つもお天気が斜め…
早く帰れたあの日にやっておけば…
なんていう後悔が多いことが最近の悩みです。

重い腰を上げるきっかけに家事日和を教えてくれるLINE Botを作りたい。
今回はアイデア出しも兼ねてLINE Botを動かすところまで実装してみたいと思います。

使用環境

・Node-RED
・LINE Bot
・Web API

アイデア

気象庁の天気予報APIから情報を取得し、その週の天気予報から最適な洗濯、買い物日和を推薦させる。
・雨が降らない
・暑すぎず、寒すぎず
・花粉が少ない
など、快適に外出できる条件を検索して推薦してくれたら嬉しい。

実装:LINE Botから必要な情報を引き出してもらう。

気象庁の天気予報APIから情報を取得するのに必要な情報をLINE Botに引き出してもらう。
LINE Botに入力した情報を元に天気予報API

フロー
image.png

天気予報API
該当する地域(エリアコード)の情報をJSON形式で取得できる。

https://www.jma.go.jp/bosai/forecast/data/forecast/{エリアコード}.json

コード

[{"id":"d6b8724dc52b8888","type":"tab","label":"フロー 2","disabled":false,"info":"","env":[]},{"id":"214702b8f1d4c42c","type":"ReplyMessage","z":"d6b8724dc52b8888","name":"","replyMessage":"","x":1100,"y":140,"wires":[]},{"id":"bf5155ed1e0ea4b7","type":"Webhook","z":"d6b8724dc52b8888","name":"","url":"/webhook","x":120,"y":140,"wires":[["467612710e25d60c"]]},{"id":"467612710e25d60c","type":"switch","z":"d6b8724dc52b8888","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"天気","vt":"str"},{"t":"cont","v":"買い物","vt":"str"},{"t":"cont","v":"洗濯","vt":"str"},{"t":"cont","v":"家事","vt":"str"},{"t":"cont","v":"関東","vt":"str"},{"t":"eq","v":"東京都","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":7,"x":330,"y":140,"wires":[["4665bc2b4e90a417"],["4665bc2b4e90a417"],["4665bc2b4e90a417"],["4665bc2b4e90a417"],["e55e7cab5df1f9c6"],["66c36950cacd3445"],["69b08fb10599cc66"]]},{"id":"4665bc2b4e90a417","type":"template","z":"d6b8724dc52b8888","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"住んでいる地方を入力してね!\n\n北海道地方\n東北地方\n関東甲信地方\n東海地方\n北陸地方\n近畿地方\n中国地方\t\n四国地方\n九州北部地方\n九州南部・奄美地方\n沖縄地方","output":"str","x":560,"y":140,"wires":[["214702b8f1d4c42c","467612710e25d60c"]]},{"id":"69b08fb10599cc66","type":"template","z":"d6b8724dc52b8888","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"わかりません。","output":"str","x":560,"y":260,"wires":[["214702b8f1d4c42c"]]},{"id":"e55e7cab5df1f9c6","type":"template","z":"d6b8724dc52b8888","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"住んでいる地域を入力してね!\n\n【関東甲信地方】\n茨城\n栃木\n群馬\n埼玉\n千葉\n東京都\n神奈川\n山梨\n長野","output":"str","x":560,"y":180,"wires":[["214702b8f1d4c42c"]]},{"id":"66c36950cacd3445","type":"http request","z":"d6b8724dc52b8888","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://www.jma.go.jp/bosai/forecast/data/forecast/130000.json","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":570,"y":220,"wires":[["a431892d3e240506"]]},{"id":"a431892d3e240506","type":"function","z":"d6b8724dc52b8888","name":"function pops","func":"let array_data = msg.payload;\nlet pops = [];\n\n//降水確率を取得\nfor (let i = 0; i < 1; i++) {\n    pops[i] = msg.payload[1].timeSeries[0].areas[0].pops;\n}\nmsg.pops = pops;\nmsg.payload = msg.pops;\n\nreturn msg;\n\n//httpリクエストの前に地域コードを選択するようにすれば、for文で選択した\n//地域コードから降水確率を取得することができる。","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":220,"wires":[["214702b8f1d4c42c"]]}]

実行結果
現状では週間降水確率まで取得させることができた。
https://x.com/i/status/1795151894232391688

追加したい機能

・取得した情報を元に最適な日程を推薦させる。

おわりに

今回は最近の自分の困りごとを最近知ったツール(LINE Bot)を使って実装しようと試みました。やりたかったことが盛り込み切れなかったので、今後も勉強を続けて改良していきたいと思います。

参考文献

https://qiita.com/youtoy/items/932bc48b03ced5a45c71
https://qiita.com/miyaken3381/items/1bc7530a211a507a19dc
https://zenn.dev/inoue2002/articles/2e07da8d0ca9ca

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?