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

Using depth maps and weight noising to get better character LoRAs

A few weeks ago I introduced a new method for training style LoRAs which has…

11 hours ago

The Statistics of Token Selection: Logits, Temperature, and Top-P Walkthrough

When large language models, or LLMs for short, produce outputs, several criteria are at stake,…

11 hours ago

Process financial documents using Amazon Bedrock Data Automation

Financial institutions process thousands of documents daily, including tax forms, loan statements, and purchase orders.…

11 hours ago

Introducing Google AI Threat Defense to help you outpace the adversary

aside_block <ListValue: [StructValue([('title', 'Summary of today’s news'), ('body', <wagtail.rich_text.RichText object at 0x7f00683723a0>), ('btn_text', ''), ('href',…

11 hours ago

Illinois Lawmakers Just Passed America’s Strongest AI Safety Bill

The bill requires companies like OpenAI, Anthropic, and Google to have third parties confirm they’re…

12 hours ago

Childlike AI uncovers why language grows more structured across generations

New research from the University of the Witwatersrand, South Africa, has significant implications for understanding…

12 hours ago