hero-chatbot-tutorial

Conditions

When designing a chatbot, there is often a need for branching paths in the bot's logic. A straightforward example would be presenting the user with a choice (like whether they want to use a discount code or not) and then acting accordingly.

Before we talk about how that can be achieved with a NativeChat chatbot, take a look at the diagram below.

chapter_7-illustration-2x

The above scenario can be achieved in two steps:

  1. A confirmation step - which asks the user a Yes/No question:
    {
        "type": "confirmation",
        "entity": "hasCode",
        "messages": [
          "Do you have a Discount Code?"
        ]
    },
    
  2. A question step with a conditions array - which only shows if hasCode is true:
    {
        "type": "question",
        "entity": "discount-code",
        "entity-type": "",
        "messages": [
          "What is your Discount Code?"
        ],
        "conditions": [
          "{{hasCode}}"
        ]
    }
    

Validation

Conditions can also be used with custom validators, where you can provide a set of expressions to check if the provided user input is valid. For example, you might not want your users to order more than 5 tickets.

{
  "type": "question",
  "entity": "tickets",
  "entity-type": "Number",
  "messages": [
    "How many tickets would you like?"
  ],
  "reactions": {
    "validations": [
      {
        "type": "custom",
        "parameters": {
          "condition": "{{$lt tickets 6}}"
        },
        "error-message": [
          "Sorry, but you can't buy more than 5 tickets."
        ]
      }
    ]
  }
}

Conditional Operators

There are a number of very useful operators that you could use.

Operator $not

The $not operator is a simple negation operator, that changes true to false, and false to true.

Following on the initial Discount Code example, you could use it to show a different message when hasCode is false.

You could do this like this:

{
  "type": "question",
  "entity": "discount-code",
  "messages": [
    "You could sign up to our newsletters to receive a discount code next month."
  ],
  "conditions": [
    "{{$not hasCode}}"
  ]
}

Comparison operators

You could also use a comparison operator to complete the same scenario, and check if:

  • hasCode equals false, like this:
    "{{$eq hasCode false}}"
    
  • hasCode not equals true, like this:
    "{{$ne hasCode true}}"
    
  • $eq - equals
  • $gt - greater than
  • $gte - greater or equal
  • $lt - less than
  • $lte - less or equal
  • $ne - not equal

Operator $in

The $in operator allows you to check if a value is present in an array.

"{{$in myValue myArray}}"

Boolean $and $or

You can also use $and and $or to build more complex conditions, like this:

"{{$and (condition 1) (condition 2)}}"

For example when a price should be less than 20, but also it should be a positive number:

"{{$and  ($lt price 20) ($gt price 0) }}"

Operator $has

The $has operator allows you to check if a specific entity has been provided by a user. This operator comes in handy in many scenarios.

Skip Parent Question

The $has operator comes in handy when looking up an entity requires a two (or more) step process.

For example, in order to find a city, the chatbot asks for a country first, and then based on the country it provides a list of cities. However, if the user already provided the city, then it would be good to skip the country step.

Please, note that the value of the country is used in the validation of the city. So, the city validation should also be updated, so that it only check the city.country when country has been provided.

Follow these steps to update the rent-car conversation:

  1. Find the country question step
  2. Add a conditions array => start typing co and select conditions
  3. Add the following condition => "{{$not ($has city)}}"
  4. Find the city question step
  5. Update Validations->parameters->condition

You need to use an $or operator where:

  • either country is not provided => ($not ($has country))
  • or the city.country is the same as the provided country => ($eq city.country country)

Like this:

"condition": "{{$or ($not ($has country)) ($eq city.country country)}}"
Condition has not city demo

Test

Save and test the rent-car conversation.

Try the following conversations:

  1. Send: I want to rent a car from today to tomorrow
  2. Send: Berlin
  3. You should be asked to choose a car
  4. Select a car
  5. Send: I want to rent a car from today to tomorrow in France
  6. Send: Berlin
  7. You should be shown an error message
  8. Send: Paris
  9. You should be asked to choose a car
  10. Select a car
Loading animation
Save this e-book for later

Loading animation
Would you like a free consulting session?

Want to learn more about NativeChat?

Get started today