How to Build an Intelligent Chatbot with Python and Dialogflow

Chatbots are a powerful example of artificial intelligence (AI) in use today. Just think about Google Assistant and how intelligent the platform became thanks to machine learning. But, what is a chatbot? How do you create a custom bot for your website? Which technologies can you use to build it? In this blog post, you will find the answers to these questions through practical examples. Using Python and Dialogflow frameworks, you’ll build a cloud infrastructure for astoundingly intelligent chatbots. At the end of this tutorial, your chatbot will be able to understand the intents of your users and give them the information they are searching for, taking advantage of Google AI.

Conversational Chatbots, the state of the art

Chatbots are software systems created to interact with humans through chat. The first chatbots were able to create simple conversations based on a complex system of rules. The limits of these systems have been overcome by chatbots that use AI and machine learning to interpret the intents of their interlocutor. 

Chatbots can help in many practical cases and drastically reduce management costs. There are many examples that have become well-known successful use cases. For example, retailer H&M uses them to guide users through their purchase process on their website. In general, many support systems use chatbots to achieve operational efficiency, including answering common questions or helping users solve repetitive tasks. And some of them are very complex, such as those offering commercial offers or giving advice as a robo-advisor.

Using a chatbot in your company can offer many advantages:

  • 24-hour a day availability at reduced costs;
  • Ease of use on the web and measurable performances;
  • Accessibility to very large amounts of updated knowledge
  • Compatibility with all devices, including mobile, social networks, and even SMS

There are many types of chatbots that can be found online. Some of them do not require programming skills, much less knowledge of machine learning or natural language processing. Examples of this kind of chatbots are Rasa, Octane Ai, Massively, or ManyChat. However, the incredible rise of machine learning systems makes chatbots evolve. If you are interested in learning more, I recommend starting from one of our Learning Paths on how to use artificial intelligence cloud systems.

There are also very common systems that use AIML (Artificial Intelligence Markup Language) to model smart conversational systems. Today, the most widely used chatbots are those made available by major vendors such as Google, AWS and Microsoft. We all know Alexa, Cortana, and Google Assistant. You should carefully consider their service offerings when you are choosing the technology stack for your chatbot. All three of these big vendors provide reliable and scalable cloud computing services that will help you to implement and customize your chatbot according to your needs. By now, most famous platforms to easily create text or voice-based bots are the following:

  • Dialogflow (Google, formerly Api.ai)
  • Azure Bot Service (Microsoft)
  • Lex (AWS)
  • Wit.ai (Facebook)
  • Watson (IBM)

How to start with Dialogflow chatbot framework

Among all the services taken into consideration, Dialogflow is certainly one of the most impressive. The power of Google’s machine learning makes the difference: the natural language processing (NLP) engine is among the best on the market. As indeed its slogan recites: “Dialogflow is user-friendly, intuitive and makes sense”. It is also very easy to integrate with Google Cloud Speech-to-Text and third-party services such as Google Assistant, Amazon Alexa, and Facebook Messenger.

This tutorial is now divided into two parts. The first part shows you how you can configure the chatbot and does not require programming skills as it will be entirely done in the Google console. The second part shows you how to integrate the chatbot with your services and it requires a basic knowledge of Python.

Set up Dialogflow

Setting up Dialogflow is very easy:

  1. Sign up for free at dialogflow.com using a Google account
  2. Accept all the requested permissions in order to allow Dialogflow to manage your data across GCP services and let you optionally integrate Google Assistant (see the Figure 1 below)
  3. Access the Dialogflow console at console.dialogflow.com
  4. Create a new agent selecting the primary language (other languages can be added later) and the Google Project identifier. This is the name of the project in the Google Cloud Console and it is needed to enable billing and other settings. Don’t worry, if you don’t have an existing project create a new one. We’ll see later how to get a fresh token for the project.

Dialogflow Login
[FIG 1: Permissions asked by Google Cloud Console to let you access the Dialogflow console]
Dialogflow is based on two main concepts: intent and context. The intent is to accurately identify the purpose of the sentence that the user has sent to the bot. On the other side, the context is used to give coherence and fluency to the discussion, preserving the key concepts that have already been used in the conversation.

Intent creation
[FIG 2: Intent creation is the central part of the Dialogflow chatbot logics]
A further key concept is that of the entities, which allow you to identify common or parametrically recurring concepts in the discussion. There are a number of predefined types of entities that the system provides ready to use, such as dates or colors. Using these values you can make the chatbot very versatile.

Creation of a new entity
[FIG 3: Creation of a new entity for the Dialogflow chatbot]
For this tutorial, however, we will show a basic example and focus on intents only.

Once you created the agent, let’s start by defining some intents through the Dialogflow interface. The first thing I suggest to do is always use the graphical interface on the right to test our real-time chatbot.

As you can see in the Figure 4, just write in the “Try it now” form to get an answer. If you have not yet defined any intent, the system will use the fallback intent. In this way, you will prevent the discussion from coming to a standstill. Actually, this is a big advantage for us, but please pay attention and use this feature intelligently to bring the conversation to the right intent.

Now browse the “Default fallback intent”. Take a look at the full list of all “Responses”. They have already been defined for you. When the intent is matched, the chatbot engine randomly selects an item as the answer. Now it’s time to create one or more intents: use the console ensuring to also fill in the “Training phrases” form. In fact, these are the sentences that you expect from the user and that will trigger the intent. The more detailed you are in compiling sentences, the more precise the chatbot will be in identifying the intent. Finally, proceed by inserting more intents and always remember to progressively test the results using the helper on the right.

