node-red
LINEmessagingAPI

天気情報を取得してLINEで投稿する

概要

  • とりあえず、冬休みに何か作ろうと思った
  • 毎朝、天気予報を見ているが、LINEに通知されてくれば便利かなぐらいのモチベーション

事前準備

  1. LINE Messaging APIを利用できる環境の準備
  2. IBM BluemixでNode-Redを起動

  3. Node-Redでフロー作成

    • 天気情報を取得するAPIの情報については、天気情報を取得するなどでも記載されている通り、OpenWeatherMapが良さそうだったのですが、さくっと作るために、livedoor天気を利用させて頂きました。

Node-Redのフロー

[{"id":"dfad30eb.8da4","type":"inject","z":"3647a876.1ec048","name":"毎朝6:00(JST)","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 21 * * *","once":false,"x":157,"y":112,"wires":[["db9264f1.82cbf8"]]},{"id":"db9264f1.82cbf8","type":"http request","z":"3647a876.1ec048","name":"Livedoorから東京都の天気予報を取得","method":"GET","ret":"txt","url":"http://weather.livedoor.com/forecast/webservice/json/v1?city=130010","tls":"","x":417,"y":123,"wires":[["a9a0ce5c.cfda8"]]},{"id":"603a26c1.e9f798","type":"debug","z":"3647a876.1ec048","name":"","active":true,"console":"false","complete":"payload","x":990,"y":200,"wires":[]},{"id":"a9a0ce5c.cfda8","type":"json","z":"3647a876.1ec048","name":"ResponseをJSONに変換","pretty":false,"x":441,"y":231,"wires":[["f309d232.26ab7"]]},{"id":"f309d232.26ab7","type":"function","z":"3647a876.1ec048","name":"天気予報結果を組立","func":"telop = msg.payload.forecasts[0].telop;\ncelsius = msg.payload.forecasts[0].temperature.max.celsius;\ndescription = msg.payload.description.text;\n\ntmp = telop+\"\\n\"+\"最高気温 \"+celsius+\"℃\\n\\n\"+description;\nmsg.text = {};\nmsg.text = tmp;\nreturn msg;","outputs":1,"noerr":0,"x":667,"y":309,"wires":[["a31c7bb7.412d28"]]},{"id":"a31c7bb7.412d28","type":"function","z":"3647a876.1ec048","name":"Lineへの応答を編集","func":"msg.headers = {};\nmsg.headers.Authorization = 'Bearer ★{channel access token}★';\nmsg.headers[\"Content-Type\"] = 'application/json';\nmsg.payload = null;\nmsg.payload = {};\nmsg.payload.to = \"★送信先のID。Webhookイベントオブジェクトで返される、userId、groupId、またはroomIdの値★\";\nmsg.payload.messages = [];\nmsg.payload.messages[0] = {};\nmsg.payload.messages[0].type = \"text\";\nmsg.payload.messages[0].text = msg.text;\nreturn msg;","outputs":1,"noerr":0,"x":517,"y":454,"wires":[["d842ea4b.16c098"]]},{"id":"d842ea4b.16c098","type":"http request","z":"3647a876.1ec048","name":"LineBotへ応答","method":"POST","ret":"txt","url":"https://api.line.me/v2/bot/message/push","tls":"","x":780,"y":500,"wires":[["603a26c1.e9f798"]]}]