loading...

Manage Inputs in Dialogflow

smitjethwa profile image Smit Jethwa ・3 min read

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:
Image1

In the above snapshot,
12 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, number entity is already defined (system entity).

Image2

Step2:
Define a variable in the training phrases.
Image3
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.
Image4
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.
Image5

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

Code:

'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;
  }
  else{
    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);

This function will return the Result and end the conversation.

Output:
Image6

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

Posted on Oct 8 '19 by:

smitjethwa profile

Smit Jethwa

@smitjethwa

Hello, I'm a CSE student in Mumbai. I code in Python and JavaScript. I've developed many actions and exploring more in VUI development. Apart from this, I'm a Machine Learning and NLP enthusiast.

Discussion

markdown guide