After each change you make and test, remember to save your progress by clicking on the “Save” button, so the machine learning model can train.

 

Creation of a fallback for Dialogflow chatbot
[FIG 4: Creation of a fallback for Dialogflow chatbot]
As you can see, it is possible to reach a good result even with the configuration of intents only. The Google NLP engine is doing most of the job for you. If you’re feeling confident (and if your use case requires it) you can start adding a context to your intents. You can even pass parameters between one intent and another, maintaining the context of the discussion. When you are satisfied with the level achieved, we will proceed with the next step: integrating the bot into our site!

Integrate the chatbot into your website

There are two ways that you can integrate a Dialogflow chatbot into your website: using a widget or using Python.

1) Using a widget

The easiest way to integrate Dialogflow into an HTML page is to use the iframe. Select “Integrations” from the menu on the left and make sure that “Web Demo” is enabled. Simply copy and paste the HTML code to view the agent directly on your site.

Integrate the chatbot in your website using the iframe
[FIG 5: Integrate the chatbot in your website using the iframe]

2) Using Python

The following script allows you to call Dialogflow using Python 3. You can find the client on GitHub for free. The script initializes a client session that takes the intent as input and finally returns a response, the so-called “fulfillment”, and the corresponding confidence as a decimal value. The sentence for which we want to get an answer is saved in the variable named “text_to_be_analyzed”. Edit the script by adding your sentence. Using Python, it is easy to create more custom logic. For example, you can catch a particular intent and then trigger a custom action.

"""Install the following requirements:
    dialogflow        0.5.1
    google-api-core   1.4.1
"""
import dialogflow
from google.api_core.exceptions import InvalidArgument

DIALOGFLOW_PROJECT_ID = 'google-project-id'
DIALOGFLOW_LANGUAGE_CODE = 'en-US'
GOOGLE_APPLICATION_CREDENTIALS = '1234567abcdef.json'
SESSION_ID = 'current-user-id'

text_to_be_analyzed = "Hi! I'm David and I'd like to eat some sushi, can you help me?"

session_client = dialogflow.SessionsClient()
session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)

text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)
query_input = dialogflow.types.QueryInput(text=text_input)
try:
    response = session_client.detect_intent(session=session, query_input=query_input)
except InvalidArgument:
    raise

print("Query text:", response.query_result.query_text)
print("Detected intent:", response.query_result.intent.display_name)
print("Detected intent confidence:", response.query_result.intent_detection_confidence)
print("Fulfillment text:", response.query_result.fulfillment_text)

As you can see, the function requires a session_id. This is nothing but a value that allows us to recognize the session in which you are working. For this purpose, I suggest that you use the ID of the user to retrieve it easily.

Finally, in order for the Python code work properly, you will need a fresh token to call the artificial intelligence of our chatbot. In fact, the V2 (version 2) of the Dialogflow API relies on an authentication system based on a private key associated with the Google Cloud Platform Service Account, instead of the access tokens. Please follow the tutorial here to accomplish this step. Through this simple procedure, it will be possible to obtain a private key in the JSON format. Be sure to store the file in a safe place because if you lose the key, you will have to generate a new one by going through the whole procedure again.

Pros and Cons

In my opinion, the great power of this tool lies in the ability for you to design your own business logic through the use of an intuitive console and easily integrate external modules. Moreover, Dialogflow can scale to thousands of users, being built on Google Cloud Platform, the scalable cloud infrastructure provided by Google.

Here, then, my list of Dialogflow pros and cons for you.

PROS:

  • Many languages supported; currently, more than ten languages are supported including Russian, Italian, Chinese and Hindi (I invite you to check the complete list at dialogflow.com/docs/reference/language)
  • The context functionality, which allows the defined intents to follow the sense of the conversation
  • The “fallback” intent
  • Progressive chatbot training
  • Graphical interface to test the conversational flow in real time
  • Many integrations with third-party messaging systems (at the time the article was written, those include: Google Assistant and Google Home, Android, Facebook Messenger, Slack, Viber, Twitter, Twilio, Skype, Telegram, Kik, Line, Cisco Spark, Amazon Alexa, and Microsoft Cortana).
  • Many programming languages are supported and SDK provided to create advanced custom logic for your chatbot

CONS:

  • You need to be able to use the SDK to implement advanced logic
  • No way to deny a context, as the matching of intents can only be triggered and not blocked
  • You have to enter all the variants and synonyms of the intent to get a good result (the NLP support does not seem to be 100% for every supported language yet).

Conclusion

If you’re new to Python, Cloud Academy offers an Introduction to Python Learning Path that guides you through the background and basics of Python so you get you the skills and knowledge you need to get started quickly as possible.

If you’re just starting with Dialogflow and this post has piqued your interest, I highly recommend you starting with the “prebuild agents.”  These are customizable agents specialized in different areas of knowledge that you can simply import into your chatbot. Then you can set up a webhook as described in this post and get the agent responding. All the intents and even entities of the agent are editable and ready to use. Feel free to add more functionalities directly from the Google Cloud Platform or enhance your algorithms with NLP.

Cloud Academy