As you might know, Dialogflow contexts expire within 10 minutes (or 20 minutes, depending on who you ask). In any case, there is a time limit for all contexts and they expire after that time.
Sometimes, people need to extend the context duration for long running chat scenarios.
This is what someone asked me in a comment:
Yeah i do have undeterministic values for my context variables. I’m writing a Chatbot for my master thesis. I do have different courses – the user is able to get information about it. I use the context variables to track about which course he is now talking about and which intents i have to use (intends that are shared among courses). If the user wants to change the topic to another course i’m setting all the context variables to 0 and initialize the new ones in the new intent and topic.
I’m currently working on getting the lifespan higher than 10 minutes. I want to try to save the context variables from the current sessionID and send it back to dialogflow. I hope this gonna works
Do you have another solution for this 10 minutes lifespan problem?
Would be very greatful about an answer!
It is solvable, but requires some extra effort on your part.
There are two prerequisites to getting this to work:
- custom integration
- authenticated user
If your users are not authenticated, or you don't want to develop a custom integration, then there isn't any way to do this.
How to extend the context lifespan beyond 10 minutes
When you use a custom integration, you are relaying the user's request to Dialogflow's REST API by calling the detectIntent method.
This method actually takes in the contexts object as a parameter:
In other words, you can ask Dialogflow to provide a response to your query as if the current context is set to <your_context_name>. As you can imagine, this means if you can figure out a way to store the context long term, you can have long running contexts in your conversations.
So here are the steps to create long running contexts in your conversation.
1 Use a custom integration
Relay user's message to Dialogflow's REST API and return response to the user
2 Save output context after each response to your DB
Each time you get a response from the API, make sure you save all the output contexts into your DB. You will need to associate it with a userID (which is why I mentioned the authentication).
3 Next time user logs in, get the last set context
When the user logs in next time (i.e. next time you see a message from the same userID), fetch the last known output contexts
4 Append the last set contexts to your detectIntent API call
Now pass the contexts object you retrieved from your DB as part of the detectIntent API call.
Your query will now behave as if the old context was still active.