DEV Community

Smit Jethwa
Smit Jethwa

Posted on β€’ Edited on

2 1

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

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

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

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 '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);
Enter fullscreen mode Exit fullscreen mode

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 reading! For any doubts, feel free to connect with me on Twitter- smitjethwa

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay