コンテンツにスキップ

ループ処理サンプル

robosteinでループ処理を作成する場合のサンプルフローです。

サンプルフロー


[{"id":"f4eb7384.e6fde","type":"comment","z":"bac93068.01bd1","name":"ループパターン1","info":"このループ構造の場合高速でループするため、\n例えば外部のサービス(APIやサイト)を実行する\nような処理で利用すると相手サービス次第では\nうまく動作しない可能性があります。\n\nその場合はループパターン2を利用しましょう。","x":120,"y":4100,"wires":[]},{"id":"490e9f11.2e122","type":"inject","z":"bac93068.01bd1","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":4540,"wires":[["2a9e9132.27d22e"]]},{"id":"2a9e9132.27d22e","type":"template","z":"bac93068.01bd1","name":"ループ回数","field":"loopCount","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"10","output":"str","x":310,"y":4540,"wires":[["f1ea3d25.021b4"]]},{"id":"f1ea3d25.021b4","type":"switch","z":"bac93068.01bd1","name":"ループ条件","property":"loopCount","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"},{"t":"lte","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":4540,"wires":[["7f929950.1549e8"],["65f9d512.59f2ac","a1856d61.76798"]]},{"id":"e5ffd87d.3033f8","type":"function","z":"bac93068.01bd1","name":"減算処理","func":"msg.loopCount -= 1\nreturn msg;\n","outputs":1,"noerr":0,"x":500,"y":4440,"wires":[["f1ea3d25.021b4"]]},{"id":"ff5e95d6.9796d8","type":"debug","z":"bac93068.01bd1","name":"繰り返しが終わったあとの処理はこっちに","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":4680,"wires":[]},{"id":"1278c012.a088e","type":"debug","z":"bac93068.01bd1","name":"この処理も繰り返されます。","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1140,"y":4600,"wires":[]},{"id":"65f9d512.59f2ac","type":"corobo-trigger","z":"bac93068.01bd1","name":"正常終了","status":"1","statusType":"num","x":700,"y":4740,"wires":[]},{"id":"3cfe5497.c1f63c","type":"comment","z":"bac93068.01bd1","name":"ループパターン2","info":"このループ構造の場合処理の後にループの処理が\n入る事になるのでパターン1とは違い、処理を\n終えてからのループとなります。\n\n例えば外部のサービス(APIやサイト)を実行する\nような処理で利用すると相手サービスに対して\nやさしい処理となるのでこちらのループを利用\nしましょう。\n","x":120,"y":4480,"wires":[]},{"id":"642ff77f.739e18","type":"function","z":"bac93068.01bd1","name":"繰り返しで処理をしたいノードを","func":"\nreturn msg;","outputs":1,"noerr":0,"x":1160,"y":4540,"wires":[["1278c012.a088e","e5ffd87d.3033f8"]]},{"id":"9c390f00.6c99f","type":"template","z":"bac93068.01bd1","name":"ここらへんに","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"あと{{loopCount}}回!","output":"str","x":900,"y":4540,"wires":[["642ff77f.739e18"]]},{"id":"a1856d61.76798","type":"template","z":"bac93068.01bd1","name":"終了処理","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"お疲れ様でした!\n","output":"str","x":700,"y":4680,"wires":[["ff5e95d6.9796d8"]]},{"id":"7f929950.1549e8","type":"delay","z":"bac93068.01bd1","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":710,"y":4540,"wires":[["9c390f00.6c99f"]]},{"id":"b29a7368.07184","type":"inject","z":"bac93068.01bd1","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":4160,"wires":[["238690ca.b2f9c"]]},{"id":"238690ca.b2f9c","type":"template","z":"bac93068.01bd1","name":"ループ回数","field":"loopCount","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"10","output":"str","x":310,"y":4160,"wires":[["a897ea73.6deac8"]]},{"id":"a897ea73.6deac8","type":"switch","z":"bac93068.01bd1","name":"ループ条件","property":"loopCount","propertyType":"msg","rules":[{"t":"gt","v":"0","vt":"str"},{"t":"lte","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":4160,"wires":[["e46c59ca.148c98","165d453b.8296fb"],["a15dcce1.6f9a3","cf35ad13.dc576"]]},{"id":"165d453b.8296fb","type":"function","z":"bac93068.01bd1","name":"減算処理","func":"msg.loopCount -= 1\nreturn msg;\n","outputs":1,"noerr":0,"x":500,"y":4060,"wires":[["a897ea73.6deac8"]]},{"id":"696229af.e850d8","type":"debug","z":"bac93068.01bd1","name":"繰り返しが終わったあとの処理はこっちに","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":4300,"wires":[]},{"id":"b8b7f18f.588d4","type":"debug","z":"bac93068.01bd1","name":"この処理も繰り返されます。","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1140,"y":4220,"wires":[]},{"id":"a15dcce1.6f9a3","type":"corobo-trigger","z":"bac93068.01bd1","name":"正常終了","status":"1","statusType":"num","x":700,"y":4360,"wires":[]},{"id":"8945c005.f8a0d","type":"function","z":"bac93068.01bd1","name":"繰り返しで処理をしたいノードを","func":"\nreturn msg;","outputs":1,"noerr":0,"x":1160,"y":4160,"wires":[["b8b7f18f.588d4"]]},{"id":"cfa588d8.a1a118","type":"template","z":"bac93068.01bd1","name":"ここらへんに","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"あと{{loopCount}}回!","output":"str","x":900,"y":4160,"wires":[["8945c005.f8a0d"]]},{"id":"cf35ad13.dc576","type":"template","z":"bac93068.01bd1","name":"終了処理","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"お疲れ様でした!\n","output":"str","x":700,"y":4300,"wires":[["696229af.e850d8"]]},{"id":"e46c59ca.148c98","type":"delay","z":"bac93068.01bd1","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":710,"y":4160,"wires":[["cfa588d8.a1a118"]]}]

上記を設置すると以下のようなフローが表示されます。 slack-botノード

解説

コメントにも記載がありますが以下となります。

ループパターン1

このループ構造の場合、高速でループするため、例えば外部のサービス(APIやサイト)を実行するような処理で利用すると相手サービス次第ではうまく動作しない可能性があります。基本はループパターン2を利用しましょう。

ループパターン2

このループ構造の場合、処理の後にループの処理が入る事になるのでパターン1とは違い、処理を終えてからのループとなります。例えば外部のサービス(APIやサイト)を実行するような処理で利用すると相手サービスに対してやさしい処理となるのでこちらのループを利用しましょう。

処理の確認

どちらの処理もデバッグコンソール上にループ処理の状態を表示しますのでパラメータを変更したりして動作の変化を確認してください。

これを使って

このサンプルフローを参考にし、

  • ループ条件があるフローの作成

など、パーツとしての利用やフロー作成の参考にしてください。