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.
The above scenario can be achieved in two steps:
{
"type": "confirmation",
"entity": "hasCode",
"messages": [
"Do you have a Discount Code?"
]
},
conditions
array - which only shows if hasCode
is true:
{
"type": "question",
"entity": "discount-code",
"entity-type": "",
"messages": [
"What is your Discount Code?"
],
"conditions": [
"{{hasCode}}"
]
}
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."
]
}
]
}
}
There are a number of very useful operators that you could use.
$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}}"
]
}
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$in
The $in
operator allows you to check if a value is present in an array.
"{{$in myValue myArray}}"
$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) }}"
$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:
country
question stepconditions
array => start typing co and select conditions
"{{$not ($has city)}}"
city
question stepValidations->parameters->condition
You need to use an $or
operator where:
($not ($has country))
($eq city.country country)
Like this:
"condition": "{{$or ($not ($has country)) ($eq city.country country)}}"
Test
Save and test the rent-car
conversation.
Try the following conversations:
Get started today