Categories: FAANG

Extend Session Timeout For Dialogflow CX

What is a Session Timeout?

Once your webhook service receives a webhook request, it needs to send a webhook response. The following limitations apply to your response:

  • The response must occur within a timeout (max 30 seconds) that you configure when creating the webhook resources, otherwise, the request will time out.
  • The response must be less than or equal to 64 KiB in size.

In this, we increase the session timeout above 30 seconds to avoid the error of webhook timeout.

Why are we required to increase Session Timeout?

When requesting data from an API or retrieving information from databases or AI models like ChatGPT, Claude, Gemini, or LLaMA, there may be delays exceeding the maximum timeout limit of 30 seconds. To prevent encountering a webhook timeout error, extending the session timeout duration is necessary.

How to Increase the Session Timeout?

Here’s a step-by-step guide:

1. Dialogflow Setup

Sign in to the Dialogflow CX console and create a new agent. Give the Agent name as you like. You will find the below screen for the new agent:

Now navigate to ‘Start Page’. Click on the ‘Default Welcome Intent’ and add a new page of a name you like we have used ‘page_1’. Click on Save after adding a new page.

Navigate to ‘page_1’. Click on the Routes to add a new route and create a new intent ‘intent_1’.

In ‘intent_1’, add Training Phrases like ‘I want to increase the webhook’ and Save the intent.

In ‘intent_1’, add a new parameter ‘$session.params.text = null’ in Condition of ‘page_1’.

To do the transition, add a new page ‘page_2’ and click Save after adding a new page.

Navigate to page ‘page_2’ and in Condition, add parameter condition ‘$session.params.text = null’.

Now, Enable webhook (create webhook if not created) and add webhook with tag ‘response’. Click on Save after adding a webhook.

Add a new route in ‘page_2’ and in Condition, add parameter ‘$session.params.text = True’.

Now, add an agent response, which will be displayed after the webhook timeout. Click on Save after adding the agent response.

Now, click on the State Handler, select Event Handler, and click Apply.

To do the Transition, add a new page ‘page_3’, and click Save after adding a new page.

Navigate to page ‘page_3’. Click on the Routes to add a new route and in Condition, select ‘Customize expression’ and add ‘true’.

Now, Enable webhook and add webhook with tag ‘get_response’. Add a previous page ‘page_2’ and Save after adding a previous page.

2. Webhook Code

from flask import Flask, request, jsonify
import time

app = Flask(__name__)

# Webhook endpoint to handle POST requests
@app.route('/webhook', methods=['POST'])
def webhook():
# Get the JSON data from the request
data = request.get_json(silent=True, force=True)
# Extract the tag from the JSON data
tag = data["fulfillmentInfo"]["tag"]
target_page = data['pageInfo']['currentPage']
if tag == 'response':
parameters_sending(target_page)

if tag == "get_response":
# Simulate delay and then fetch content from a file
count = 0
while True:
time.sleep(1)
count += 1
print("entering get response")
with open("user_name.txt", "r") as file:
content = file.read().strip()
if content:
# If content exists, prepare response with the content
reply = {
"fulfillmentResponse": {
"messages": [
{
"text": {
"text": [
f'Message received: {content}'
]
}
}
]
},
"sessionInfo": {
"parameters": {"test": "True"},
},
}
# Clear the content of the file
with open("user_name.txt", "w") as file:
file.write("")
return jsonify(reply)

return {
"fulfillmentResponse": {
"messages": [
{
"text": {
"text": [
f'Completed under webhook timeout limit.'
]
}
}
]
},
"sessionInfo": {
"parameters": {"test": "True"},
},
}

# Function to perform a background task
def parameters_sending(target_page):
time.sleep(8)
print("completed")
# Write a message to a file
with open("user_name.txt", "w") as file:
file.write("Your Testing Is Successfully Executed.")

response= {
"sessionInfo": {
"parameters": {"test": "False"},
},
'targetPage': target_page

}
return response

# Main function to start the Flask app
if __name__ == '__main__':
app.run(debug=True)

Conclusion

Extending the session timeout from the default 30 seconds is important to handle any delays that might occur when getting data from APIs or databases. By giving more time for responses, we avoid running into errors that could disrupt the flow of information between services. This adjustment ensures that operations run smoothly, ultimately enhancing the overall user experience.

Originally published at Extend Session Timeout For Dialogflow CX on April 18, 2024.


Extend Session Timeout For Dialogflow CX was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.

AI Generated Robotic Content

Recent Posts

10 Ways to Use Embeddings for Tabular ML Tasks

Embeddings — vector-based numerical representations of typically unstructured data like text — have been primarily…

9 hours ago

Over-Searching in Search-Augmented Large Language Models

Search-augmented large language models (LLMs) excel at knowledge-intensive tasks by integrating external retrieval. However, they…

9 hours ago

How Omada Health scaled patient care by fine-tuning Llama models on Amazon SageMaker AI

This post is co-written with Sunaina Kavi, AI/ML Product Manager at Omada Health. Omada Health,…

9 hours ago

Anthropic launches Cowork, a Claude Desktop agent that works in your files — no coding required

Anthropic released Cowork on Monday, a new AI agent capability that extends the power of…

10 hours ago

New Proposed Legislation Would Let Self-Driving Cars Operate in New York State

New York governor Kathy Hochul says she will propose a new law allowing limited autonomous…

10 hours ago

From brain scans to alloys: Teaching AI to make sense of complex research data

Artificial intelligence (AI) is increasingly used to analyze medical images, materials data and scientific measurements,…

10 hours ago