How to create a chatbot using Dialogflow Enterprise Edition and Dialogflow API V2


In this tutorial, we will learn how to create a chatbot using Dialogflow Enterprise Edition and Dialogflow API V2. This tutorial is an addition to the list of tutorials which covers Google Cloud and its awesome suite of products and services.

Create a chatbot using Dialogflow Enterprise Edition and Dialogflow API V2

By the end of this tutorial, you will have a better understanding of the following.

  1. Introduction to Dialogflow
  2. Introduction to Dialogflow Enterprise Edition
  3. How to create an agent in Dialogflow
  4. Understanding of Intents and Entities
  5. Construct the Customer Service Agent
  6. Fulfillments and Cloud Functions and Dialogflow V2 API
  7. How to create a Customer Service Agent Chatbot
  8. Store the ticket information in Cloud Datastore
  9. Conclusion
  10. References

Since we have a lot to cover in this tutorial, let’s get started.

1. Introduction to Dialogflow:

Dialogflow is an end-to-end development suite for building conversational interfaces for websites, mobile applications, popular messaging platforms, and IoT devices. It can be used to build chatbots, voice assistants etc especially relevant that are capable of having natural and rich interactions with your users.
Dialogflow is also powered by machine learning to recognize the intent and context of what a user says, allowing a conversational interface to provide highly efficient and accurate responses. So at the point of this tutorial, Google offers two versions of Dialogflow.

2. Introduction to Dialogflow Enterprise Edition:

Dialogflow Enterprise Edition is available as part of the Google Cloud Platform (GCP). It provides unlimited text and voice interactions, higher volume usage quotas, and support from Google Cloud support. Dialogflow Enterprise Edition is a premium offering, available as a pay-as-you-go service.
Dialogflow Enterprise Edition is ideal for businesses that need an enterprise-grade service that can easily scale to support changes in user demand.

3. How to create an agent in Dialogflow Enterprise Edition:

In Dialogflow standard edition, you can go to dialogflow.com to create your agent. While, for the Dialogflow Enterprise Edition, we create a project first in the Google Cloud Platform and use the Dialogflow API. Follow the steps below on How to create a chatbot using Dialogflow Enterprise Edition and Dialogflow API V2.

3.1 Project Setup and Authentication:

First of all, we need to create a Google Cloud Platform Project using https://console.cloud.google.com/

  • Create or select an existing GCP project.
  • Enable the Dialogflow V2 API for that project.
  • Click on ENABLE APIS AND SERVICES
  • Search for Dialogflow API in the search bar and click on it.
  • Go ahead and click on ENABLE THE API.
  • 3.2 Create a Dialogflow agent.
  • After enabling the API, click on Dialogflow agent tab and choose an agent name for your project.
  • Agent Name
  • Language
  • Timezone
  • Click on Create button to create a Dialgflow Enterprise Edition Agent.
  • Therefore, for the rest of our tutorial, we would be building up the agent we created on GCP using dialogflow.com. So, click on the Open agent at dialogflow.com
  • Therefore, Once you click the link, it should also ask you to choose your google account. Choose the same google account with which you created the project in the first place.
  • It should now also show you a page to allow dialogflow to view and manage data. Finally, Click on Allow.
  • In addition, Review account settings and click ACCEPT.
  • Finally, you will now see the Dialgflow dashboard and at the time of this tutorial, you can also see a message saying that “Dialogflow API V2 has officially launched. It is now the default API for all new Dialogflow agents and all new features will only be released on V2. Learn how to migrate your V1 agents here.”

3.3 Create a service account.

  • Before we proceed with dialogflow, let us also make sure we have a few more items setup.
  • From the Google Cloud service accounts page, select your project.
  • You should see a screen similar to the one shown below.
  • Download a private key as JSON.
  • From the GCP console, go to APIs and Services and click on credentials.
  • Click on Create credentials and choose Service account key.
  • Select your service account from the dropdown, choose JSON and click Create. This will download the JSON key to your computer. Save it securely.
  • Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the file path of the JSON file that contains your service account key. Open up the Terminal or command prompt window and execute the following command as per your Operating System.

Example: Linux or macOS

Replace [PATH] with the file path of the JSON file that contains your service account key.

export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

For example:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

Example: Windows

Replace [PATH] with the file path of the JSON file that contains your service account key, and [FILE_NAME] with the filename.

With command prompt:

set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

3.4 Install and initialize the Cloud SDK.

  • Follow the instructions as shown in the doc.
  • Finally, following those steps, you should obtain a service account key file (in JSON) that will allow you to authenticate to the Dialogflow V2 API in this Quickstart:
  • gcloud auth activate–service–account — key–file=service–account–key–file
  • Once you have successfully followed the above steps, you would have activated the service account credentials.

4. Understanding of Intents and Entities in Dialogflow:

Intents: An intent represents a mapping between what a user says and also what action should be taken by your software.

Intent interfaces also have the following sections:

  • Training Phrases
  • Action
  • Response
  • Contexts

