Beginner’s Guide to Creating Chatbots Using Dialogflow (Api.ai)

DZone 's Guide to

Beginner’s Guide to Creating Chatbots Using Dialogflow (Api.ai)

Learn how to create a simple chatbot using Dialogflow and walk through an outline of the chronological flow of using Dialogflow.

· AI Zone ·
Free Resource

In this article, I will show you how to create a simple chatbot using Dialogflow. You will find here details about the tools and technology used to do this. This article also has a walkthrough of Dialogflow (previously API.ai) and outlines the chronological flow so that by the time you finish reading, you will have a basic idea of how to create a chatbot like RepoFinder.

Why You Need A Chatbot

Gartner predicts that by 2020, 85% of customer interactions will be managed without a human.

Google also declared an imminent shift from a mobile-first world to an AI-first world. Companies across various industries are riding the wave and have deployed AI-powered chatbots for their businesses. Chatbots, if they can fulfill their potential, will be much more sophisticated than traditional apps. They can understand natural human language, by virtue of which, chatbots will be much more interactive and engaging. As AI improves in the future, talking to a chatbot will be like talking to someone who has the empathy of a real human and a brain reminiscent of Google.

Now let’s get back to building a bot.

Tools And Technology Used

  • Dialogflow: Bot framework to create intelligent chatbots, which you can then integrate with your apps.
  • Node.js: To define the fulfillment logic, which eventually processes the data.
  • Applozic chat as a service: Chat interface to query the bot and display the resulting output.

Now, let me help you get familiar with Dialogflow — but first, a little about the project, as I will be drawing inferences from it in the course of this article:

Creating Your First Chatbot: RepoFinder (Project Name)

RepoFinder helps you find open source development libraries from GitHub based on your input.

Creating Agent

The first step is creating an agent, which essentially is the bot you are building. You can create one through the console by following these instructions. In our case, we’ve named it RepoFinder.

Creating Intents

Next, you will have to create Intents. Intents basically help the bot perceive the user’s input and decide the subsequent action. Intents can be created either from the console or by calling APIs.

Dialogflow, by default, gives two intents: default fallback intent and default welcome intent.

More information on creating intents is here.

Creating Entities

Once intents are created, you need to define entities. Entities are powerful tools used to extract parameter values from the user’s query. Any actionable data that you want to get from a user’s request should have a corresponding entity.

Consider our agent RepoFinder. If the user says, “Tell me the best Chat SDK on GitHub,” this should tell the agent that the user needs some info on Chat SDK.

So how do you configure the agent to do it? Well, for each user expression mapped to an intent, the agent needs to figure out the respective input that the user wants info about. The agent does this with the help of entities.

So for each intent that you create, every user expression should contain a corresponding entity, which your bot agent needs to figure out. Now by default, the agent can’t do that — you need to train it to do so.

Training Agent

Dialogflow provides a training tool that allows you to add annotated examples to relevant intents in bulk (see screenshot below). It helps to improve the classification accuracy of the agent. Here, you will also receive a log of all the queries sent to your agent and what the agent responded with in return. This is very useful if you tell your agent something and it responds with an output you don’t like. It can also be helpful if you realize later on that you forgot a synonym of an entity and users are using it a lot. You can go tell your agent what to do in that case.


Now your bot is all set to function. Every time it receives a query, it will first capture the intent and then extract the entity. The next step is to generate a response that the user is ideally seeking. You can do this by leveraging webhooks to fetch data from external sources (GitHub API server, in our case). This you can do in the Fulfillment Section by specifying the webhook URL.

The penultimate step is to tell the intent to use this webhook to respond with the data that was returned from it.

Integrating RepoFinder in Application/Website

The image below demonstrates the integration architecture and the sequence of information flow:

  1. User comes to the website and asks for the required library.
  2. The website uses Applozic Chat APIs to send the message to the application server.
  3. The application server sends the query to a Dialogflow agent.
  4. The agent uses machine learning algorithms to match the user’s requests to specific intents and uses entities to extract relevant data from them, thereby processing natural language to convert it into actionable data.
  5. RepoFinder returns the actionable result to the application server. A part of a response from RepoFinder looks like this:
    { “parameters”: { “keyword.original”: “chat”, “keyword”: [“chat”] }}
    Dialogflow agent’s response with all the details available here.
  6. The application server calls GitHub Search APIs/internal database to get the list of libraries related to the keyword.
  7. The application server then sends the list of libraries back to the user again by calling Applozic APIs.

RepoFinder Is Ready To Help You

You can check out RepoFinder here.

You can get the full project with source code on GitHub.

ai, bot development, chatbots, dialogflow, tutorial

Published at DZone with permission of Suraj Kumar . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}