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 other tags available here.

Bot Variables

Dexter also supports bot variables. These variables describe important pieces of information about your bot and are "global" to your bot. This means that every person that talks to your bot will see the same thing—this is dramatically different than user variables described above, where every user may see a different value. Some common uses of bot variables include:

Getting Bot Variables

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

There are two methods to set a bot variable.

Setting Bot Variables: Method 1

Use ! var in your script to set a variable and its value. This method is most useful for controlling global "settings" such as API keys and flavors of the week. Its value will change whenever you publish any topic.

! var flavor_of_the_week = Mocha
+ what is the flavor of the week
- This week's flavor is <bot flavor_of_the_week>! Show this conversation at the cashier to get a discount.

Setting Bot Variables: Method 2

Just like user variables, you can set a bot variable dynamically in the middle of a conversation with angle brackets. This is more useful when you want to be able to dynamically change these global settings based on user interactions—imagine a bot-wide trivia game, where you'd set the questions and answers via bot variables.

+ eiffel tower
- Correct! Updating the question for everyone... <bot quiz_question=Where is the Statue of Liberty?> <send> Your next question: <bot quiz_question>

For the best experience and avoid conflicting variable values, please choose only one method to set bot variables. Do not allow the same variable to be set via both Method 1 and Method 2.

Bot variables will always reflect their last-altered value, meaning if you publish a topic and subsequently change it via <bot flavor_of_the_week=Chocolate>, the variable's value will reflect the second value.

This is why we strongly recommend using either method 1 or method 2, not both.

March 26, 2018 Update

Prior to this date, changes you made to variables set with Method 1 (e.g., ! var flavor_of_the_week) would not have overwritten values set with <bot flavor_of_the_week=Mocha> when you published. Starting on March 26, 2018, every time you publish a topic within a bot, the ! var flavor_of_the_week values will overwrite previous ones. Please follow our best practices above around choosing either Method 1 or Method 2.

Global variables like <env> are not supported on Dexter.


My bot isn't returning data when I use a <get> tag.

Try the following suggestions: * Double-check that your <get> tag is formatted properly. If the bot is responding with <getvarname> you may be missing a space between the <get> and the varname. The proper syntax should be <get varname>. * Has this variable been set to an empty string? If so, <get> will return an empty string.

Why is my bot returning "undefined" when I use a <get> tag?

Has this user variable been set? If the bot has not been set in the past, the variable will return "undefined". Did you recently remove the value it was set to? When a variable is reset (like so <set var>), the variable will return "undefined".

Why is <set var = abc> not setting the variable to a value?

There should be no spaces before the equal sign.