LoginSignup
9
3

More than 1 year has passed since last update.

【Node-RED】新しくなったAPIリクエストの設定方法

Last updated at Posted at 2022-04-14

はじめに

Node-REDでLINE Botを作る際に、APIリクエストの仕様が新しくなっていた。(今更かもしれないが……)
結果は新旧どちらで設定しても変わらないが、新だとノードの数をちょっと減らせるので若干すっきりするような気がする。
従来の設定方法(旧)と、新しい設定方法(新)について以下に記載する。

(旧)http inノードをスタートとする場合

Replyノードに、msg.payload.events[0].message.textで返したいテキストを入れる。

image.png

josn
[{"id":"9a8ab7a1.5d84b8","type":"ReplyMessage","z":"9c7a7c03.ad6f2","name":"","replyMessage":"","x":840,"y":140,"wires":[]},{"id":"68647f64.58b23","type":"function","z":"9c7a7c03.ad6f2","name":"","func":"// http requestの結果を取得する\nconst result = msg.payload;\n// LINEサーバーからの内容を復元する\nmsg.payload = msg.line;\n// 返信メッセージをhttp requestの結果にする\nmsg.payload.events[0].message.text = result;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":140,"wires":[["9a8ab7a1.5d84b8"]]},{"id":"6c42593d.ed8ec8","type":"http request","z":"9c7a7c03.ad6f2","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://weather.tsukumijima.net/api/forecast/city/400040","tls":"","persist":false,"proxy":"","authType":"","x":510,"y":140,"wires":[["68647f64.58b23"]]},{"id":"8ad7991e.58a2f8","type":"function","z":"9c7a7c03.ad6f2","name":"","func":"// LINEサーバーからの情報を「msg.line」に移動させる\nmsg.line = msg.payload;\n// msg.payloadにメッセージ本体を入れる\nmsg.payload = msg.payload.events[0].message.text;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":140,"wires":[["6c42593d.ed8ec8"]]},{"id":"25d1c787.6317c8","type":"http in","z":"9c7a7c03.ad6f2","name":"","url":"/webhook","method":"post","upload":false,"swaggerDoc":"","x":180,"y":140,"wires":[["8ad7991e.58a2f8"]]}]

http inノード
メソッド:POST
URL: /webhook

functionノード(左)
コードに以下を指定

// LINEサーバーからの情報を「msg.line」に移動させる
msg.line = msg.payload;
// msg.payloadにメッセージ本体を入れる
msg.payload = msg.payload.events[0].message.text;

return msg;

http requestノード
出力形式:JSONオブジェクト
※任意のAPI(ここでは天気予報APIを設定)

functionノード(右)
コードに以下を指定

// http requestの結果を取得する
const result = msg.payload;
// LINEサーバーからの内容を復元する
msg.payload = msg.line;
// 返信メッセージをhttp requestの結果にする
msg.payload.events[0].message.text = result;

return msg;

Replyノード
「Secret」「AccessToken」設定

(新)Webhookノードをスタートとする場合

ReplyMessageノードがmsg.payloadの中身をそのまま返してくれる。
msg.payload.events[0].~が入ってきたら旧仕様で動く。

image.png

json
[{"id":"796aa5ee.42b38c","type":"Webhook","z":"e9907faa.36928","name":"","url":"/webhook","x":140,"y":200,"wires":[["6c98ee1f.7816f"]]},{"id":"74a57a35.8b9074","type":"ReplyMessage","z":"e9907faa.36928","name":"","replyMessage":"","x":640,"y":200,"wires":[]},{"id":"6c98ee1f.7816f","type":"http request","z":"e9907faa.36928","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://weather.tsukumijima.net/api/forecast/city/400040","tls":"","persist":false,"proxy":"","authType":"","x":310,"y":200,"wires":[["b9403a59.40b1e8"]]},{"id":"b9403a59.40b1e8","type":"function","z":"e9907faa.36928","name":"","func":"msg.payload = '今日の' + msg.payload.title + 'は' + msg.payload.forecasts[0].telop + 'です。' ;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":200,"wires":[["74a57a35.8b9074"]]}]

webhookノード
Path: /webhook

http requestノード
URL:https://weather.tsukumijima.net/api/forecast/city/400040
出力形式:JSONオブジェクト
※任意のAPI(ここでは天気予報APIを設定)

functionノード
コードに以下を指定

msg.payload = '今日の' + msg.payload.title + '' + msg.payload.forecasts[0].telop + 'です。' ;
return msg;

Replyノード
「Secret」「AccessToken」設定

9
3
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
9
3