Evaluating Expressions with Converse.AI Extensions — Part I

Graeme Cassels
State of the Conversation
3 min readNov 30, 2017

So you have a Converse.AI account, you have the Converse.AI CLI tool and you’ve created some very cool extensions that you are reusing across multiple templates. You’re rocking it! 👍 😎

But now you’re thinking: “Oh gee, I just want to evaluate a simple expression in one template. Do I have to build a whole extension just for this task?”

NOPE!

Well, actually, yes! But only once — we’ll build a module that allows you to execute unique javascript expressions on the fly using Node.js’ VM module.

Simple Expression

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

After checking if input is not undefined we’ll add the following code.

// Use the VM module to evaluate the expression.
var script = new vm.Script(input);
var result = script.runInNewContext();
// Send the result back to Converse.AI
var response = new ModuleResponse();
response.setValue({result});
app.send(Status.SUCCESS, response);

Saving and deploying this code allows us to evaluate expressions on the fly. We can even use handlebars in the expressions because handlebars are evaluated before sending our expression to our module.

The Simple Expression module’s input definition on the left, and on the right is the results printed into the chat interface.

Printing out the result of this Simple Expression module we can see that the user’s first name has been concatenated with the result of one hundred divided by ten. In this case the evaluation is Graeme10.

In the screenshots above the handlebar is evaluated before sending the entire expression to our module as the value for input. The expression that our module will actually evaluate is: "Graeme" + (100/10) which is evaluated and returned as Graeme10.

What’s Next?

While there are good reasons for this kind of module to exist there are also considerable disadvantages to using expressions like this. Including but not limited to: No external packages, big performance disadvantages, no syntax highlighting, no error checking or testing, no source control management, prone to mistakes, lack of reusability and many more.

For these reasons, I recommend anything more than a one line expression should be made into a fully developed Converse.AI module.

In Part II we’ll look at evaluating conditional expressions and rerouting the conversation based on the condition.

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