Manage Inputs in Dialogflow
Tags: dialogflow, actionsongoogle, chatbot, webhook
Published: 2020-03-14 21:11:38

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 the 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:
![Image1](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/input%20blog%2F1.PNG?alt=media&token=d5637883-e4e3-43ee-b253-f1ef854fd5cf)

In the above snapshot,
___noon___ is automatically identified as sys. date-time 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.

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

![Image2](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/input%20blog%2F2.PNG?alt=media&token=feb82978-de0b-41e0-92e9-e615b7b2880b)

**Step2:**
Define a variable in the training phrases.
![Image3](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/input%20blog%2F3.PNG?alt=media&token=461faedd-49d3-4ab0-a5ed-2b1813f74e53)
An agent will automatically identify and assign the names to the value.

**Step 3:**
In the Actions and Parameter section, 
Tick the checkbox of the REQUIRED option as all the parameters are required to operate.
![Image4](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/input%20blog%2F4.PNG?alt=media&token=78e04928-cf1d-443a-bb04-99fe78d6ffd4)
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 a static response will not work here. So we'll be using Webhook. Enable the Webhook from the Fulfillment section.
![Image5](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/input%20blog%2F5.PNG?alt=media&token=013529fd-0f94-4dc1-b61d-1e2b10fb0939)

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

Code:

``` javascript
'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 'favourite colour'.
// The intent collects a parameter named 'colour'.
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;
  }
  else{
  	conv.close("GOT the error!!");
  }
    // Respond with the result and 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);
```

This function will return the Result and end the conversation.

Output:
![Image6](https://firebasestorage.googleapis.com/v0/b/megahack-146b8.appspot.com/o/input%20blog%2F6.PNG?alt=media&token=a3cd0e4c-2bb8-4b9f-91aa-6cb34df8da93)


I hope, you understand how we take input from the user and perform the operation on the same. Thank you for reading! For any doubts, feel free to connect with me on Twitter- [smitjethwa](https://twitter.com/jethwa_smit)