Advanced - JavaScript

What is a Macro?

Dexter bots can interpret JavaScript to enable some advanced logic that would be hard to write using just standard dialogue scripting in small functions called "macros".

You can define JavaScript functions in Dexter macros using > object your_function_name javascript and calling them using the <call>your_function_name</call> syntax. See the following example:

> object roll_die javascript
    return Math.floor(Math.random()*6) + 1;
< object
+ roll a dice
- You rolled a <call>roll_die</call>
A few comments:

Passing in Arguments

You can even pass in arguments into the macro by including after the macro name, like so <call>macro_name param1 param2 <star></call> can be used within the function you write.

You can access individual elements of the passed-in arguments array by referring to the index number associated with the order of the arguments passed into the macro. Argument arrays are zero-indexed.

You cannot pass <get> or <bot> directions as arguments into a macro. If you would like to access user variables or bot variables inside a macro, make use of the rs.getUservar() or rs.getBotvar() functions described below.

Using Macros in Conditionals

Macros can be used in combination with conditionals to evaluate if something is true before returning a response.

+ roll a dice
* <call>roll_die</call> == 6 => You rolled a 6! You win.
* <call>roll_die</call> == 1 => You rolled a 1! You lose.
- You rolled a <call>roll_die</call> Roll again.

Macro Example

Here's a more involved example that shows off remembering user data and how to read from function arguments:

> object random_number_prompt javascript
    var min = parseInt(args[0]);
    var max = parseInt(args[1]);
    var user = rs.currentUser();
    var wholeNumber = rs.getUservar(user, 'wholeNumbers');
    if (wholeNumber !== 'undefined') {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
      return (Math.random() * (max - min)) + min;
< object
+ use whole numbers
- Ok, I'll return only whole numbers <set wholeNumbers=true>
+ number between * and *
- Here's the number: <call>random_number_prompt <star1> <star2></call>
In the example above, rs.getUservar() returns the string "undefined", not the undefined primitive type.

JavaScript Reference Table

Only a subset of standard functions are available to you. A special rs object is exposed to you that allows for access to data you have saved to your bot, getting a user's current topic, etc.

rs methodsEach of the following methods is invoked like rs.currentUser() within a macro.
string currentUser()Returns the internal user ID for the active user messaging your bot. The return value is this is required as the "user" parameters into other rs methods.
string getUservar(string user, string name)Returns data previously set with <set name=some_data>.

Note: This method ALWAYS returns a string. Calling getUservar with an unset variable returns the string "undefined", NOT the undefined primitive.
string getUservars([string user])Returns all data associated with the user. If no user is passed in, returns all data associated with all users.
void setUservar(string user, string name, string value)Equivalent to <set name=value> in a dialogue script.
void setUservars(string user, object data)Equivalent to calling setUservar(user, key, value) for each key/value pair found in the data object.
string getBotvar(string name)Returns global bot variable data previously set with ! var name = some_data or <bot name = some_data>.
string getBotvars()Returns all global variables associated with the bot.
void setBotvar(string name, string value)Equivalent to ! var name = data or <bot name=value> in a dialogue script.
void setBotvars(object data)Equivalent to calling setBotvar(key, value) for each key/value pair found in the data object.
void reply(string user, string trigger)Forces user to reply with passed in trigger.
Standard JS methods Dexter supports the following standard JavaScript globals: clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout
Available JS libraries Dexter allows access to the following Javascript libaries: lodash, moment, and moment-timezone.

More Resources

For more on this, learn more [at this github]](