Entities: Entities are powerful tools used for extracting parameter values from natural language inputs. Any important data you want to get from a user’s request, will have a corresponding entity.

You will also have a better understanding of intents and entities as we progress to the next section of the tutorial.

5. Construct the Customer Service Agent:

Since we are building a Customer Service Agent chatbot, we will also have to first welcome or greet the customer. We can achieve this through the Welcome Intent.

5.1 Welcome the User

  • Click on Welcome Intent.
  • Under the Training Phrases, go ahead an add a few phrases that your users would use to start the conversation.
  • In addition, the next step would be to add in the appropriate response from the chatbot to greet the user and prompt them for a specific question.
  • Finally, Click Save intent. You should also see a popup message saying the agent training completed.

5.2 Create Submit Ticket Intent

  • Click on CREATE INTENT. This will create a new intent where you will have to specify a name for your intent. Let’s call it Submit Ticket.
  • After that click on Training Phrases to add phrases as to what a user might say.
  • Once you have successfully added a few Training Phrases, lets add a response to get more information from the user.
  • Click on Add Response and type in a response to something like this. Sure! I can help you with that. Please provide your name for the ticket.
  • Click Save to save the Submit Ticket Intent.

5.3 Create Follow-up Intent to collect name

  • In order for us to create a follow-up intent. Follow the steps below.
  • Click on Intents on your left navigation panel.
  • Hover the mouse over Submit Ticket intent. You will see add follow-up intent option towards the right side.
  • This should give you a dropdown list. Choose Custom from the list.
  • You should now see a follow-up intent created. Click on Submit Ticket — custom.
  • Let’s change the name to something that makes sense. Change the intent name to Submit Ticket — collect name.
  • Add in few Training Phrases.
  • “I am John”
  • “My name is Sachin”
  • “Am Lucy”
  • You can also see that it will automatically highlight the names as Entities @sys.given-name.
  • Furthermore, by now, under actions and parameters, you should see the following as shown below.
  • Finally, add a response to capture more information from the user. $given-name will hold the name of the customer.
  • “Thanks $given-name! What’s your phone number?”
  • Save Intent to train the agent.

5.4 Create Follow-up Intent to collect phone number.

  • First of all, Hover the mouse over Submit Ticket — collect name intent. You will see add follow-up intent option towards the right side.
  • This should give you a dropdown list. Choose Custom from the list.
  • You should now see a follow-up intent created. Click on Submit Ticket — collect name — custom.
  • Let’s change the name to something that makes sense. Change the intent name to Submit Ticket — collect phone.
  • Add in few Training Phrases. These are just samples.
  • my phone number is + 1231232432
  • mobile number is +1 234234234
  • +1234324234
  • Change the entity from @sys.number to @sys.phone-number.
  • Finally, add a response to capture one more information from the user. $phone-number will hold the name of the customer.
  • “Thanks! Now please describe your issue”.
  • Save Intent to train the agent.

5.5 Create Follow-up Intent to collect the description of the customer’s issue.

  • Same like the previous steps, create a follow-up intent called Submit Ticket — collect description.
  • Add in few Training Phrases. These are just sample data for demonstration purposes.
  • my phone keeps rebooting
  • issue with wifi
  • Bluetooth issues
  • The screen is unresponsive.
  • frozen screen
  • Leave the responses empty. The reason we leave it empty is that we want the responses to be coming from our server after capturing all the information from the user and generating a ticket number to give it back to the user for reference.
  • Furthermore, in order for us to link it to a server or this case cloud functions, we need to enable fulfilments.
  • Click Fulfilments and enable webhook call for this intent.
  • Click Save button to save the Submit Ticket — collect description.

6. Fulfillments and Cloud Functions:

Fulfilments consists of Webhook and also Cloud Functions by firebase. Since we are trying to see how to create a chatbot using Dialogflow Enterprise Edition and Dialogflow API V2, so we need to also learn more about Webhook or Cloud Functions by Firebase.

Webhook: Setting up a webhook allows you to pass information from a matched intent into a web service and get a result from it.

Cloud Functions by Firebase: For simple webhook testing and implementation, you can use the Cloud Functions for Firebase area of the Fulfillments page. In most cases, the free “Spark” tier of Firebase is all you’ll need. Tier limitations and pricing information for the other tiers can be found on the Firebase pricing page.

To enable:

  • Click on Fulfillment in the left menu.
  • Also, enable the toggle for Inline Editor.
  • Inline Editor: You can use your own code or copy paste the code from below into the Inline Editor.
