Getting & Setting Data

Since each interaction your bot has with a user will be unique, your bot may need to remember information specific to a user. After all, it’s quite rude to forget someone's name right after they told it to you!

Storing User Variables with <set> and <get>

The <set> and <get> directions allow you to save data associated with a user to the bot's "brain." We'll often refer to this data as user variables. Consider this script:

+ my secret password is dog
- Oooh... I love a good secret. <set password=dog>
+ what was my secret password
- The secret password is: <get password>
+ forget my password
- Ok, forgetting the password <set password>

Once somebody says remember, the bot will remember that password is now dog. Now whenever you use <get password> in your script, that entire direction will be replaced with what the bot has saved. Anything that had not been set previously will come back as undefined. If you do not <set password> to any value, the password will be set to undefined instead and the previously saved data will be cleared.

If you just say <set password> without a value, the password will be set to undefined instead, and the previously saved data will be cleared.

Variable names are case-sensitive!

<set> and <get> behave with respect to an individual user. In the example script above, if Michael and Linda are users messaging the bot, but only Michael had said "remember" at some point, the bot's responses to these two will be different. The bot will tell Michael The secret password is: dog but tell Linda The secret password is: undefined.

You cannot save data to variables with names that start with an underscore, like _name. Only special Dexter namespace variables can begin with an underscore.

User Messages via <star>

The <set> and <get> directions are most useful when the bot is remembering and recalling messages that your users have sent your bot. The <star> direction in a response is replaced with the user's message:

+ what was my password
- The password is: <get password>
+ my secret password is *
- I'm remembering the password is <star> <set password=<star>>
+ *
- Hey! You said <star>
Capturing multiple <star> values

1) If you have a trigger with multiple * in them, you can use <star1>, <star2>, etc. to distinguish between them:

+ * and *
- you said <star1> then you said <star2>

2) If you are using a trigger with alternatives, the matched value in the set of possible alternatives are captured in <star> values.

+ i (like|love) *
- Oh, I hear you <star1> <star2>.

In this scenario, a user types "I like puppies", then <star1> captures "like" and <star2> captures "puppies". This does not apply to optional brackets, like so:

+ i [like|love] *
- Great, <star> are my favorite too.

In this scenario, the bot will only capture "puppies" in <star>, it will not capture whether the user "like"s or "love"s puppies.

<star> and <star1> are the same value. Try using <starN> for multiple values of "N".

Capitalization and Other Cases

Sometimes you need to respond to the capitalized-versions of what the user has said.

Make use of the <formal>, <sentence>, <uppercase>or <lowercase> tags, which will change the case formatting of the <star> text:

Learn more about tags in the Rivescript documentation here.

Bot Variables

Dexter also supports the Rivescript concept of bot variables. These variables describe important pieces of information about your bot and are "global" to your bot. You can use bot variables in your responses.

To get the value of a bot variable, use the same angle-bracket syntax with the word <bot> and the name of the bot variable, like so: <bot varName>.

There are two ways to set a bot variable:

// Set a bot variable like this
! var name = Ice Cream Bot
+ what is your name
- I am <bot name>!
// Or set a bot variable like this
+ i want to call you *
- <bot name=<star>> Okay, I am now called <bot name>.
For those acquainted with Rivescript, global variables like <env> are not supported on Dexter.