DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How to Create a Chatbot Using Dialogflow

Learn how to create a chatbot using Dialogflow.

Shashank Bodkhe user avatar by
Shashank Bodkhe
·
Dec. 04, 18 · Tutorial
Like (3)
Save
Tweet
Share
7.14K Views

Join the DZone community and get the full member experience.

Join For Free

Chatbots are artificially intelligent programs by which we can make our websites more interactive for the internet user, get better information from them, and serve them better with the least human interaction. For example, you have an e-commerce site that has many products, and here, the chatbot will help the user to find what they are really looking for by simulating human behavior. Chatbots can be used for hotel booking, flight bookings, selling/buying stocks, etc.

In this tutorial, we will design a chatbot and use webhook to get responses. 

First, get dialogflow access at "https://dialogflow.com/" as shown below and click on "GO TO CONSOLE."

Image title

Now let's have a look at the basic building blocks of a Dialogflow architecture together with how to build the chatbot. 

1) Agent is the Natural Language Understanding (NLU) component that captures user input and helps to convert that data into an appropriate response. This will be the bot the internet user will interact with.

agent

Above is an example of a simple Agent named MyAgent. We can provide a description for it, set time zones, and add other attributes as needed.

2) Intent is the component that maps user input to responses. For example, Welcome-intent will be used for greeting the user with an appropriate response like "Hi," "Hello," "Good morning, how may I help you.".etc.

Image title

There can be many intents as per requirement. In this intent, we have defined the various questions like "what is my address," "what is my contact number," etc.

3) Entities are the components specific to one particular functionality like a product name, address, etc.

Image title

This entity contains balance, contact number, etc. These are the "Resolved values" against the "Parameter name" in intents.

Now let's write the code in node.js: 

4) Code

"use strict";

const express = require("express");
const bodyParser = require("body-parser");
const restService = express();

restService.use(bodyParser.urlencoded({extended: true}));
restService.use(bodyParser.json());
restService.post("/chatbot", function (req, res) {
   if (req.body.queryResult.parameters.account_information == "contact number"
    && req.body.queryResult.parameters.account_information) {
    var speech = "999999999";
  }
  else if (req.body.queryResult.parameters.account_information == "account number"
    && req.body.queryResult.parameters.account_information) {
    var speech = "9999999999999";
  }
  else if (req.body.queryResult.parameters.account_information == "DOB"
    && req.body.queryResult.parameters.account_information) {
    var speech = "1 Jan 2019";
  }
  else if (req.body.queryResult.parameters.account_information == "address"
    && req.body.queryResult.parameters.account_information) {
    var speech = " floor no 1 , Building no 1 , address";
  }

  return res.json({
    fulfillmentText: "fulfillmentText",
    fulfillmentMessages: [{
      simpleResponses: {
        simpleResponses: [{
          "textToSpeech": "textToSpeech",
          "displayText": speech
        }]
      }
    }],
    source: "webhook-sample"
  });
});

I have used the hardcoded values for demonstration, and these values may be obtained from some business logic.

You can push the above code on GitHub and deploy your app on Heroku:

5) Heroku deployment:

herokuOnce the deployment is successful as shown on the screen, configure the fulfillment as below.

6) Fulfillment is the code that gets executed when the dialogflow agent calls business logic:

Image title

Here, provide your deployment URL on which you have deployed your node.js app. 

For simplicity of development and deployment, we will be deploying our code in Node + Heroku.

Now, go to the right-hand side panel of the dialogflow screen and type the question, "What is my address?" Below will be the output:

Image title

You can configure as many questions as you want and design intents and entities as per requirement. Happy learning!

Chatbot Dialogflow

Published at DZone with permission of Shashank Bodkhe. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • A Brief Overview of the Spring Cloud Framework
  • ChatGPT Prompts for Agile Practitioners

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: