Switching between states
A chatbot script is defined in a text file with a tree structure, where some elements are nested into others. These elements are the bot states which it can switch to in the course of a conversation. More general states include more specific ones.
State switching cases
State switching happens in the following cases:
-
The user sends a request processed by a global
q!
orintent!
tag, or an event occurs which triggers anevent!
tag. Switching to a state by a global tag is possible from any other state, except for modal states. -
The user sends a request processed by a local
q
orintent
tag, or an event occurs which triggers anevent
tag. Switching to a state by a local tag is possible from the nearest parent, sibling, or children states. -
A request or an event is processed by a local
q
,intent
, orevent
tag with afromState
ortoState
parameter. This parameter explicitly sets a state from which or to which switching by this tag is possible. -
A
go
orgo!
tag is triggered in the state, or the$reactions.transition
method is called.
q
/q!
tags are triggered with a higher priority than intent
/intent!
.
Learn more about pattern and intent priority.Trigger priority
When the bot is in some state, it can switch to others by any trigger tag accessible from the current state. The tags are triggered in a specific order depending on the distance between the current and target states: children states have the highest priority, followed by sibling states, followed in turn by parent states.
Therefore, different states can have trigger tags with the same values, but different tags will be triggered based on the state where the bot is currently in. Take a look at the following script:
patterns:
$Yes = (yes/of course)
$No = (no/not/no way)
theme: /
state: Start
q!: $regexp</start>
go!: ../MorningExercise
state: MorningExercise
a: Do you do morning exercise?
state: EveryDay
q: * $Yes *
a: Do you do it every day?
state: Yes
q: * $Yes *
a: Good!
state: No
q: * $No *
a: Morning exercise should become your habit!
state: No
q: * $No *
a: Morning exercise helps your body and brain, try working on yourself!
This script shows how positive and negative user answers are processed differently depending on the current state. Let’s have a look at how this script is processed:
- The script starts up, the
go!
reaction tag redirects the dialog to theMorningExercise
state. - In
MorningExercise
, the bot asks Do you do morning exercise?. - The bot stays in the current state, where the following patterns are active:
* $Yes *
and* $No *
. - The user says yes and the bot switches to the
MorningExercise/EveryDay
state. The bot then asks Do you do it every day? , and the next set of patterns becomes active. - The bot interprets the next user reply in the context of
MorningExercise/EveryDay
. The replies trigger patterns in nested states, since they have a higher priority.