Dialogflow Fulfillment: Webhook Tutorial

Chatbots, Python Development, Machine Learning, Natural Language Processing (NLP)

Dialogflow is a chatbot building framework that helps you build and deploy your own chatbots to multiple platforms like Google Assistant, Facebook Messenger, Telegram, Twitter, Slack, Line, Viber and many others. It is powered by a Machine Learning based NLU (Natural Language Understanding). The feature rich Dialogflow lets you create chatbot with ease.

In this blog, we are going to take a look into one pretty useful feature of Dialogflow: Fulfillment. Using Fulfillment you can communicate with your server/database to generate dynamic responses. Fulfillment is deployed as a webhook which is supplied information extracted by Dialogflow’s natural language processing (NLP).

Let’s dig deeper into webhook to understand how it works and what benefits a chatbot developer can leverage using it.

What is webhook?

A webhook is an HTTP request that is sent automatically whenever certain criteria is fulfilled. A webhook can be created in any server side programming language like Python, PHP or Node.js. You can read more about webhook here.

In Dialogflow, a webhook can be used to fetch data from your server whenever a certain intent having webhook enabled (don’t worry, we will see that) is invoked. The information from the intent is passed to the webhook service to receive result.

We are going to use Ngrok, (a web tunneling tool) that can be used to check webhooks from your local server. You can download Ngrok from ngrok.io.

Let’s start building our own Dialogflow agent with webhook using Python and deploy it using Ngrok.

Install Python

Download and install python from here as per your supported OS.

Install Flask using PIP

pip install Flask (https://pypi.org/project/Flask)

Creating webhook using Python

I am going to use Flask (a light-weight python web framework ) for creating a webhook. Let’s create a basic flask app.


# import flask dependencies
from flask import Flask

# initialize the flask app
app = Flask(__name__)

# default route
@app.route('/)
def index():
    return 'Hello World!'

# create a route for webhook
@app.route('/webhook')
def hello():
    return 'Hello World!'

# run the app
if __name__ == '__main__':
   app.run()

Now, run the app using the following command.

python app.py or FLASK_APP=hello.py flask run

Run the webhook using Ngrok

Ngrok is a web tunneling tool that provides a way to test APIs and webhooks from local server. There are two versions available. I am using the free version with registration on ngrok.io.

To run ngrok using the following command: ngrok http <port_number>
e.g. ngrok http 5000 (for flask app)

How to setup webhook in Dialogflow

Dialogflow provides an option on the left sidebar known as Fulfillment. Just enter your webhook url generated by ngrok and your are done.

Enabling webhook for intents

Enable the “Enable webhook call for this intent” switch.

Setup training phrases, parameters and action for the intent

Training phrases are the sentences that help to identify the intent.

We have to set up action name in dialogflow to identify which action has been invoked based on the user query.

Building fulfillment responses from webhook

Webhook responses should be in a proper format so that it can understand what to display to the user.

You can refer various JSON formats for webhook on this github page.

We are going to build some responses in python using this formats. Types of responses available is: Simple Response, Basic Card, Suggestions, List Card, Browse Carousel, Carousel Response. We will building all these responses in the upcoming blog.

Fetching action and parameters from request JSON.


# function for responses
def results():
    # build a request object
    req = request.get_json(force=True)

    # fetch action from json
    action = req.get('queryResult').get('action')

    # return a fulfillment response
    return {'fulfillmentText': 'This is a response from webhook.'}

# create a route for webhook
@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
    # return response
    return make_response(jsonify(results()))

You can also check this Python Library we created to simplify building the JSON responses for Dialogflow.

Hope this tutorial will be helpful to you in integrating webhook in your dialogflow app. If you have any question then post it in the comment. We’ll respond to it soon.


Feel free to contact us or email at letstalk@pragnakalp.com for your chatbot development requirement. We would love to cater to your development needs at competitive rates with utmost quality and highest customer satisfaction.

Leave a Reply

Your email address will not be published. Required fields are marked *