Can I make the bot do calculations?

Of course, you can! It’s a bot, what did you expect? ;)

But it does need a bit of an adventurous mind, as you will need to write some JavaScript code!

We will make use of the Script variables.

Step 0: enable developer mode

Step 1: create step variable(s) to be used as input for our calculation:

Step 2: create script variable which does the calculation using the other variables:

function(callback, variables) {
  // The `callback` argument has to be called with the result value in order to support the async operations. The `variables` argument is an object containing other variables
  var number = parseFloat(variables.number.value);
  callback(number * 5)
}

this is the actual calculation:

  • First it takes the input given in Step 1 (a number question in the example);
  • It then multiplies it by 5 in the second line.

Step 2: use the calculations in the bot:

Following the example above is the calculation, which can be added as below (like: {{calculation}}). This variable can be used in the chat texts, response messages, response connections.

Which will then show the calculated value!

Note: if you want to use input from multiple steps, simply add more variables of the type you need.

If you want to save this calculation along with the response and/or use this calculation result in the advanced conditions for the jump logic or the response connections, you can use Goto step with the value setting as {{calculation}}:

Edit this page

Tags
See also