Moses Sam Paul July 21, 2018
How to build a chatbot with Dialog flow | Chapter 1 — 
Introduction
medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-1-introduction-ab880c3428b5
Chatbot is a program that can conduct an intelligent conversation. It should be able to
convincingly simulate a human behaviour and pass the turing test.
In this series we will learn how to build a chatbot from scratch, connect it with external API
to pull data and deploy it on Slack as a bot.
Series Content:
Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment
Chapter 2: Understanding the building blocks of Dialogflow
Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot
Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site
through fulfilment and revert the result to the user
Chapter 5: Connect the “StockTracker” Chatbot with Slack
In this post, we’ll learn
Why build a chatbot?
Pre-requisites
Introduction to Dialogflow
Flow of conversation
Setting up dialogflow account
1/5
Authorise Dialogflow on Google Cloud
Why build a chatbot?
A chatbot is, in essence, a piece of robotic software used to imitate human conversation
through text chats and voice commands (a good example being Siri or Amazon Alexa).
2 Types of chatbots:
1. Rule based chatbots (if you ask for phones the relevant phone pages open up in an
e-commerce site that’s an example of a rule based chatbot)
2. A.I. based chat bots (learn over a period of time using Machine Learning techniques) 
— dialog flow is an example of that
Chatbots are extremely valuable for businesses and this value will only increase as time
goes by.
On obvious area of chatbot implementation is customer service. Bots are invaluable here.
Waiting on hold may soon be a thing of the past as they become advanced enough to deal
with basic level customer service queries, and this is already being used by a lot of
companies worldwide. Nordstrom, for example, implemented a chatbot to assist with
customer service at the end of 2016, and this has made their technical support much more
responsive and immediate. It’s no secret that this has resulted in significant cost reduction.
Text and Voice based chatbots are the future and if you are an entrepreneur or a techie, it’s
the right time to spend some time learning about building these bots.
Pre-requisites
No programming experience is required as this series is mostly gonna be using
GUI(Graphical User Interface) of Google’s dialogflow.
Although, in chapter 4, we will be using a little bit of programming to pull data from a
stockmarket data site to display the result via our chatbot. If you have some experience in
dabbling with javascript /Restful APIs it might help. Even if you don’t have any
experience, don’t fret will break it down.
Introduction to Dialogflow
Dialogflow (formerly Api.ai, Speaktoit) is a Google-owned developer of human–computer
interaction technologies based on natural language conversations. The company is best known
for creating the Assistant (by Speaktoit), a virtual buddy for Android, iOS, and Windows
Phone smartphones that performs tasks and answers users’ question in a natural language. [1]
Speaktoit has also created a natural language processing engine that incorporates conversation
context like dialogue history, location and user preferences. — Wiki
Supports 14+ languages in 16+ programming languages.
SDKs to work with web apps, mobile app & wearables
2/5
Integrates with 14+ chat platforms such as slack, messenger, Alexa, Google Home etc.
Flow of conversation within DialogFlow
User: We, Machines!
Text / Voice : The user interacts with an app like facebook messenger / google home to
start the interaction with the bot.
Dialogflow: Bot platform
Agent: A module within dialogflow which incorporates Natural Language Processing to
understand what the user meant and to figure out what “action” has to be carried out. The
agent transforms the user request into machine readable actionable data.
Intent: Support or the service that the user wants from the agent. Intent is configured by
the developers. Intent determines the action by the code.
Fulfillment: This is the code. This part of the conversation lets you pass on the request
from your bot to an external source and get response and pass it back to the user. This is
achieved via Webhook. Setting up a webhook allows you to pass information from a
matched intent into a web service and get a result from it.
Note: Don’t be threatened by the terms here. Once we setup dialogflow account and open
the site, all this will fall into place.
Setting up Dialogflow account.
Ok now that the boring ‘theory / lecture’ part is over, let’s jump in and start setting up the
environment where we’ll be creating our bot!
1. Goto : https://dialogflow.com/
3/5
2. Create an account with a gmail account, and “agree” to the terms & conditions.
If you wanna delve deeper, do go through the docs here.
Overview | Dialogflow
This tutorial shows you how to build a simple Dialogflow agent, walking you through the
most important features of…dialogflow.com
Authorise Dialogflow on Google Cloud
First step in creating our bot is to create an agent.
4/5
This will create a new GoogleCloud Project automatically. If you are prompted for the
authorisation, do allow. If you don’t have a google cloud platform account please create
one.
Summary:
Chatbots built with Google’s DialogFlow are intelligent personal assistants.
Dialogflow abstracts out the Natural Language Processing, Machine Learning and
other deeper concepts and gives a clean usable user interface to focus on the
conversation flow and build bots.
In the next chapter, we’ll understand the building blocks of dialogflow and start building our
bot.
5/5
Moses Sam Paul July 23, 2018
How to build a chatbot with Dialog flow | Chapter 2—
Building Blocks of DialogFlow
medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-2-building-blocks-of-dialogflow-5e37bdbc09c
In chapter 1, we covered the importance of chatbots and setting up the dialogflow account.
In this chapter we will understand the building blocks of dialogflow.
Series Content:
Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment
Chapter 2: Understanding the building blocks of Dialogflow
Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot
Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site
through fulfilment and revert the result to the user
Chapter 5: Connect the “StockTracker” Chatbot with Slack
Re-cap:
Building blocks of Dialogflow.
1/15
1. Agents : Help convert user request into actionable data. Eg: TestAgent in the image
above
2. Intents: These are configured by developers which indicate what the objective of the
user might be when he/she/she makes a specific request. Eg: Book a flight / Collect
Feedback etc
3. Entities: Help extract information from user speech with the help of prompts. Eg:
“Book a flight” intent might need such as the: to and from cities, date, class etc as
entities that the agent tries to extract from the user via conversations. The information
received here are sent on for fulfilment.
4. Fulfilment: Code that fulfils the intent of the user’s request.
5. Integrations: Twitter, Slack, Googlehome etc..
6. Prebuilt Agents: Check out the various built-in agents
7. Smalltalk: Helps make bots friendly and chatty with no coding from our end.
Agents
Agents translate user requests to actionable data i.e. intents. It’s essentially a module within
dialog flow which incorporates Natural Language Processing to understand what the user
meant and to figure out what “action” has to be carried out.
2/15
Agents manage conversations with the user through intent, entities, contexts and other
building blocks.
We are gonna build a bot that helps us with planning a business trip
Build the Agent : Trip Planner
Explore the settings page…the ML settings gives us the type of machine learning we
wanna use for this bot.
ML CLASSIFICATION THRESHOLD — 0.3
Define the threshold value for the confidence score. If the returned value is less than the
threshold value, then a fallback intent will be triggered or, if there is no fallback intents
defined, no intent will be triggered.
In simple terms, if the match is less than 30% percent then the inbuilt “fallback intent”
3/15
(sorry, I don’t understand etc) will be triggered.
Try to go through the settings tab and the google cloud platform project that has been
created automatically the moment you created the agent.
Intent
Intents are configured by developers and used to determine the action taken by the code.
Think of using Intent as a mapping….what a user says and what your software should
execute. <Don’t rush to create these intents…am just explaining stuff here, once we reach
custom intent, will show how these intents are created>
Don’t worry about context and events for now. We’ll get there in chapter 3.
Training Phrase:
Phrases you can expect from the user that will trigger the intent.
4/15
Action & Parameters:
The inputs you might need from the user to take an action on the user request. For eg: In
BookFlights intent, we might need the To city, From City, Date etc to finish the action. We
have to set entities first for it to be mapped here but go with the flow for now.
Response:
5/15
Default Intents:
Dialogflow has few default intents that can help us save a lot of time.
1. Welcome Intent: Greets the usr, exchange pleasantries (partially configured)
2. Fallback Intent : Default fall through intent when no others match (excuse me, I din’t
get what you said)
Small Talk Intent:
Helps make bots friendly and chatty with no coding from our end. Again, this comes by
default you just have to enable it.
Custom Intent:
6/15
Continuing our effort to create the TripPlanner bot...we gotta create the intents
for the same. We are gonna help users book a flight, book a room and book a car so
we need to create 3 custom intents
create custom intents
So we gotta create 3 custom intents.
1. BookFlights
7/15
if the cities and the date aren’t getting tagged / colored don’t panic..it’ll happen after we
create the entities.
Similarly, create two more custom intents …. BookCars, BookRooms.
Entities
So, till now we have created one agent and three custom intents. Let’s understand entities
in this section.
Entities are used to extract parameter values from user queries. So when the user says
“Book me a flight” we usually ask for the city, date and the type of flight probably..these are
called entities.
Example Conversation
8/15
We (chatbot) asks questions to fill our Slots
Whatever the user responds to the slot based questions are the parameters
9/15
The generic abstract form for the parameters are the entities
Three types of entities:
1. System — Time, City, Date
2. Developer — Room type
3. User — defined at the session level (user’s playlist)..think of them as cookies in
websites
Ok, time to work:
Create Developer Entities:
Book Flights Intent:
1. From City
2. To City
3. Date
4. Flight Type [optional]
Book Rooms Intent:
1. City
2. Date
3. Room Type [Optional]
Book Cars Intent:
1. City
2. Date
3. Car Type
10/15
So as you can see City and Date are system entities so we don’t have to define them..but
flight_type, room_type, car_type are all custom entities and we gotta define them..see
images below.
custom entities are configured
11/15
Now that the Entities are defined lets go back and configure the intents we had created
earlier.
Configuring Custom Intent
Book Flight intent with
Expression with annotations
parameters
prompts
12/15
13/15
Similarly, do the same for bookCars,
bookRooms intents.
14/15
Great, so what all have we done now?
Created an Agent (TripPlanner)
Created and Configured three Intents (BookFlights, BookCars, BookRooms)
Created three custom entities. (flight_type, room_type, car_type)
Play around with the bot we have created till now, let me know if you face any difficulties.
In the next chapter, we shall delve a little deeper to understand the dialogs and how to
maintain contexts between conversations.
15/15
Moses Sam Paul July 24, 2018
How to build a chatbot with Dialog flow | Chapter 3—
Dialogs for Conversations
medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-3-dialogs-for-conversations-4021fb4808ed
In the last chapter we created a basic bot that can help us book flights, rooms and cars but
did you notice that each time we had to give the city name and the date ..isn’t that a little
annoying ? Right.
So in this chapter we will look at dialogs and how it can help us make the bot a little smarter
by retaining relevant information from the previous conversations with the users.
Series Content:
Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment
Chapter 2: Understanding the building blocks of Dialogflow
Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot
Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site
through fulfilment and revert the result to the user
Chapter 5: Connect the “StockTracker” Chatbot with Slack
Using Dialogs for Managing Conversations
Linear Dialogs can span a single intent or multiple intents
Flow Predictably to elicit information needed to complete actions
Contexts allow information sharing leading to more natural conversations
Non-linear dialogs help branch to intents based on user responses
1/25
…book a room — when ? where? type ? these are linear dialogs to collect information from
the user to fulfill
Context: lets us use linear dialogs that can be used across multiple intents
2/25
Now an example of how linear dialogs can flow between multiple intents.
3/25
4/25
Within Book Flights intent…create “flight_context” as an output context. the Number 5
represents the lifespan of this context. By default it’s for 5 requests.
5/25
Go to BookRooms intent and add “flight_context” as the input context
Go to BookCars intent and add “flight_context” as the input context
Within BookRooms intent..once the input context is it, we gotta assign the default values for
parameters.
set deafult value for date as “#flight_context.date”
syntax:
#context_name.paramter_name
Remember geo-city1 is the destination city…go to BookFlight intent and check out the
names.
similarly set default values in the bookCars intent as well.
Let’s test out the agent:
1. Book a flight with all necessary inputs.
2. Book a room
3. Book a car (car type alone has to be specified)
Booking a flight with all inputs.
See “flight_context” being setup under
contexts!
2. Book a room without specifying any
date or city
great, see just saying “book a room” has
resulted in a room booked for the
destination city and date from the
bookFlights intent through the
flight_context.
3. Book a car
Book a car — user expression just asks for
the type of car
as you can see in the parameter the rest
(city, date) are retained from the
“flight_context” input context.
Ok, don’t jump off your horses….the issue
here is you always need to start with the
Bookflight intent…as in, an user has to
first book a flight and then a room / car this
is not ideal right ?
To solve this, we have thefollowup intent.
6/25
7/25
Linear Dialog using a Followup Intent
1. Clear all configuration settings…the “flight_context” has to be removed and the
default values have to be removed form both “bookRooms” & “bookCars” intents.
Leave it in the “bookFlights” intent.
2. Create a custom follow-up intent
8/25
9/25
10/25
Set the input context as flight_context which is the output context of bookFlights intent,
remember?
set few user expressions too…
Make sure you manually add the geocity &date parameters
set the default response too…
So a normal book a room would trigger the BookRooms intent.. see below
11/25
booking a flight and then using the same
expression would trigger the followup
intent
12/25
13/25
14/25
Till now we have seen only linear dialogs…but real-world conversations aren’t linear…don’t
trust me? try getting into an argument with your girl :P
Non-linear dialogs branch to the next intent based on responses from the previous intent,
eg:
customer satisfaction survey
15/25
Location Feedback:
Facilities Feedback
16/25
Configuration: create a new feedback intent — set output context, few training phrases and
the text reponse
17/25
Set a new entity called rating
18/25
New Intent: Location- Good
19/25
New Intent : Location — Bad
20/25
New Intent: Facilitites — Good
21/25
22/25
New Intent : Facilities — Bad
23/25
Overall Intents:
Summary :
24/25
Dialogs can be linear or non-linear
Linear Dialogs
* Linear Dialogs can span single or multiple intents
* Primary reason of Linear Dialogs is to capture information from the user to
complete actions
* Context allows information sharing to simulate a more natural conversations
Non-Linear Dialogs
* Non-linear dialogs help branch to intents based on user responses
So, our bot can now manage different types of dialogs and might look smarter…but till now
we have not connected with an external site to pul data. So in the next chapter, we shall
see how we can create one more bot / Agent (StockTracker) that will connect to an external
site and get us the data real-time, no more pre-defined text responses. We are entering the
fulfilment world!
25/25
Moses Sam Paul July 25, 2018
How to build a chatbot with Dialog flow | Chapter 4— External API for Fulfilment
medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-4-external-api-for-fulfilment-3ab934fd7a00
In this chapter we are gonna create a “StockTracker” bot that will pull a specific ticker data from an external website through
RESTFUl APIs. [Don’t worry if you don’t understand the jargons, we shall break it down]
Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment
Chapter 2: Understanding the building blocks of Dialogflow
Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot
Using External API for Fulfillment
Overview of the Module:
Completing actions requires more than a text response
Fulfill actions using webhooks [webhooks are just HTTP callback that can be notified using post request]
Host Code using Cloud Functions for Firebase
Other external Endpoints such as AWS lambdas will work as well
Quick Recap:
1/8
Fulfilment
Executing the code that will complete the action requested by the user to fulfil the intent of the user.
Code might require us to update the datbase
Once the code is generated a response is generated to tex response that’s sent to the user..which is in-turn sent to the agent
(bot) which in turn is sent to the app that the user used to interact with your agent.
…..
Example:
We are gonna get the stock price of Apple from an external source.
The Intrinio platform
Apple Inc | AAPL | Intrinio
Apple, Inc. engages in the design, manufacture, and marketing of mobile communication, media devices, personal…intrinio.com
Alternative for Indian Market could be: https://kite.trade/startups
Signup and go to your ccount section..you should find your API access keys
2/8
API explorer:
https://intrinio.com/api-explorer?
formula=historical_data&values=eyJpZGVudGlmaWVyIjoiQUFQTCIsIml0ZW0iOiIiLCJzZXF1ZW5jZSI6MH0%3D
Documentation
http://docs.intrinio.com/#introduction
Test:
https://api.intrinio.com/historical_data?identifier=AAPL&item=open_price&start_date=2018-07-12&end_date=2018-07-12
A prompt will ask for your username and password …get it from your “my account” page and you will get aJSON response as
below.
Demo
Setup fulfilment for the stockmarket tracker using cloud functions for firebase.
Use the Blaze pay as you go tier to access external APIs
Step 1: Setup a New Agent in Dialog Flow
Step 2: Setup entities
3/8
Step 3: Intent
4/8
Fulfillment:
5/8
'use strict';
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 });
var chat = "here is a sample response: trump sucks";
response.setHeader('Content-Type','applicaiton/json');
response.send(JSON.stringify({"fulfillmentText": chat}));
});
<Error Log: If you are following a dialogflow v1 tutorial you might be using response.send(JSON.stringify({“speech”: chat,
“displayText”, chat}));
That will throw an error
Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: speech in message
google.cloud.dialogflow.v2.WebhookResponse.
That’s because speech and displaytext has been replaced with “fulfillmentText” in v2 of Dialogflow.
>
Enable webhook as fulfillment within the GetStockPrice intent
6/8
Now we gotta make sure a specific action is named and that is called during the webhook.
Set the action within the intent’s action and parameters section: <input.getStockPrice>
7/8
Code:
Output:
Few Errors you might come across:
1. Upgrade to Blaze Pay as you go
Error:
Billing account not configured. External network is not accessible and quotas
are severely limited. Configure billing account to remove these restrictions
2. Cannot read property action
Had initially written
let action = request.body.result.action;
and got this error:
TypeError: Cannot read property 'action' of undefined
at exports.dialogflowFirebaseFulfillment.functions.https.onRequest
(/user_code/index.js:9:37)
at cloudFunction (/user_code/node_modules/firebase-
functions/lib/providers/https.js:26:47)
at /var/tmp/worker/worker.js:684:7
at /var/tmp/worker/worker.js:668:9
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
but printing the header and the body of requests showed that dialogflow v2 has
changed things a little bit.
Request Body: {"responseId":"b539a7bf-eccc-43a9-9f6b-a1fe6ea41763","queryResult":
{"queryText":"yesterday","action":"input.getStockPrice","parameters":
{"date":"2018-07-
18T12:00:00+05:30","company_name":"apple","price_type":"high"},"allRequiredParamsPresent":true,"fulfillmentMessages":
[{"text":{"text":[""]}}],"intent":{"name":"projects/stocktracker-9268f/agent/intents/7ccfc0ff-7271-4a60-83ce-
868d6eea03a0","displayName":"GetStockPrice"},"intentDetectionConfidence":1,"languageCode":"en"},"originalDetectIntentRequest":
{"payload":{}},"session":"projects/stocktracker-9268f/agent/sessions/4c4870db-0d23-ecc0-823e-de2bde1716f9"}
so instead of “query” add “queryResult”
let action = request.body.Queryresult.action;
Summary:
So what did we learn in this chapter? We used our bot to parse the user request and used dialogflow’s fulfilment module to create
a cloud function hosted on firebase to pull data from a third party site through a RESTFUL API and revert to the user.
Wait, but did we really though ? Till now we haven’t distributed / deployed out bot in an external environment be it on slack or
twitter to let the user really interact with our bot.
In the next chapter, we will integrate / deploy / distribute our app on a slack channel and let users interact with our chatbot.
8/8
Moses Sam Paul July 25, 2018
How to build a chatbot with Dialog flow | Chapter 5—
Connect Chatbot with Slack
medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-5-connect-chatbot-with-slack-47732712aa23
In this chapter, we are going to deploy our “stocktracker” bot to a slack channel so that
external users can actually interact with our bot.
Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment
Chapter 2: Understanding the building blocks of Dialogflow
Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot
Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site
through fulfilment and revert the result to the user
Integrating with third-party Applications
Slack, Kik, Viber, Skype, Google Assistant.
1. Slack workspace
1/10
2/10
2. Create and app on slack — https://api.slack.com
3/10
4/10
3. Subscribe to events (message.im)
5/10
4. Head over to Dialogflow:
6/10
copy the oauth & event URLs
oAuth
https://bots.dialogflow.com/slack/cbb071ff-0f33-4058-b76c-591461623fb3/start
event
https://bots.dialogflow.com/slack/cbb071ff-0f33-4058-b76c-591461623fb3/webhook
5. Go back to Slack API…add oAuth url & Event Subscription URLs
7/10
6. Distribution:
8/10
7. Test: Now let’s go back to our slack workspace and take the bot for a test spin.
9/10
Summary:
So what did we learn in this chapter?
We created new slack workspace, create an app on slack connect it with our bot and tested
it.
10/10

How to build a Chatbot with Google's Dialogflow

  • 1.
    Moses Sam PaulJuly 21, 2018 How to build a chatbot with Dialog flow | Chapter 1 —  Introduction medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-1-introduction-ab880c3428b5 Chatbot is a program that can conduct an intelligent conversation. It should be able to convincingly simulate a human behaviour and pass the turing test. In this series we will learn how to build a chatbot from scratch, connect it with external API to pull data and deploy it on Slack as a bot. Series Content: Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment Chapter 2: Understanding the building blocks of Dialogflow Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site through fulfilment and revert the result to the user Chapter 5: Connect the “StockTracker” Chatbot with Slack In this post, we’ll learn Why build a chatbot? Pre-requisites Introduction to Dialogflow Flow of conversation Setting up dialogflow account 1/5
  • 2.
    Authorise Dialogflow onGoogle Cloud Why build a chatbot? A chatbot is, in essence, a piece of robotic software used to imitate human conversation through text chats and voice commands (a good example being Siri or Amazon Alexa). 2 Types of chatbots: 1. Rule based chatbots (if you ask for phones the relevant phone pages open up in an e-commerce site that’s an example of a rule based chatbot) 2. A.I. based chat bots (learn over a period of time using Machine Learning techniques)  — dialog flow is an example of that Chatbots are extremely valuable for businesses and this value will only increase as time goes by. On obvious area of chatbot implementation is customer service. Bots are invaluable here. Waiting on hold may soon be a thing of the past as they become advanced enough to deal with basic level customer service queries, and this is already being used by a lot of companies worldwide. Nordstrom, for example, implemented a chatbot to assist with customer service at the end of 2016, and this has made their technical support much more responsive and immediate. It’s no secret that this has resulted in significant cost reduction. Text and Voice based chatbots are the future and if you are an entrepreneur or a techie, it’s the right time to spend some time learning about building these bots. Pre-requisites No programming experience is required as this series is mostly gonna be using GUI(Graphical User Interface) of Google’s dialogflow. Although, in chapter 4, we will be using a little bit of programming to pull data from a stockmarket data site to display the result via our chatbot. If you have some experience in dabbling with javascript /Restful APIs it might help. Even if you don’t have any experience, don’t fret will break it down. Introduction to Dialogflow Dialogflow (formerly Api.ai, Speaktoit) is a Google-owned developer of human–computer interaction technologies based on natural language conversations. The company is best known for creating the Assistant (by Speaktoit), a virtual buddy for Android, iOS, and Windows Phone smartphones that performs tasks and answers users’ question in a natural language. [1] Speaktoit has also created a natural language processing engine that incorporates conversation context like dialogue history, location and user preferences. — Wiki Supports 14+ languages in 16+ programming languages. SDKs to work with web apps, mobile app & wearables 2/5
  • 3.
    Integrates with 14+chat platforms such as slack, messenger, Alexa, Google Home etc. Flow of conversation within DialogFlow User: We, Machines! Text / Voice : The user interacts with an app like facebook messenger / google home to start the interaction with the bot. Dialogflow: Bot platform Agent: A module within dialogflow which incorporates Natural Language Processing to understand what the user meant and to figure out what “action” has to be carried out. The agent transforms the user request into machine readable actionable data. Intent: Support or the service that the user wants from the agent. Intent is configured by the developers. Intent determines the action by the code. Fulfillment: This is the code. This part of the conversation lets you pass on the request from your bot to an external source and get response and pass it back to the user. This is achieved via Webhook. Setting up a webhook allows you to pass information from a matched intent into a web service and get a result from it. Note: Don’t be threatened by the terms here. Once we setup dialogflow account and open the site, all this will fall into place. Setting up Dialogflow account. Ok now that the boring ‘theory / lecture’ part is over, let’s jump in and start setting up the environment where we’ll be creating our bot! 1. Goto : https://dialogflow.com/ 3/5
  • 4.
    2. Create anaccount with a gmail account, and “agree” to the terms & conditions. If you wanna delve deeper, do go through the docs here. Overview | Dialogflow This tutorial shows you how to build a simple Dialogflow agent, walking you through the most important features of…dialogflow.com Authorise Dialogflow on Google Cloud First step in creating our bot is to create an agent. 4/5
  • 5.
    This will createa new GoogleCloud Project automatically. If you are prompted for the authorisation, do allow. If you don’t have a google cloud platform account please create one. Summary: Chatbots built with Google’s DialogFlow are intelligent personal assistants. Dialogflow abstracts out the Natural Language Processing, Machine Learning and other deeper concepts and gives a clean usable user interface to focus on the conversation flow and build bots. In the next chapter, we’ll understand the building blocks of dialogflow and start building our bot. 5/5
  • 6.
    Moses Sam PaulJuly 23, 2018 How to build a chatbot with Dialog flow | Chapter 2— Building Blocks of DialogFlow medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-2-building-blocks-of-dialogflow-5e37bdbc09c In chapter 1, we covered the importance of chatbots and setting up the dialogflow account. In this chapter we will understand the building blocks of dialogflow. Series Content: Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment Chapter 2: Understanding the building blocks of Dialogflow Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site through fulfilment and revert the result to the user Chapter 5: Connect the “StockTracker” Chatbot with Slack Re-cap: Building blocks of Dialogflow. 1/15
  • 7.
    1. Agents :Help convert user request into actionable data. Eg: TestAgent in the image above 2. Intents: These are configured by developers which indicate what the objective of the user might be when he/she/she makes a specific request. Eg: Book a flight / Collect Feedback etc 3. Entities: Help extract information from user speech with the help of prompts. Eg: “Book a flight” intent might need such as the: to and from cities, date, class etc as entities that the agent tries to extract from the user via conversations. The information received here are sent on for fulfilment. 4. Fulfilment: Code that fulfils the intent of the user’s request. 5. Integrations: Twitter, Slack, Googlehome etc.. 6. Prebuilt Agents: Check out the various built-in agents 7. Smalltalk: Helps make bots friendly and chatty with no coding from our end. Agents Agents translate user requests to actionable data i.e. intents. It’s essentially a module within dialog flow which incorporates Natural Language Processing to understand what the user meant and to figure out what “action” has to be carried out. 2/15
  • 8.
    Agents manage conversationswith the user through intent, entities, contexts and other building blocks. We are gonna build a bot that helps us with planning a business trip Build the Agent : Trip Planner Explore the settings page…the ML settings gives us the type of machine learning we wanna use for this bot. ML CLASSIFICATION THRESHOLD — 0.3 Define the threshold value for the confidence score. If the returned value is less than the threshold value, then a fallback intent will be triggered or, if there is no fallback intents defined, no intent will be triggered. In simple terms, if the match is less than 30% percent then the inbuilt “fallback intent” 3/15
  • 9.
    (sorry, I don’tunderstand etc) will be triggered. Try to go through the settings tab and the google cloud platform project that has been created automatically the moment you created the agent. Intent Intents are configured by developers and used to determine the action taken by the code. Think of using Intent as a mapping….what a user says and what your software should execute. <Don’t rush to create these intents…am just explaining stuff here, once we reach custom intent, will show how these intents are created> Don’t worry about context and events for now. We’ll get there in chapter 3. Training Phrase: Phrases you can expect from the user that will trigger the intent. 4/15
  • 10.
    Action & Parameters: Theinputs you might need from the user to take an action on the user request. For eg: In BookFlights intent, we might need the To city, From City, Date etc to finish the action. We have to set entities first for it to be mapped here but go with the flow for now. Response: 5/15
  • 11.
    Default Intents: Dialogflow hasfew default intents that can help us save a lot of time. 1. Welcome Intent: Greets the usr, exchange pleasantries (partially configured) 2. Fallback Intent : Default fall through intent when no others match (excuse me, I din’t get what you said) Small Talk Intent: Helps make bots friendly and chatty with no coding from our end. Again, this comes by default you just have to enable it. Custom Intent: 6/15
  • 12.
    Continuing our effortto create the TripPlanner bot...we gotta create the intents for the same. We are gonna help users book a flight, book a room and book a car so we need to create 3 custom intents create custom intents So we gotta create 3 custom intents. 1. BookFlights 7/15
  • 13.
    if the citiesand the date aren’t getting tagged / colored don’t panic..it’ll happen after we create the entities. Similarly, create two more custom intents …. BookCars, BookRooms. Entities So, till now we have created one agent and three custom intents. Let’s understand entities in this section. Entities are used to extract parameter values from user queries. So when the user says “Book me a flight” we usually ask for the city, date and the type of flight probably..these are called entities. Example Conversation 8/15
  • 14.
    We (chatbot) asksquestions to fill our Slots Whatever the user responds to the slot based questions are the parameters 9/15
  • 15.
    The generic abstractform for the parameters are the entities Three types of entities: 1. System — Time, City, Date 2. Developer — Room type 3. User — defined at the session level (user’s playlist)..think of them as cookies in websites Ok, time to work: Create Developer Entities: Book Flights Intent: 1. From City 2. To City 3. Date 4. Flight Type [optional] Book Rooms Intent: 1. City 2. Date 3. Room Type [Optional] Book Cars Intent: 1. City 2. Date 3. Car Type 10/15
  • 16.
    So as youcan see City and Date are system entities so we don’t have to define them..but flight_type, room_type, car_type are all custom entities and we gotta define them..see images below. custom entities are configured 11/15
  • 17.
    Now that theEntities are defined lets go back and configure the intents we had created earlier. Configuring Custom Intent Book Flight intent with Expression with annotations parameters prompts 12/15
  • 18.
  • 19.
    Similarly, do thesame for bookCars, bookRooms intents. 14/15
  • 20.
    Great, so whatall have we done now? Created an Agent (TripPlanner) Created and Configured three Intents (BookFlights, BookCars, BookRooms) Created three custom entities. (flight_type, room_type, car_type) Play around with the bot we have created till now, let me know if you face any difficulties. In the next chapter, we shall delve a little deeper to understand the dialogs and how to maintain contexts between conversations. 15/15
  • 21.
    Moses Sam PaulJuly 24, 2018 How to build a chatbot with Dialog flow | Chapter 3— Dialogs for Conversations medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-3-dialogs-for-conversations-4021fb4808ed In the last chapter we created a basic bot that can help us book flights, rooms and cars but did you notice that each time we had to give the city name and the date ..isn’t that a little annoying ? Right. So in this chapter we will look at dialogs and how it can help us make the bot a little smarter by retaining relevant information from the previous conversations with the users. Series Content: Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment Chapter 2: Understanding the building blocks of Dialogflow Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site through fulfilment and revert the result to the user Chapter 5: Connect the “StockTracker” Chatbot with Slack Using Dialogs for Managing Conversations Linear Dialogs can span a single intent or multiple intents Flow Predictably to elicit information needed to complete actions Contexts allow information sharing leading to more natural conversations Non-linear dialogs help branch to intents based on user responses 1/25
  • 22.
    …book a room — when? where? type ? these are linear dialogs to collect information from the user to fulfill Context: lets us use linear dialogs that can be used across multiple intents 2/25
  • 23.
    Now an exampleof how linear dialogs can flow between multiple intents. 3/25
  • 24.
  • 25.
    Within Book Flightsintent…create “flight_context” as an output context. the Number 5 represents the lifespan of this context. By default it’s for 5 requests. 5/25
  • 26.
    Go to BookRoomsintent and add “flight_context” as the input context Go to BookCars intent and add “flight_context” as the input context Within BookRooms intent..once the input context is it, we gotta assign the default values for parameters. set deafult value for date as “#flight_context.date” syntax: #context_name.paramter_name Remember geo-city1 is the destination city…go to BookFlight intent and check out the names. similarly set default values in the bookCars intent as well. Let’s test out the agent: 1. Book a flight with all necessary inputs. 2. Book a room 3. Book a car (car type alone has to be specified) Booking a flight with all inputs. See “flight_context” being setup under contexts! 2. Book a room without specifying any date or city great, see just saying “book a room” has resulted in a room booked for the destination city and date from the bookFlights intent through the flight_context. 3. Book a car Book a car — user expression just asks for the type of car as you can see in the parameter the rest (city, date) are retained from the “flight_context” input context. Ok, don’t jump off your horses….the issue here is you always need to start with the Bookflight intent…as in, an user has to first book a flight and then a room / car this is not ideal right ? To solve this, we have thefollowup intent. 6/25
  • 27.
  • 28.
    Linear Dialog usinga Followup Intent 1. Clear all configuration settings…the “flight_context” has to be removed and the default values have to be removed form both “bookRooms” & “bookCars” intents. Leave it in the “bookFlights” intent. 2. Create a custom follow-up intent 8/25
  • 29.
  • 30.
  • 31.
    Set the inputcontext as flight_context which is the output context of bookFlights intent, remember? set few user expressions too… Make sure you manually add the geocity &date parameters set the default response too… So a normal book a room would trigger the BookRooms intent.. see below 11/25
  • 32.
    booking a flightand then using the same expression would trigger the followup intent 12/25
  • 33.
  • 34.
  • 35.
    Till now wehave seen only linear dialogs…but real-world conversations aren’t linear…don’t trust me? try getting into an argument with your girl :P Non-linear dialogs branch to the next intent based on responses from the previous intent, eg: customer satisfaction survey 15/25
  • 36.
  • 37.
    Configuration: create anew feedback intent — set output context, few training phrases and the text reponse 17/25
  • 38.
    Set a newentity called rating 18/25
  • 39.
  • 40.
    New Intent :Location — Bad 20/25
  • 41.
  • 42.
  • 43.
    New Intent :Facilities — Bad 23/25
  • 44.
  • 45.
    Dialogs can belinear or non-linear Linear Dialogs * Linear Dialogs can span single or multiple intents * Primary reason of Linear Dialogs is to capture information from the user to complete actions * Context allows information sharing to simulate a more natural conversations Non-Linear Dialogs * Non-linear dialogs help branch to intents based on user responses So, our bot can now manage different types of dialogs and might look smarter…but till now we have not connected with an external site to pul data. So in the next chapter, we shall see how we can create one more bot / Agent (StockTracker) that will connect to an external site and get us the data real-time, no more pre-defined text responses. We are entering the fulfilment world! 25/25
  • 46.
    Moses Sam PaulJuly 25, 2018 How to build a chatbot with Dialog flow | Chapter 4— External API for Fulfilment medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-4-external-api-for-fulfilment-3ab934fd7a00 In this chapter we are gonna create a “StockTracker” bot that will pull a specific ticker data from an external website through RESTFUl APIs. [Don’t worry if you don’t understand the jargons, we shall break it down] Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment Chapter 2: Understanding the building blocks of Dialogflow Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot Using External API for Fulfillment Overview of the Module: Completing actions requires more than a text response Fulfill actions using webhooks [webhooks are just HTTP callback that can be notified using post request] Host Code using Cloud Functions for Firebase Other external Endpoints such as AWS lambdas will work as well Quick Recap: 1/8
  • 47.
    Fulfilment Executing the codethat will complete the action requested by the user to fulfil the intent of the user. Code might require us to update the datbase Once the code is generated a response is generated to tex response that’s sent to the user..which is in-turn sent to the agent (bot) which in turn is sent to the app that the user used to interact with your agent. ….. Example: We are gonna get the stock price of Apple from an external source. The Intrinio platform Apple Inc | AAPL | Intrinio Apple, Inc. engages in the design, manufacture, and marketing of mobile communication, media devices, personal…intrinio.com Alternative for Indian Market could be: https://kite.trade/startups Signup and go to your ccount section..you should find your API access keys 2/8
  • 48.
    API explorer: https://intrinio.com/api-explorer? formula=historical_data&values=eyJpZGVudGlmaWVyIjoiQUFQTCIsIml0ZW0iOiIiLCJzZXF1ZW5jZSI6MH0%3D Documentation http://docs.intrinio.com/#introduction Test: https://api.intrinio.com/historical_data?identifier=AAPL&item=open_price&start_date=2018-07-12&end_date=2018-07-12 A promptwill ask for your username and password …get it from your “my account” page and you will get aJSON response as below. Demo Setup fulfilment for the stockmarket tracker using cloud functions for firebase. Use the Blaze pay as you go tier to access external APIs Step 1: Setup a New Agent in Dialog Flow Step 2: Setup entities 3/8
  • 49.
  • 50.
  • 51.
    'use strict'; 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 }); var chat = "here is a sample response: trump sucks"; response.setHeader('Content-Type','applicaiton/json'); response.send(JSON.stringify({"fulfillmentText": chat})); }); <Error Log: If you are following a dialogflow v1 tutorial you might be using response.send(JSON.stringify({“speech”: chat, “displayText”, chat})); That will throw an error Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: speech in message google.cloud.dialogflow.v2.WebhookResponse. That’s because speech and displaytext has been replaced with “fulfillmentText” in v2 of Dialogflow. > Enable webhook as fulfillment within the GetStockPrice intent 6/8
  • 52.
    Now we gottamake sure a specific action is named and that is called during the webhook. Set the action within the intent’s action and parameters section: <input.getStockPrice> 7/8
  • 53.
    Code: Output: Few Errors youmight come across: 1. Upgrade to Blaze Pay as you go Error: Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions 2. Cannot read property action Had initially written let action = request.body.result.action; and got this error: TypeError: Cannot read property 'action' of undefined at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:9:37) at cloudFunction (/user_code/node_modules/firebase- functions/lib/providers/https.js:26:47) at /var/tmp/worker/worker.js:684:7 at /var/tmp/worker/worker.js:668:9 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickDomainCallback (internal/process/next_tick.js:128:9) but printing the header and the body of requests showed that dialogflow v2 has changed things a little bit. Request Body: {"responseId":"b539a7bf-eccc-43a9-9f6b-a1fe6ea41763","queryResult": {"queryText":"yesterday","action":"input.getStockPrice","parameters": {"date":"2018-07- 18T12:00:00+05:30","company_name":"apple","price_type":"high"},"allRequiredParamsPresent":true,"fulfillmentMessages": [{"text":{"text":[""]}}],"intent":{"name":"projects/stocktracker-9268f/agent/intents/7ccfc0ff-7271-4a60-83ce- 868d6eea03a0","displayName":"GetStockPrice"},"intentDetectionConfidence":1,"languageCode":"en"},"originalDetectIntentRequest": {"payload":{}},"session":"projects/stocktracker-9268f/agent/sessions/4c4870db-0d23-ecc0-823e-de2bde1716f9"} so instead of “query” add “queryResult” let action = request.body.Queryresult.action; Summary: So what did we learn in this chapter? We used our bot to parse the user request and used dialogflow’s fulfilment module to create a cloud function hosted on firebase to pull data from a third party site through a RESTFUL API and revert to the user. Wait, but did we really though ? Till now we haven’t distributed / deployed out bot in an external environment be it on slack or twitter to let the user really interact with our bot. In the next chapter, we will integrate / deploy / distribute our app on a slack channel and let users interact with our chatbot. 8/8
  • 54.
    Moses Sam PaulJuly 25, 2018 How to build a chatbot with Dialog flow | Chapter 5— Connect Chatbot with Slack medium.com/swlh/how-to-build-a-chatbot-with-dialog-flow-chapter-5-connect-chatbot-with-slack-47732712aa23 In this chapter, we are going to deploy our “stocktracker” bot to a slack channel so that external users can actually interact with our bot. Chapter 1: Basic Gyaan about Chatbots and setting up the dialog flow environment Chapter 2: Understanding the building blocks of Dialogflow Chapter 3: Using linear, followup and non-linear dialogs to create a “TripPlanner” bot Chapter 4: We created a new bot “StockTracker” bot to pull data from an external site through fulfilment and revert the result to the user Integrating with third-party Applications Slack, Kik, Viber, Skype, Google Assistant. 1. Slack workspace 1/10
  • 55.
  • 56.
    2. Create andapp on slack — https://api.slack.com 3/10
  • 57.
  • 58.
    3. Subscribe toevents (message.im) 5/10
  • 59.
    4. Head overto Dialogflow: 6/10
  • 60.
    copy the oauth& event URLs oAuth https://bots.dialogflow.com/slack/cbb071ff-0f33-4058-b76c-591461623fb3/start event https://bots.dialogflow.com/slack/cbb071ff-0f33-4058-b76c-591461623fb3/webhook 5. Go back to Slack API…add oAuth url & Event Subscription URLs 7/10
  • 61.
  • 62.
    7. Test: Nowlet’s go back to our slack workspace and take the bot for a test spin. 9/10
  • 63.
    Summary: So what didwe learn in this chapter? We created new slack workspace, create an app on slack connect it with our bot and tested it. 10/10