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. Dexter exposes a feature called "macros" found in RiveScript, which is the language that Dexter's dialogue scripts are based upon.

You can define JavaScript functions in Dexter within your default topic using > object your_function_name javascript and calling them using the <call>your_function_name</call> syntax:

> object random_number javascript
    return Math.floor(Math.random()*10);
< object
 
+ random number
- <call>random_number</call>

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.

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 JavaScript and RiveScript 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 methodsDexter supports the following standard JavaScript globals: clearImmediate, clearInterval, clearTimeout, setImmediate, setInterval, setTimeout

More Resources

For more on this, learn more about the RiveScript JavaScript interpreter.