NLU chatbots are getting popular among businesses because they provide a better understanding of the websites, applications, etc. to the users. Companies train their bot in natural language to provide customer support at all times. Moreover, it boosts user engagement through a smooth user interaction experience and gives them quick access to all the app or website’s services. Additionally, an intelligent bot can give proper recommendations to the users and help them in their essential tasks. For instance, Alexa and Siri are great examples of smart voice bots.

  1. What is Dialogflow?
  2. Creating an Agent
  3. Configuring an Agent
  4. Creating an Intent for Agent
  5. Training an Agent
  6. Entities for an Intent
  7. Action & Parameters
  8. Data Extraction
  9. Fulfillment for an Intent
  10. Writing Functions in the Fulfillment Section
  11. Connecting Function to the Intent

Dialogflow Chatbot

What is Dialogflow?

Dialogflow is Google’s chatbot platform for creating conversational AI systems that work on natural language processing. Dialogflow has Machine Learning as its backbone implementation. These systems include the IVR system, chatbots, and voice bots, trained in many languages.

In this step-by-step tutorial, the user will be building a chatbot for a tech-solutions company using Dialogflow.

Creating an Agent

1. Starts by logging into https://dialogflow.com/ using the Gmail account and click on the Console to get to the main Dialogflow page.

2. Click on the Create Agent button to create an agent.

Dialogflow Chatbot

3. An agent is a term to refer to a bot. In this tutorial, the user names his agent as Avatar. The agent name can have no spaces. A bot requires a primary language and default time zone in order to function. In this case, the parameters are English and (GMT+5:00) Asia, respectively. Users can now click on the Create button to create an agent.

Configuring an Agent

1. After creating the agent, an Intent screen appears before the user. Intents categorize the user’s expressions and map them to the appropriate bot action. It is essential to know the basic conversation flow of a chatbot to understand the concept of intents. It starts with the user giving a voice or typed input to the bot; the bot parses it to understand the user’s intention and returns the appropriate response accordingly.

2. In this flow, intents play the role of matching the input to the pre-trained phrases and returning responses.

Dialogflow Chatbot Tutorial

Creating an Intent for Agent

1. The picture above shows the two intents: Default Fallback Intent and Default Welcome Intent. These two intent gets triggered when the user types in “Hello.” The user starts by creating a custom intent by pressing the Create Intent button at the top right.

2. Provide a name to newly created intent. In this tutorial, the user is making a simple bot for a tech solutions company. The name for the current intent is company_intent, which will be trained to give the company’s introduction to the bot users. Furthermore, different sections are to required to create an intent. Section details are:

    • Context: It links the different intents together and carries the required information to other intents.
    • Event: Events trigger the intent from within, without any input from the user.
    • Training Phrases: The user provides some training phrases for the bot’s intent to look for them in the input expression to invoke a specific intent.

3. The users can give any names to the context and events because the agent does not couple them with any entity over the process.

Dialogflow Chatbot for Beginners

Training an Agent

1. Click on the training phrase to open up this section and write down some expressions for this intent. In this case, the user wants this intent to be trained in terms of introductory words. The trained phrases can be as follow:

    • Your services
    • Purpose of the company

Whenever the user inputs a query such as  “tell me about your services,” the bot automatically invokes this intent.

Dialogflow Chatbot Explained

2. The user can see a section for text responses below the training phrases. Users can add more than one response to an intent. Based on multiple responses configured against an intent, the bot will randomly choose one of them to reply to the user.

3. Moreover, there is a toggle button below the response section, which says: “Set this intent as the end of the conversation.” The user can enable it to mark some responses as the end of the conversation, for instance, Goodbye.

Dialogflow Chatbot 101

4. Press the Save button to save the intent. Then write the sentences containing the training phrases in the bot interface at the right, and the bot will return a text response accordingly.

Dialogflow Chatbot Advanced

Entities for an Intent

1. The intents above explained the uses of intents to give a response to the users. However, they cannot extract useful information from the user input. Entities help to extract the data such as dates, numbers, ages, from the user’s expression to give an appropriate reply. For instance, if a company provides various services, and a user asks about a particular service, the agent needs to extract that service’s name from the user’s text.

2. An entity can be created for the services to extract data from the user-provided information. Users can create an entity by clicking the Create Entity For this example, the name will be “services.”

