Evaluating Expressions with Converse.AI Extensions — Part II

Graeme Cassels
State of the Conversation
4 min readDec 7, 2017

In the last article we looked at evaluating simple javascript expressions on the fly. A useful feature to have access to and ultimately pretty cool. 😎

But now you’re thinking: “I want to reroute the conversation based on the output of this expression. Do I have to add another module or junction?”

NOPE!

And there’s no but this time.

Simple Condition

What if we want to reroute the conversation based on the result of a simple expression. I.e. what if you want to go down a separate path if a user’s first name begins with the letter “G”.

Of course we could use our “Simple Expression” module from the last article to evaluate whether the username begins with “G” or “g” and run the result through a Conditional Check module matching for the value true.

var firstchar = "{{user.firstname}}".charAt(0);
firstchar === 'G' || firstchar === 'g';

When evaluating multiple expressions the last expression is the one returned. No return keyword is necessary.

There’s nothing wrong with this approach. However, allow me introduce you to a feature in module development called “Exits” that may help us streamline this process.

Exits define the module’s exit routes in a template — an exit route is shown by the grey path in the designer after each module, state, or junction. In the image below, the junction has one entry route and three exit routes. Only one exit route can be executed at run-time.

A junction with one entrance route and three exit routes.

A similar process can be done when developing our own modules.

Again, using the Converse.AI CLI tool, let’s create a module called simple_condition, and add one parameter called input of type TEXT.

The Converse.AI CLI tool isn’t capable of defining Exits just now so before writing the code let’s jump into the plugin.json spec and modify it to allow two exit routes — “True” and “False”. Look for your simple_condition module in the spec, and add the following property to enable the two exits:

"exits": [
"True",
"False"
],

The module’s spec should now look something like:

{
"id": "simple_condition",
"name": "Simple Condition",
"description": "Evaluate a simple expression and reroute path accoridingly.",
"hasReturn": false,
"exits": [
"True",
"False"
],

"param": [
{
"param": "input",
"displayName": "Input",
"description": "The expression to evaluate.",
"type": "TEXT",
"optional": false
}
]
},

Which would create the following in the designer.

Simple Condition module with two exits — True and False–these are only placeholder states, you must add a state to the placeholder exit if you want to continue the conversation.

If we jump back into the code for this module check that input is not undefined and add the following, save, and then deploy, we can evaluate conditional expressions on the fly.

// Use the VM module to evaluate the expression.
var script = new vm.Script(input);
var result = script.runInNewContext();
// Set the Exit based on the result
var response = new ModuleResponse();
response.setExit((result) ? 'True' : 'False');
app.send(Status.SUCCESS, response);
The Simple Condition module’s input definition on the left, and on the right is the conversation rerouted to the Is G state.

Using the expression from above we can evaluate if the user’s first name begins with the letter “G” on the fly and reroute the path to the “Is G” state or “Not G” state.

What’s Next?

As mentioned in the last article there are good reasons for this kind of module but there are also considerable disadvantages over using expressions.

As a programmer, the “Simple Expression” & “Simple Condition” modules are two of my favorite, and most used, modules. If you want to take it further you can have a look at apply a scope/context to the module.

To do so, just replace the line:

var result = script.runInNewContext();

With:

var context = vm.createContext(scope);
var result = script.runInContext(context);

Where scope is another parameter.

Take a look at the code for this module (and many more) in the converseai-extensions github page. Or dive into the Converse.AI Extensions docs to create some unique modules of your very own — feel free to share them with me.

--

--

Graeme Cassels
State of the Conversation

Software Engineer @Smartsheet working on @Converse.AI | Forever a student of film | Snowboarder | Adventurer