How to code a Webhook Development with Python for Dialogflow to build Chatbots (2022)

In this blog, we are going to see how to write a webhook in python for Dialogflow a platform for building chatbots.

A webhook is nothing but a file whose logic is written for chatbots.

As in my previous blog also I have given you a brief overview of Dialogflow. The focus area of this blog is webhook.

How to code a Webhook Development with Python for Dialogflow to build Chatbots (1)

You could find out many resources on how to write a webhook in Python but I feel that there is a lack of connectors between the available blogs. Hence, here I will try to make this blog very useful who is looking to build chatbots using Dialogflow.

What is a webhook?

In Dialogflow, it will provide you the interface for creating the intents, entities with its pre-built machine learning intelligence.

But after creating an intent there will be a response associated with every intent to continue the conversation. Sometimes it happens that, you don’t want a static response or question for the user, it could be fetching the information from some API and asking the user to confirm on it or it could be calculating the years based on start date i.e., any kind of logic which requires some kind of software outsourcing services, this is the point where webhook/fulfillment comes into the picture.

Fulfillment: It has some function as webhook, but the only difference is it is linked up with Firebase directly and you can also use the inline editor provided by the Dialogflow itself. For short programs fulfillment is good but for major chatbots go for webhook.

Cloud functions on GCP

GCP stands for Google Cloud Platform it is a cloud platform built by Google which has so many functionalities available like Data Storages, Cloud functions(to store your program files), App Engine(Deploy your App), etc.

Questions are:

How Cloud functions and webhook are connected?

So, Dialogflow just gives a UI where we can embed the link of webhook but that webhook needs to be deployed somewhere. You can use any other servers to deploy your webhook like Heroku, or AWS cloud platform, etc.

If you want to look at how to deploy python code using Heroku, go to -: Link of Heroku blog

(Video) Webhook: Python | Part - 9 | Create Chatbots using Dialogflow(API.AI) & deploy on GCloud

As Heroku is a free hosting service, it will delay your response sometimes. So here we will host our webhook on Cloud Functions.

How to enable webhook and deploy them from a local machine?

How to code a Webhook Development with Python for Dialogflow to build Chatbots (2)

So, in the web interface of Dialogflow, if you will go to the fulfillment tab then enable webhook and along with the URL, in the end, will be displayed a by default webhook name. You can replace the name of webhook according to your need, I kept it webhook (highlighted with an arrow in the above image).

Now, you have the whole URL.

How to deploy cloud functions on GCP?

Part 1: Setup Google Cloud SDK

Here are the following steps to setup a function which can be deployed from local machine to cloud functions -:

Step 1: Create a gcp account

Make a gcp account from which you have created a Dialogflow account.

Step 2: Make a folder which contains python files or whatever files you want to use

After creating a folder that contains files, as these files are available locally but we want to deploy to some cloud service.

We have to setup google cloud sdk first and then initialize the gcloud sdk i.e, selecting the project and then deploy the local files to a cloud function.

You can create multiple webhooks.For Dialogflow handling the conversation, we have to deploy a single webhook.

(Video) Dialogflow Python Webhook Example | Python Flask Webhook | Dialogflow Tutorial | Responsive Webhook

Setup google cloud sdk

We need to setup a google cloud sdk i.e, needs to install using an .exe

  • Step 1: Download the google cloud sdk installer.

  • Step 2: Install using the .exe file

    How to code a Webhook Development with Python for Dialogflow to build Chatbots (3)

  • Step 3: For deploying anything on GCP, you need to initialize the gcloud

    In initializing the gcloud what you have to do -:

    - Select the project on which you want to perform specific operations like deploying to cloud functions, using storage options like BigQuery, Firestore, etc. (You can see all these operations in the interface of google cloud. You can check the login to gcp in a browser using and please choose the appropriate id in the top right corner)


  • What is imbalanced data?

Let’s see some demo -:

(Video) How To Build Chatbot With Google DialogFlow | Build Chatbot

After installing the sdk for Google cloud, we need to initialize the Google cloud (i.e, a project on which we want to work with a local machine).

Step 1: Open up a command prompt and type command -: ‘gcloud init’

How to code a Webhook Development with Python for Dialogflow to build Chatbots (4)

As you can see if you don’t have any configuration you need to select 2, otherwise 1.

As I already set it up, I have to choose 1 but in your case, you have to choose 2 and it will ask details like email id, etc.

