State
This document's purpose is to give an overview about State objects and how they are used in Flows
Introduction
During a conversation there is often a need to store some values somewhere and use them again on a later stage. This could be for example keeping the name of your user and then using it later to personalize messages like Thanks, {name}!
. Or you might want to ask your users a sequence of questions and then push them to your database.
For this cases and others we use State. The State object is part of the Flow and allows you to store and manage key-value information as you go, and reuse it later in messages or functions.
Following this document will give you a brief overview about how to set and read data from State objects.
The State object is managed separately for each different flow, and is being cleared upon redirecting the conversation to another Flow.
Store Data
Any data is stored in the State object as a key-value form.
By default, Nodes output are stored automatically in the State with under the generated key node.identifeier
. For example, consider the following prompt node:
One this node execution is completed (aka the user has given a valid input), the output object of this Node will be stored in the State under the key state.get_username.text
with the value presenting the username.
Set command
You may use the set
command to store custom values in the State. set
command can be invoked when routing between Nodes in the same Flow:
Manage lists in State
Use append
operator to add store multiple items as a list in the State:
Read Data
Messages
Use the binding operator {state.key}
to bind variables from the state to messages you send, for example, consider the following node greeting_node
In this case we bind the value under the key username
from the State to the message text.
Bind Params
State values can be also bound to the params
object, which is used when across Nodes as for instance when invoking a function:
In the above example, then function create_user
will be invoked with the parameter user_name
and the value will be as
Functions
When invoking a function inside a Flow, a snapshot of the State object is passed to the invoked function. You can then manipulate the data and operate on it as you need.
Last updated