DEV Community

Smit Jethwa
Smit Jethwa

Posted on

Manage Inputs in Dialogflow

Hello AoG Devs!

In this post, I'll explain to you how to take inputs from the user in Dialogflow and perform an action.
In the Dialogflow, Developer can specify the variables in training phrase. If the variables are number, name of a famous place, date, location, time, temperature, etc. A system entity will be assigned to them.

For Example:

In the above snapshot,
12 noon is automatically identified as and if the user enters a time. Likewise, Mumbai is a city and 12% is recognized as a percentage etc.

To explain the inputs in Dialogflow, I've created a simple calculator. In which we'll see how the developer can handle the user input.

Define Entity of Operator. For operands, number entity is already defined (system entity).


Define a variable in the training phrases.
An agent will automatically identify and assign the names to the value.

Step 3:
In Actions and Parameter section,
Tick checkbox of REQUIRED option as all the parameters are required to perform the operation.
PS: Developer can add a PROMPT message. It'll prompt the user if the parameter is missing in the input.

Step 4:
Now, the result will vary according to user input. So using static response will not work here. So we'll be using Webhook. Enable the Webhook from the Fulfillment section.

Step 5:
In the Fulfillment tab, Enable the Inline Editor(Powered by Cloud Functions for Firebase).


'use strict';

// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');

// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');

// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});

// Handle the Dialogflow intent named 'favorite color'.
// The intent collects a parameter named 'color'.
app.intent('Calculator', (conv, {number1, number2, operator}) => {
var answer = 0;
  if (operator=='+'){
    answer = number1 + number2;
  else if (operator=='-'){
    answer = number1-number2;
  else if (operator == '/'){
    answer = number1/number2;
  else if (operator == '*'){
    answer = number1*number2;
    conv.close("GOT the error!!");
    // Respond with the result end the conversation.
    conv.close(number1 +" "+ operator +" "+ number2 + ' is ' + answer);

// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);
Enter fullscreen mode Exit fullscreen mode

This function will return the Result and end the conversation.


I hope, you understand how we take input from the user and perform the operation on the same. Thank you for the reading! For any doubts, feel free to connect with me on Twitter- smitjethwa

Discussion (0)