'use strict';
const http = require('http');
// Imports the Google Cloud client library
const Datastore = require('@google-cloud/datastore');
// Your Google Cloud Platform project ID
const projectId = 'REPLACE_WITH_YOUR_PROJECT_ID';
// Instantiates a client
const datastore = Datastore({
projectId: projectId
});
// The kind for the new entity
const kind = 'ticket';
// The kind for the new entity
const kind = 'ticket';
exports.dialogflowFirebaseFulfillment = (req, res) => {
console.log('Dialogflow Request body: ' + JSON.stringify(req.body));
// Get the city and date from the request
let ticketDescription = req.body.queryResult['queryText']; // incidence is a required param
//let name = req.body.result.contexts[0].parameters['given-name.original'];
let username = req.body.queryResult.outputContexts[1].parameters['given-name.original'];
let phone_number = req.body.queryResult.outputContexts[1].parameters['phone-number.original'];
console.log('description is ' +ticketDescription);
console.log('name is '+ username);
console.log('phone number is '+ phone_number);
function randomIntInc (low, high) {
return Math.floor(Math.random() * (high - low + 1) + low);
}
let ticketnum = randomIntInc(11111,99999);
// The Cloud Datastore key for the new entity
const taskKey = datastore.key(kind);
// Prepares the new entity
const task = {
key: taskKey,
data: {
description: ticketDescription,
username: username,
phoneNumber: phone_number,
ticketNumber: ticketnum
}
};
console.log("incidence is " , task);
// Saves the entity
datastore.save(task)
.then(() => {
console.log(`Saved ${task.key}: ${task.data.description}`);
res.setHeader('Content-Type', 'application/json');
//Response to send to Dialogflow
res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your ticket, the ticket number is " + ticketnum + ". Someone from the helpdesk will reach out to you within 24 hours."}));
//res.send(JSON.stringify({ 'fulfillmentText': "I have successfully logged your ticket, the ticket number is " + ticketnum + ". Someone from the helpdesk will reach out to you within 24 hours.", 'fulfillmentMessages': "I have successfully logged your ticket, the ticket number is " + ticketnum + ". Someone from the helpdesk will reach out to you within 24 hours."}));
})
.catch((err) => {
console.error('ERROR:', err);
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ 'speech': "Error occurred while saving, try again later", 'displayText': "Error occurred while saving, try again later" }));
});
}
  • Replace project_id with your own google cloud platform project ID in line 6. You should find the project ID under dashboard>Project Info in the Google cloud console.
  • Because we are gonna use Google Cloud Datastore to store the ticket information, we need to add the necessary dependencies under package.json.
  • Copy the code from below and add it to your package.json in the inline editor or just add the code on line 20 to add the cloud datastore dependency.
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"engines": {
"node": "~6.0"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "2.0.0-alpha.4",
"firebase-admin": "^4.2.1",
"firebase-functions": "^0.5.7",
"dialogflow": "^0.1.0",
"@google-cloud/datastore": "^1.1.0",
"dialogflow-fulfillment": "0.3.0-beta.3"
}
}
  • Click DEPLOY button to deploy our Cloud Functions.
  • Deployment might take a few seconds to be completed.
  • Note: The above cloud functions are being served using the new Dialogflow API V2 or V2 API. Hence, some of the codes commented in the index.js file was based on the V1 API. So this clearly shows how simple it is to migrate from V1 to V2 API in Dialogflow. I will do a separate tutorial on the migration from V1 to V2 API soon.

7. How to create a Customer Service Agent Chatbot:

Finally, its time for us to test our agent and implement our chatbot. Dialogflow allows us to easily integrate our agent into multiple platforms like Google Assistant, Facebook Messenger, Slack, web etc under Dialogflow Integrations.

For this Tutorial, we will see how we can create our agent and embed that onto our own website.

  • Click on Integrations from the left navigation menu.
  • Enable Web Demo
  • A pop-up window with a generated link will show up.
  • First of all, click on the link to see your chatbot working its magic.
  • As a result, you can also use the embed code to add the chatbot to your website.

8. Store the ticket information in Google Cloud Datastore:

Finally, our chatbot is up and running. Most noteworthy is if you go through the cloud functions, we are capturing the following details about the customer.

  • Capture Customer Info
  • username
  • phone_number
  • description

8.1 Store the details into Google Cloud Datastore:

We can also see the details that we captured through our chatbot in our Google Cloud Datastore.

  • Go to Google Cloud Console.
  • Click on the navigation menu to show the options.
  • Choose Datastore -> Entities.
  • Hence, view the data captured by the Chatbot with all specific information.
Google Cloud Datastore with ticket information

9. Conclusion:

In conclusion, Its been a long tutorial, covering various aspects of how to create a chatbot using Dialogflow Enterprise Edition and Dialogflow API V2. In addition, we also learned how to capture the information from the user and store it using Google Cloud Datastore. Hence, this is just scratching the surface and the possibilities of leveraging Dialogflow and Google Cloud services are endless. I have done my best to cover this tutorial with every step so that you can easily learn to develop such applications. In the next tutorial, we will also learn how to use this in the form of Google Assistant and also how we can migrate from V1 API to V2 API.

Finally, see you all in the next tutorial series. Until then, also check out my other tutorial posts on Sentiment Analysis using Google Cloud Natural Language API and Entity Analysis using Google Cloud Natural Language API and Getting started with Flutter. Check out more tutorials on techwithsach.com

10. References: