I collect data from user input and to finish I want to calculate a value based on that input.
For example, I collect the person's weight and then the height to calculate the person's BMI, General Flow. How can I calculate BMI in the last step and show the result to the user?
In addition to jess’ post, here's another approach you might try in order to calculate the BMI based on the user’s inputs. Here are the differences from the first approach provided:
Composite custom entities
This will allow you to create entities wherein you can easily extract the numbers provided by the user instead of getting a string and converting this string to a number in your webhook. With these entities, it’s not necessary to list every other option for height and weight.
Form Parameters
Instead of defining parameters in an intent adding the parameters, you can add the parameters in a page where the agent can interact with the end-user for multiple turns until the parameters are fulfilled.
Here’s the step-by-step procedure to implement these features.
Create composite custom entities in order to collect form parameters from the end-user for the page. You can design your custom entities as follow:
a. Create custom entities for height and weight unit names.
b. Then, create the composite custom entities containing the number and the unit names for each. Note that you should add an alias to ensure that these values will be returned individually.
Create an intent that will be used to trigger the start of the flow. Note to add enough training phrases for what end-users might type or say.
Create a page where you can transition from your Default Start Page when the Intent.BMI intent has been triggered. This page will also be used to collect the form parameters which you can use to calculate the BMI.
Create the flow by adding an intent route for Intent.BMI intent where the transition is BMI page. The flow would look like this.
Now, proceed to the BMI page and add the form parameters accordingly. Make sure to set these parameters as required. Add condition routes as well wherein you can return the response from your webhook once the parameters are fulfilled.
a. The BMI page may look like this.
b. For parameters, here’s an example on how to add these parameters.
c. For condition routes, I added a condition to return a response once the form parameters are fulfilled. If not yet fulfilled, the agent will keep prompting the user for a valid input. I used a webhook to return the responses wherein this webhook extracted the value for each parameter and was able to calculate the BMI.
In your webhook, create a function that will extract the form parameters and calculate the BMI based on these values. Here’s another example using Node.js.
index.js
package.json