Different routing for values that are not less than a specific number
If you want your chatbot to be able to give a different response when a number is greater, equal or less than a specific number, you can use a simple code action.
Let’s say we want our bot to check if the number the user says is either lower than 10, or that it is 10 or higher.
We created a simple flow for that:
The code action in the flow above contains the following code:
async payload => {
if (payload.params.number[0].value < 10)
{
trigger('<10')
}
else
{
trigger('>=10')
}
}
The code action first checks if the number is less than 10, and if so, it will trigger the <10 event. Else, it will trigger the >=10 event.
Don’t forget to change the event names for your own event names if you copy and paste this code into your own project.
When we test it, you can see it all works now!
Different routing for values that are equal to a specific number
In the last example it’s a bit weird to respond with “10 is 10 or more.”. Let’s add another event to the flow for when the value is equal to 10:
The code action now should look like this:
async payload => {
if (payload.params.number[0].value < 10)
{
trigger('<10')
}
else if (payload.params.number[0].value === 10)
{
trigger('=10')
}
else
{
trigger('>10')
}
}
Value is NOT equal to
It also works the other way around. The example above illustrates you can use “payload.params.number[0].value === 10” to check if the parameter is 10, but you also have the possibility to check if it is not equal to 10.
To do that, simply replace “===” with “!==”.
Values in a range
You can also check if the value is in a range (between 2 specific numbers).
To illustrate this, we created another flow:
This code action contains the following code:
async payload => {
if (payload.params.number[0].value >= 10 && payload.params.number[0].value <= 20)
{
trigger('in_range')
}
else
{
trigger('not_in_range')
}
}