Skip to main content

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! or intent! tag, or an event occurs which triggers an event! 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 or intent tag, or an event occurs which triggers an event 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, or event tag with a fromState or toState parameter. This parameter explicitly sets a state from which or to which switching by this tag is possible.

  • go or go! tag is triggered in the state, or the $reactions.transition method is called.

tip
When both patterns and intents are used in one script, the 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:

  1. The script starts up, the go! reaction tag redirects the dialog to the MorningExercise state.
  2. In MorningExercise, the bot asks Do you do morning exercise?.
  3. The bot stays in the current state, where the following patterns are active: * $Yes * and * $No *.
  4. 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.
  5. 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.