In this help doc we will be using MathJS API to develop a simple chatbot that can converts Kilograms(kg) to Pounds(lbs) and adds the two shopping items. This is just an example and you can do any sort of mathematical calculation using this.

Link to MathJS API: https://api.mathjs.org/

Link to video explaining the same:

Let's start

Step 1: Lets start by creating a gambit with no display text and no input UI and lets name it as "init".

Step 2: Create a gambit "total_tomato" which will record "the quantity of tomato in Kilograms"

Step 3: Create a gambit "tomato_api" of "API_Config" type.

  1. API end point URL: http://api.mathjs.org/v4/
  2. Method: POST
  3. Select RAW and add the below JSON
{ "expr": 
[
"{{ursp.total_tomato}}kg*1lbs"
]
}

The JSON above is how the MathJS API takes an input you can find more details for further customisation here https://api.mathjs.org/

Also, please note: you can see that we are converting our total_tomato value which is in Kg(s) to lbs(pounds), {{ursp.total_tomato}}kg*1lbs.

Step 4: Create a gambit "total_potato" which will record "the quantity of potato in Kilograms

Step 5: Create a gambit "potato_api" of "API_Config" type.

  1. API end point URL: http://api.mathjs.org/v4/
  2. Method: POST
  3. Select RAW and add the below JSON
{ 
"expr":
[
"{{ursp.total_potato}}kg*1lbs"
]
}

You can see that we are converting our total_potato value which is in Kg(s) to lbs(pounds), {{ursp.total_potato}}kg*1lbs.

Step 6: Now lets create a final gambit to display the total quantity of both tomato and potato.

  1. Create a gambit, name it "total".
  2. The first message bubble replace "Type your text" with
    {{=tomato=parseFloat(this.pre.tomato_api.result[0]).toFixed(2)}}
    The above is a basic javascript line, we are making sure that the value we have received is a number and allowing maximum of two decimal place for the value.
    ex: if the value is 6.789, .toFixed(2) will set the value as 6.78 and store it into "tomato"
  3. Add another message bubble and replace "Type your text" with
    {{=potato=parseFloat(this.pre.potato_api.result[0]).toFixed(2)}}
  4. Add a final messsage bubble and add the below line
    Total weigh in pounds is {{=parseFloat(tomato)+parseFloat(potato)}}
    basically, we are adding about the values.

That's it. Voila, you have made a simple billing chatbot.

Did this answer your question?