3. Add the names of all the services provided by the company and their synonyms in each row. The agent is case sensitive, so the user should add the words in both cases, i.e., lower and upper case, to make sure all the user input bases are available by intent. Once things are in order, press Save to continue.

Dialogflow Chatbot Account

4. Create an intent and name it “Services.” The user can add additional training phrases from available services. In most cases, the agent itself highlights the entities relevant to its name; however, there is an option to check out the available services as per the requirement.

Dialogflow Chatbot Tools

Action & Parameters

1. The Action and parameters section below will start showing the parameter names and values immediately. The user will have to select the “Required” as the agent needs the service name to provide a valid response to the customer’s query.

Dialogflow Account

2. Click on the prompts to add, “Please be specific about the services. Thank you“, to ask the user to mention the service about which he needs information. With this, whenever a user inquires about the service’s details without specific service information, the agent will respond with “Please be specific about the services. Thank you.”

Dialogflow

Data Extraction

1. When the bot extracts the data from the customer’s input, the entity can use the relevant in the responses by using the ‘$’ sign with the entity name. For instance, the bot can be configured to respond with, “This $services includes ……” to describe the services.

2. In this scenario, the user writes the “$services” in the response section and tests it in the bot interface.

3. Once the intent is saved and trained appropriately, a test can be performed over the bot to see how it operates. For a sample, the customer queries the bot by asking, “Tell me something about your marketing services.” The bot will store the “marketing services” as an entity and respond accordingly.

Fulfillment for an Intent

  1. The response section in the intent page can easily manage static responses to the matching intent, as seen above. However, it is incapable of modifying the responses or can reply intelligently to the users’ queries. For this, Dialogflow has a fulfillment section in which users can write their functions in Node.js.
  2. Click the fulfillment section in the left navigation bar, and then enable the fulfillment button. Please note that the users should have a paid subscription in order to use this section.
  3. Uncomment the below code in the index.js file in the fulfillment section.
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

Writing Functions in the Fulfillment Section

  1. Write the function name and pass the agent to it to declare a function. Additionally, Dialogflow does not require the users to mention the return type of the function explicitly. However, the users can choose to return the data they want from the functions.
  2. The users can extract the data from the input or choose to perform some operation in the function and then produce the agent’s desired results to show them to the user. For instance, in the example below, “agent.parameters.services” extracts the stored data in the “services” entity of the intent, which in turn takes the data from the user input.
  3. The users can write their functions based on the example below. In this example, a constant variable n stores the user-provided value of the “services” parameter. The if and else conditions below check that if the n is graphic designing, the agent sends a  message “We design posters, social media posts, banners, etc.” to the user. Else if the n is marketing or digital marketing, the agent sends a message, “Our company specializes in search engine optimization (SEO), pay-per-click, Digital Strategy, Content & Social media marketing, and Google Data Studio” to the user.
  4. The users can also provide suggestions to the agent users to reply by choosing that suggestion. For instance, in the example below, if the user asks about the company’s marketing or digital marketing services, the agent replies with a brief introduction to the services. However, it shows a “Read more” suggestion to the user. The user can press that suggestion to know more about it.
function services_handler(agent) {
    const n = agent.parameters.services;
 //   agent.add(n);
    if (n == "graphic designing"){
      agent.add(`We design posters, social media posts, banners etc.`);
    }
    else if (n == "marketing" || "digital marketing") {
      agent.add(`Our company specializes in search engine optimization (SEO), pay-per click, Digital Strategy ,Content & Social media marketing and Google Data Studio`);
       agent.add(new Suggestion(`Read more`));
    }
  }

The suggestions are also the intent of the agent. Therefore, create a follow-up intent in the intent section and add the suggestion “Read more” in its training phase. Finally, the user can train the agent to respond to that intent.

Connecting Function to the Intent

Below is the code to connect the functions to the respective intents. The users do not need to create a new Map for every function but can call the map class’s set method and give the intent name and the function’s name as its argument to connect them both. Finally, the handleRequest method of the agent takes the intent map and maps the functions on the intents.

let intentMap = new Map();
  intentMap.set('Services', services_handler);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);

The above samples show a fundamental Avatar agent capable of introducing the company and talking about its services. The developers can build many complex avatars using similar techniques, which can be quite sufficient to boost customer service and support through an automated and efficient approach.