How to modify the chatbot timeout
# 🤝help
s
Hey, As I mentioned in the community forum, would be great to have the ability to customize the timeout of the chatbot, by default is 5min. For some use cases, the user could take some time to answer a question from the chatbot. If this time exceed the timeout (5min) the chatbot will start from the initial node again, restarting the whole conversation. Being able to customize it, each chatbot could define it's timeout depending on the need and avoid restarting the conversation. Thanks!
a
Hi @stale-author-40821, this is a great question! Long timeout lengths can be a security concern, especially if the user is sending the bot personal information. The workaround, then, is not to extend the timeout period but change how the bot reacts to a user coming back from a timeout. There's a special "Conversation Timeout" flow that gets triggered right before a timeout occurs. You can use this flow to save the conversation state as a user variable (https://botpress.com/docs/cloud/studio/variables/#user-variables). Then, when a conversation starts, the bot can check for a state associated with the user and, if it's present, load it and ask the user if they want to continue from where they left of (or just do that automatically).
s
Thanks @acceptable-kangaroo-64719 ! Uhm ok. The main issue is that I have to store the full state of the bot before the timeout, and try to recover from it. To recover the chatbot from a timeout (if exists) I have to do it just after the start, right? There is no option like a flow, or something like that.
a
correct, you would need to do it right after the "start" node in the main flow
s
ok, I will give it a try and see how it could work. Thanks!
f
hi @stale-author-40821, When you mentioned "store the full state", did you mean the "event" or the "event.state"
The simplest way is like what Gordy said, just in the user variables, add a flow and a node. Update those variables on the checkpoint you want. Then on the entry node, check if those variables exist or not, if they exist then send the user to the respective node and flow. Also store in the user variables the important information that you want your bot to remember
s
Hey guys I've been testing with this option and seems doable and easy to implement. But I'm having some issues with the user variables, probably due to a lack of knowledge/info... The thing is that i'm storing some information I need to recover from a timeout in a user variable, inside the "Timeout workflow". But, when I try to read this variable in the "Main workflow" to recover, this variable is always empty... Something I have to take into account? 🤔
a
Workflow variables are limited to the workflow, so a workflow variable from
Main
would be empty in
Timeout
or vice-versa. User variables are supposed to be global though, and available in every flow. You can read more about variables here https://botpress.com/docs/cloud/studio/variables/
s
Yeah I imagined that, but... If I print a workflow variable, which is filled in the Main workflow, in a Timeout node, it contains the correct information. I mean, in the attached screenshot, this variable contains the information I added previously. If I store this in the
user.message_history
var, it should be global, right?
a
Correct
s
so this is what is happening.. 🥲 1. I have 2 user vars (
last_node
and
message_history
). 2. When I start a new conversation, these vars are empty 👍 3. When I force a timeout, the workflow has the information and it's stored in the user var (just
message_history
in that case) 👍 4. When I send another message to the chatbot, the user vars have no value in the Main workflow 😭 Something am I doing wrong? 🙏
Hey! Any update on that? I'm a bit blocked 😅
a
Hey, it looks like your variables are workflow variables and not user variables. To make user variables, you need to go into chatbot settings and then the "variables" tab.
s
Yep, this is what I did. I've created these 2 variables (
last_node
and
message_history
) in the user variables section, but... seems not working
a
I gave it a try on my own and was able to get saving user variables to work just fine. Here's some pics from my setup.
also, all of my capture cards have "skip if variable filled" turned on and retries set to 1
s
well i'm doing mostly the same: 1. save the variables in the
user
object, in the timeout flow 2. print the whole
user
object in the main flow i've been changing most of the configuration and tried so many different things but it's not working at all
a
does it work if you change all your user vars to workflow vars at the start?
s
nope. every time the main workflow starts, the
user
object is completely empty
a
How are you restarting the chat? Are you clicking the little 🔄 icon or are you starting each time as a new user?
s
i'm clicking the 🔄 , yep
a
are
user.message_history
and
workflow.chatgpt_message_history
the same type?
s
yep, i've deleted and copy&pasted to double-check if this could be the issue
a
I tested again on my side, this time using array variables instead of strings, and it's working just fine. Here's my setup: 1. I have an
After_Incoming_Message
hook called
save_history
that runs this code:
Copy code
js

  if (!workflow.message_history) {
    workflow.message_history = []
  }
  event.state.session.history.map((message) => {
    let entry = `${message.sender}: ${message.preview}`
    console.log(entry)
    if (!workflow.message_history.includes(entry)) {
      workflow.message_history.push(entry)
    }
  })
  console.log(workflow.message_history)
2. In my
Timeout
flow, I'm running this code:
Copy code
js
if (workflow.username) {
  user.username = workflow.username
}
if (workflow.usercolor) {
  user.usercolor = workflow.usercolor
}
if (workflow.userpet) {
  user.userpet = workflow.userpet
}
if (workflow.message_history) {
  user.past_message_history = workflow.message_history
}
3. Bot
user.past_message_history
and
workflow.message_history
are arrays of strings.
is that similar to your setup?
s
uhmm, I don't have any hook as I'm saving the variables directly in the
Timeout
flow. When the timeout triggers, I my
workflow
variables have the correct value, so I guessed I can store directly the value into the
user
vars. Despite that, the rest is quite the same..
a
Thanks for the video. It looks like there's something preventing user variables from being saved in the Timeout flow. I've filed an internal bug report. In the meantime, I recommend using a hook to store chat history to the user variable- something like this:
Copy code
js
  if (!user.message_history) {
    user.message_history = []
  }
  event.state.session.history.map((message) => {
    let entry = `${message.sender}: ${message.preview}`
    if (!user.message_history.includes(entry)) {
      user.message_history.push(entry)
    }
  })
s
ohhh cool, I'm becoming crazy haha. Thanks @acceptable-kangaroo-64719 , i'll do that meanwhile
158 Views