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

Fine-tuning SDXL with childhood pictures → audio-reactive geometries – [Experiment]

After a deeply introspective and emotional journey, I fine-tuned SDXL using old family album pictures…

4 hours ago

Beyond Accuracy: 5 Metrics That Actually Matter for AI Agents

AI agents , or autonomous systems powered by agentic AI, have reshaped the current landscape…

4 hours ago

Apple Workshop on Reasoning and Planning 2025

Reasoning and planning are the bedrock of intelligent AI systems, enabling them to plan, interact,…

4 hours ago

MediaFM: The Multimodal AI Foundation for Media Understanding at Netflix

Avneesh Saluja, Santiago Castro, Bowei Yan, Ashish RastogiIntroductionNetflix’s core mission is to connect millions of members…

4 hours ago

Scaling data annotation using vision-language models to power physical AI systems

Critical labor shortages are constraining growth across manufacturing, logistics, construction, and agriculture. The problem is…

4 hours ago

Start Your Surround Sound Journey With $50 off This Klipsch Soundbar

This soundbar is just the beginning, with the option to add wireless bookshelf speakers or…

5 hours ago