Loop for API call result polling

I would like to exectute an API call , each 5 sec, until a value from the call is set to “completed”.
so now, I created this flow with a loop but the engine seems not to accept it :

exp :
1 - I collect values from previous steps ( node “collect_vars " -> ok )
2 - I wait 5 sec ( builtin action, thanks to @abhisheksimion )
3 I perform the API call and store result in temp.pollresponse ( thanks to @asashour )
4 - Checking api value result, if equal to " pending”, I want to loop to step 2 , otherwise go next step ( check is ok )
5 - debug - just display variable value

running this flow leads to an error, see the logs :

09:42:57.822 bp:dialog (transmit) [R-0dsgjgjalgd54kybNov] transit (main.flow.json) [wait_5_Sec] -> [Poll_mobile_approve]{ botId: ‘transmit’ }, +8s
09:42:57.823 bp:dialog (transmit) [R-0dsgjgjalgd54kybNov] execute action “basic-skills/call_api”{ botId: ‘transmit’ }, +2ms
09:42:58.275 bp:dialog (transmit) [R-0dsgjgjalgd54kybNov] eval transition “always” to [#]{ botId: ‘transmit’ }, +452ms
09:42:58.276 bp:dialog (transmit) [R-0dsgjgjalgd54kybNov] transit (skills/CallAPI-02aa28.flow.json) [entry] >> (error.flow.json) [entry]{ botId: ‘transmit’ }, +454ms
09:42:58.277 bp:dialog (transmit) [R-0dsgjgjalgd54kybNov] render element “#!builtin_text-error”{ botId: ‘transmit’ }, +2ms
09:42:58.281 bp:dialog (transmit) [R-0dsgjgjalgd54kybNov] ending flow{ botId: ‘transmit’ }, +5ms

how can I create a loop ? is this not the proper way to do this ?

Hey @smalldragoon ,

The issue you are experiencing is caused by the infinite loop detection mechanism that was introduced in Botpress a couple months ago.

Basically, if a node is repeated 3 or more times, an infinite loop is detected:
2020-01-09 at 10.58 AM

To fix this, you have 2 possibilities:

  1. You create a custom Action that will call your API endpoint and sleep 5 seconds, then retry calling the API endpoint if the result is still pending. In other words, the loop will be strictly inside your custom Action

  2. This is a hack: you could, through a custom action, clear the event’s stacktrace. This would break the infinite loop checker, which uses a heuristic based on the number of times a node appears in the stacktrace. In other words, if the stacktrace is empty, the loop checker will never kick in. I do not recommend this method.

Hope this helps!

hi @spgin, thanks a lot for your support.
You answered to my previous post on which I was stuck to create a delay in a custom action due to error with module loading ( for ref and follow in forum, post here : Issue with node.js module - Sleep )
if you recommend to avoid method 2, let’s focus on method 1 :smiley:
Waiting for your feedback on other thread for module- adding
Thanks a lot again !

for reference, final solution here : How to add an external module ( ex : system-sleep ) to package.json?

Hi , I’m finally back to this post as option 1 has not exactly the same behavior than the API call .
I searched in the doc and do not find reference on how to clear stack trace. I know it is not what you recommand but it may work … ( I will post in feature request as well )
Coudl you point some documentation on clearing the stack trace ?
Thanks !