Creating Loops
Video tutorial available in the Zapbot dashboard
In this guide, we demonstrate how to implement a dynamic loop to collect guest names for a restaurant reservation chatbot.
Overview
We walk through setting up the chatbot to request the number of guests, and then, using JavaScript, create a list based on this number.
How It Works
- Ask for count: Request the number of guests from the user
- Create a list: Use the 'Shift' set variable value to iterate through this list
- Collect information: Store each guest's name through the loop
Implementation
Despite the lack of a specific loop block in the platform, you can create loop-like behavior through custom code using the Set Variable block.
Key Concepts
- Use JavaScript in the Set Variable block to manage iteration
- The 'Shift' operation removes and returns the first element of an array
- Connect blocks to create the looping behavior
- Use conditions to check when the loop should end
Example: Guest Name Collection
- Ask "How many guests will be attending?"
- Store the number in a variable
- Create an array with that many empty slots
- Loop through each slot, asking for a guest name
- Store all names in a results array