Example: using counters and conditions
This example counts the number of times an interaction is shown to the user, then uses a condition to decide whether to show the interaction again or whether to advance the conversation by showing a different interaction. It uses two types of action:
- A counter variable to count the number of times a user has seen the Not current range message.
- A condition to determine the next step based on the value of the counter.
This shows the full example:
The counter is incremented whenever the Not Current Range message is triggered. The following Message seen? condition evaluates the counter. If counter > 1
it advances the conversation by showing the Tell us more question otherwise it automatically redisplays the Favorite flavor message.
Step 1: Create the interactions
Start by creating the message and questions interactions that will be used by the bot.
Result of step 1
The details of these interactions are given below.
Type | Name | When we receive | Send to user | Other... |
---|---|---|---|---|
Message | Favorite flavor | start | Which of these flavors do you prefer? | On the Buttons tab, add a quick reply for each flavor. You only need to enter a label in the Display field. |
Message | Lemon | lemon | Thank you. Have a good day. | |
Question | Tell us more | You said your favorite is "{lastSentText}". If this is wrong, please list your favorite flavors below. | On the Answers tab, select Accept any answer. |
Step 2: Create a condition
Create a condition that will determine the next interaction based on a counter variable that you will create later.
Result of step 2
To create the condition:
- Drag an action interaction to the canvas.
- Give it a name that describes its purpose. For example Message seen?
- Select Conditions from the Action Type dropdown.
- Draw a line to Favorite flavor as the next interaction to go to. The bot automatically goes to this interaction whenever the condition is not met.
- On the Settings tab, add a condition like this:The condition appears as a separate element on the canvas. When the counter is greater than 1 the bot shows the Tell us more interaction because the user has already seen the message twice. When the counter is equal to or less than 1, the bot automatically shows the Favorite flavor interaction again because the user has not seen the message or has only seen it once (so the condition is not met).
Variable Operator Value Next {counter} > 1 Tell us more
Step 3: Create a counter
The next step in this example is to create a counter variable using an Action Type of Set Variable type.
Result of step 3
To create the counter:
- Drag an action interaction to the canvas.
- Name the interaction. For example counter.
- Select Set Variable as the Action Type.
- Link to the condition interaction.
- On the Settings tab, enter the JavaScript to define a counter variable. For example:
if (context.counter) { context.counter = context.counter + 1; } else { context.counter = 1; }
Step 4: Set up the responses that will be counted
The last step in this example is to set up the default response and message that will be counted. The default response is shown to the user whenever they enter a message that is not understood. The message is shown immediately after the default response, and it is this message that is counted. You can vary this message by adding random responses.
Result of step 4
To set up the default response and message:
- Create the message that is shown immediately after the default response:
Name of interaction When the User Says Respond to User Next Other... Not Current Range caramel, mint, pistachio We don't make that flavor Select the counter interaction. Optionally, on the Advanced tab, add some random responses to send to the user - On the toolbar, click Bot Settings.
- Go to the Defaults tab.
- In Default Question/Answer Reply, enter the text that you want to send to the user whenever their answer to a message is not understood. For example, I'm sorry.... This answer will be used by all the interactions in this bot.
- Link the message to the Not Current Range interaction.
- Click Save.
- Test the bot in Preview.