Hi,
I have a somewhat complicated Bot (Multiple actions, quite a few intents, forms etc). To enhance the user experience we decided to have a logical session timeout where the last time user interacted with the bot is older than N seconds, we want to reset the session. i.e. clear all slots, deactivate active forms etc. What is the best way to do this?
One simple way of doing it is for all actions to insert a snippet like this: But seems like there should be a better way of doing this in a more centralized and less repetitive manner. Any advice from anyone?
try:
# we are taking 1-last because last event is the user utterance that lead to this action.
last_event_time = tracker.events[-2].get('timestamp')
except:
logger.error("unable to obtain last event time. Defaulting to -1 which will trigger a restart")
last_event_time = -1
current_ts = datetime.now().timestamp()
time_since_last_event = current_ts - last_event_time # this is in seconds
logger.info(f"current ts ={current_ts}, last_event_ts = {last_event_time}, session time_since_last_event={time_since_last_event}")
if time_since_last_event > SESSION_LIFETIME:
dispatcher.utter_template("utter_greet", tracker)
logger.info(f"Restarting session since {time_since_last_event} is greater than session lifetime {SESSION_LIFETIME}")
return [Restarted()]