Now we need to choose the project on which we want to work.

How to code a Webhook Development with Python for Dialogflow to build Chatbots (5)

If you have many projects it will come up and you have to select the project number in which you want to work.

Finally, it will be setup.

Part 2: Deploy Webhook has written in Python to Cloud functions

As we have already seen how to initialize Google cloud and now wants to write a webhook in Python development services.

Sometimes it may come in mind that we need to create different webhooks for different functions for a chatbot. But it is not the case there is only one webhook that contains the main file to execute all functions let’s see how.

(Video) DialogFlow Chatbot with Python | #142

Step 1: Create a folder which will be deployed to CF (Cloud functions)

Let’s say I have created a file in that folder -:

1. from flask import Flask, request, make_response, jsonify2. import pandas as pd3. import logging4. import os5. 6. def webhook(request): ## request parameter will contains the request body 7. req = request.get_json(force=True)8. ## Printing the request body to check what information is retrieved9.'Request: '+str(req))10. 11. ## As the request body is the the response from dialogflow if user entered something which is present in our chatbot flow and matches with the intent present ## extract the name of the intent which is detected.12. detect_intent = req["queryResult"]["intent"]["displayName"]13. 14.'Intent Detected: '+ str(detect_intent))15. 16. ## This is how we are going to map the the intent with a function to perform that functionality for that intent17. if detect_intent == 'Default Welcome Intent':18. res = welcome(req)19. if detect_intent == 'Location':20. res = location(req)21. 22. ## Similarly you can create multiple functions and map to your intent ## returning the res from the function for which intent is matched.23. return {'fulfillmentText':res}24. 25. def welcome(req):26. ## Checking what are the parameters available27.'queryResult').get('parameters'))28. 29. ## Here you can do lots of stuff ## Checking the length of input, getting start date etc.30. 31. return 'welcome intent'32. def location(req):33. ## Checking what are the parameters available34.'queryResult').get('parameters'))35. 36. ## Here you can do lots of stuff ##Using some api to fetch the weather data for that location ## Just an example37. data = requests.get(‘https://get-weather-data?location=’India’’)38. return str(data)

Step 2: Creating a requirements.txt file to let the CF know which the required dependencies are.

Requirements.txt -:


You can add on the functions in and libraries in the requirements.txt file according to t your need. You can also deploy other files by creating other files like excel sheet etc in the same folder.

Step 3: Deploying the created webhook to CF

Open the command prompt to the specified path where your webhook folder is located

Use command -: gcloud functions deploy <webhook_name> --runtime python37 --trigger-httpIn our case, gcloud functions deploy webhook --runtime python37 --trigger-http</webhook_name>

And it will be deployed to your cloud functions in GCP.

