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

SACHIN KUMAR
May 10, 2018 · 14 min read

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

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.

  • Enable the Dialogflow V2 API for that project.
  • After enabling the API, click on Dialogflow agent tab and choose an agent name for your project.
  • Agent Name
  • Language
  • Timezone
  • 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.
  • 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.”
  • From the Google Cloud service accounts page, select your project.
  • You should see a screen similar to the one shown below.
  • From the GCP console, go to APIs and Services and click on credentials.

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

set GOOGLE_APPLICATION_CREDENTIALS=[PATH]

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

  • Action
  • Response
  • Contexts

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.

  • Under the Training Phrases, go ahead an add a few phrases that your users would use to start the conversation.
  • 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.
  • 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.
  • You should now see a follow-up intent created. Click on Submit Ticket — custom.
  • 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.
  • “Thanks $given-name! What’s your phone number?”
  • You should now see a follow-up intent created. Click on Submit Ticket — collect name — custom.
  • 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.
  • “Thanks! Now please describe your issue”.
  • my phone keeps rebooting
  • issue with wifi
  • Bluetooth issues
  • The screen is unresponsive.
  • frozen screen
  • 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.

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.

  • 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" }));
});
}
  • 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"
}
}
  • 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.

  • Enable Web Demo
  • A pop-up window with a generated link will show up.
  • 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.

  • username
  • phone_number
  • description
  • 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.

10. References:

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

SACHIN KUMAR

Written by

Google Developer Expert | CTO — TupeloLife | Head of Google Developers Group Doha

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.