Creating a Loop in WhatsApp

Cesar Banchio Updated by Cesar Banchio

Sometimes we want a certain part of the flow to be repeated several times. On these scenarios, we can create a loop. To create a loop on WhatsApp, a requirement on the flow is to have a user input. A simple case can be viewed below:

The flow is divided in 2 parts, the loop and the rest of the bot. On this scenario, we will use the iteration variable to set the amount of times we want to loop, and will be used in a conditions block to decide if the loop should continue or not.

The first jump to block will be set to send the user to the message block "Welcome to the loop"

A closer look to the loop part of the flow:

At the beginning of the flow, we will use a formula block to subtract 1 to the iteration variable (very important, otherwise, we will be on an infinite loop), and then we will check at the end of the flow if this variable equals to 0. If so, the loop ends and we can go on with the flow. Make sure you have a user input in the flow, on this example is the buttons block, otherwise we will get a closed loop error.

The formula inside the formula block is:

Subtract(@iteration, 1)

If it doesn't equal to 0, we will use a jump to block to send the user to the formula block:

How did we do?

How to calculate the number of days between two selected dates (WhatsApp)

Recognise the users input when sending a Message Template with buttons

Contact