Xlera8

Dialogflow Tutorial: Pagination Functionality in Chatbot using Python + Django

Pragnakalp Techlabs

Hey, Chatbot Creators! In this blog, we will be showing the concept of pagination implemented with Dialogflow fulfillment. In many scenarios, In which there is a long list of items to display on the webpage or chatbot, in this case showing a long and long list at once is not proper way and not convenient for users too. To solve this issue generally, pagination is being used. Pagination functionality can be described as splitting the whole data into small chunks called pages and opening pages one by one. Same as we read the next page as well as the previous page in the book.

We will discuss about the implementation of the pagination functionality in the Dialogflow chatbots. As in some cases, we have to pass a very long list of items in the Dialogflow responses. This can be implemented by managing the data in the context. Let’s understand this in more detail.

We are using a webhook to handle the fulfillment request and to manage the Dialogflow responses.

To make pagination functionality work, when we are giving next or previous page command, the last page’s starting point and page size should be known. Keeping page size constant, we are storing the current page’s starting point of the list in the parameter of the output context for future next or previous page command.

Here, we will use the Python language to write the webhook code. To set up the python interpreter in your system, get the information from here.

1. Crawl Twitter Data using 30 Lines of Python Code

2. Blender Vs Rasa open source chatbots

3. Picture my voice

4. Chat bots — A Conversational AI

Django is a python-based web framework. Here we used Django to create a webhook that will communicate with Dialogflow. Open the terminal in the Linux system and run below command to install Django.

Follow the following steps to set up a Django project in your system.

This will create Pagination_Function Project in the following structure:

Now run the following commands:

Now add the newly created app myapp in /Pagination_Function/settings.py file.

So now it’s time to setup Dialogflow, which manages the responses.

Open Dialogflow and sign in with your Google account. Create a new agent. Give this agent name Pagination_Functionality. After that use the CREATE button to create an agent.

Let’s create a new intent of this agent and give this a name. Also, set the training phrases and action for this newly created intent. Enable fulfillment request call to give responses from the webhook.

Now create two other intents with the name Next and Previous. Same as before, set its training phrases, and action and also enable fulfillment request call.

Now set input context in the Next and Previous intent to manage the flow of context from and it’s parameter’s value.

Now our DialogFlow agent is ready.

To create a webhook URL using Django, copy the following code in your Django project’s file Pagination_Function/Pagination_Function/urls.py

This will point a URL with path /webhook to webhook function in the view.py file. Let’s create this function in the file Pagination_Function/myapp/views.py copy the following code to that file.

As you can see, we have imported the df_response_lib from the folder library. This library can be cloned from here. Create a new folder with the name library inside the Django project and put the file df_response_lib.py inside of this folder.

Now to run the Django server, run the following command.

If run without any error then congratulations. Your webhook setup is successfully done.

To access just created URL /webhook publically and to connect webhook with the DialogFlow agent, we need a web tunneling tool named NGROK. Download and install then run bellow command to generate URL.

You should get the output like below:

Copy URL from this and paste in the file Pagination_Function/Pagination_Function/settings.py, in the ALLOWED_HOSTS list.

This setup enables calling webhook using this URL. Now set this webhook URL to fulfillment section of the agent to enable webhook call from the DialogFlow.

Open DialogFlow and in the created agent open FulFillment Section and Enable Webhook. In The URL section of the webhook, set the URL created by NGROK with the /webhook path. (Must include ‘/’ at the end of the URL, otherwise, it will create error.)

Yupiiee! Our DialogFlow + Django setup is ready for the Pagination functionality in the chatbot.

Now, it’s time to turn the pages in the chatbot by trying in the test console located at the right side of the window of the DialogFlow. Start conversation by saying Pagination and you will see a list of items, then try for Next and Previous.

Fill up the form on our blog to download the sample agent and python/django code

Feel free to comment your doubts/questions. We would be glad to help you.

If you are looking for Chatbot Development services then do contact us or send your requirement at [email protected]. We would be happy to offer our expert services.

Source: https://chatbotslife.com/dialogflow-tutorial-pagination-functionality-in-chatbot-using-python-django-e7c5091f04d4?source=rss—-a49517e4c30b—4

Chat with us

Hi there! How can I help you?