Now, if you will go to and go to cloud functions ( then you see the created functions like webhook etc. there you can check logs and other details for your functions.

As we have created and also used the link in Dialogflow for Chatbot.

If you will test your chatbot then it will use this webhook to return a response.


We have seen how to use Cloud functions and how to deploy webhook of Python to CF. Cloud function supports different languages like Nodejs, C#, etc. You can choose the environment whatever you like.

(Video) Learn to Build a Chatbot with DialogFlow and Python


How do I connect Dialogflow to Webhook? ›

Enable and manage fulfillment
  1. Go to the Dialogflow ES Console.
  2. Select an agent.
  3. Select Fulfillment in the left sidebar menu.
  4. Toggle the Webhook field to Enabled.
  5. Provide the details for your webhook service in the form. ...
  6. Click Save at the bottom of the page.

How do I create a Webhook API in Python? ›

  1. Step 1: Install Flask to your Python Environment. The first step in setting up Python Webhook Integration is to install Flask to your Python environment. ...
  2. Step 2: Create a Web Service. Once you have installed Flask to your Python environment, you need to create a Web Service using Flask. ...
  3. Step 3: Run the Flask Server.
26 Oct 2021

How does Python integrate Dialogflow? ›

To use the V2Beta1 client library:
  1. Enable beta features. See agent general settings.
  2. Specify the V2Beta1 API in your code by using the following import statement: from google. cloud import dialogflow_v2beta1 as dialogflow.
3 days ago

How do you make a simple chatbot in Python? ›

In this Article, you will learn about How to Make a Chatbot in Python Step By Step.
  1. Prepare the Dependencies.
  2. Import Classes.
  3. Create and Train the Chatbot.
  4. Communicate with the Python Chatbot.
  5. Train your Python Chatbot with a Corpus of Data.
22 Sept 2022

Does Dialogflow require coding? ›

Today, Dialogflow stands out as one of the most well-built platforms for AI solutions and the only platform that you can use to build AI conversational agents, without any coding.

What is webhook in Dialogflow? ›

Webhooks are services that host your business logic. During a session, webhooks allow you to use the data extracted by Dialogflow's natural language processing to generate dynamic responses, validate collected data, or trigger actions on the backend.

What is webhook VS API? ›

An application programming interface (API) is a software interface that serves as a bridge between computers and applications. A webhook is a way for one application to deliver data to another app in real-time.

What is webhook example? ›

Some real-world examples of webhooks include: Automatically receive an email every morning about your first meeting in case you forget to check your calendar. Have Instagram photos upload automatically to Twitter accounts. Configure the doorbell to flash the lights when it rings.

How do I get webhook in Python? ›

Creating a webhook in Flask

We'll create a route in Flask that allows us to receive data on a /webhook path. Flask has an inbuilt . route() decorator that binds a function to a URL. The function receives data in JSON format from a POST request and displays the data on the terminal.

How do you make a webhook? ›

Create a Webhook
  1. Go to your stack, and click on the “Settings” icon on the left navigation panel.
  2. Click on Webhooks. ...
  3. Click on the + New Webhook button located at the top of the page.
  4. In the Create Webhook page, provide the following webhook details: ...
  5. Click on the Save button.

How do you send data to a webhook? ›

With webhooks, it's generally a three-step process: Get the webhook URL from the application you want to send data to. Use that URL in the webhook section of the application you want to receive data from. Choose the type of events you want the application to notify you about.

What algorithm does Dialogflow use? ›

Dialogflow uses two algorithms to match intents: rule-based grammar matching and ML matching.

Which is the package we use to integrate Dialogflow Python SDK with Google server? ›

  1. Azure.
  2. DevOps.
  3. Google Cloud Platform.
  4. Machine Learning.
14 Feb 2019

Does Dialogflow use machine learning? ›

When your agent is trained, Dialogflow uses your training data to build machine learning models specifically for your agent. This training data primarily consists of intents, intent training phrases, and entities referenced in an agent; which are effectively used as machine learning data labels.

How do you make a flask chatbot in Python? ›

Here are the 5 steps to create a chatbot in Flask from scratch:
  1. Import and load the data file.
  2. Preprocess data.
  3. split the data into training and test.
  4. Build the ANN model using keras.
  5. Predict the outcomes.
  6. Deploy the model in the Flask app.

Which is the package we use to integrate Dialogflow Python SDK with Google server? ›

  1. Azure.
  2. DevOps.
  3. Google Cloud Platform.
  4. Machine Learning.
14 Feb 2019

How do you integrate Python chatbot in HTML? ›

How to add ChatBot to your website
  1. Go to the Integrations section and select Chat Widget.
  2. Click on the Publish your bot section.
  3. Click Copy to clipboard to copy the code.
  4. Paste the code to your website's source code before the /body closing tag.
4 Apr 2022

How do you make an AI chatbot? ›

How to make a chatbot from scratch in 8 steps
  1. Step 1: Give your chatbot a purpose. ...
  2. Step 2: Decide where you want it to appear. ...
  3. Step 3: Choose the chatbot platform. ...
  4. Step 4: Design the chatbot conversation in a chatbot editor. ...
  5. Step 5: Test your chatbot. ...
  6. Step 6: Train your chatbots. ...
  7. Step 7: Collect feedback from users.
23 Aug 2022


1. Google Dialogflow w Fulfillment & Webhook + Python Flask!!
2. Build a Chatbot from Scratch - Dialogflow on Node.js
3. Making of simple AI Chat Bot using webhook | Python | | API.AI | ngrok
(Hemanta Nandi)
4. Tutorial 03 - Creating Dynamic Responses with Webhooks (DialogFlow)
5. How to Integrate Chatbot with Python Django Website
(Parwiz Forogh)
6. Whatsapp Business API Chatbot using Python, Dialogflow, Twilio Sandbox and AWS Lambda
(Ankit Malhotra)

You might also like

Latest Posts

Article information

Author: Neely Ledner

Last Updated: 11/01/2022

Views: 6123

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.