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:

Example bot with counter

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.

Note: This example uses messages rather than question interactions. This is because the bot will not advance the conversation until the user has answered the question.

Step 1: Create the interactions

Start by creating the message and questions interactions that will be used by the bot.

Result of step 1

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

Result of Step 2

To create the condition:

  1. Drag an action interaction to the canvas.
  2. Give it a name that describes its purpose. For example Message seen?
  3. Select Conditions from the Action Type dropdown.
  4. 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.
  5. On the Settings tab, add a condition like this:
    Variable Operator Value Next
    {counter} > 1 Tell us more
    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).

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

Result of Step 3

To create the counter:

  1. Drag an action interaction to the canvas.
  2. Name the interaction. For example counter.
  3. Select Set Variable as the Action Type.
  4. Link to the condition interaction.
  5. 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

Result of Step 4

To set up the default response and message:

  1. 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
  2. On the toolbar, click Bot Settings.
  3. Go to the Defaults tab.
  4. 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.
  5. Link the message to the Not Current Range interaction.
  6. Click Save.
  7. Test the bot in Preview.

    Bot preview