How can I automatically timeout Ai Agent sessionId and email chat transcripts when the user becomes inactive?

Hi!

I’ve got a chatbot workflow running on my self-hosted n8n (v1.89.2) that’s working well, but I’m struggling with one annoying problem - users who just disappear mid-conversation.

My current setup

I built an AI agent chatbot where customers interact with an AI agent. When the conversation ends, I want to receive the transcript via email for review. The chatbot will be embedded to a webpage.

Right now, I’ve got the email part working, but it only triggers when someone explicitly types “end chat” or something similar. The problem is… almost nobody does this! They just stop responding and leave the chat hanging.

What I’m trying to build

I need a way to detect when a user has gone inactive and automatically:

  1. Close their session
  2. Maybe also send them a “chat has ended” message if they are still in the webpage
  3. Email me the transcript

My ideal flow would be:

  • User sends a message (with a sessionId)
  • Start/reset a 5-minute timer for that specific session
  • If another message comes in, reset the timer and continue the chat
  • If the timer expires:
    • Send the user a “This chat has ended due to inactivity” message
    • Email me the transcript using the chat history (already stored in PostgreSQL) this is setup as a separate workflow tool to be called, it takes in the sessionID to pull the chat history from the DB and send the email to me.

I already store all chat messages in PostgreSQL, so I have the transcript data covered. I just need a reliable way to implement these session timeouts and trigger the appropriate actions when a user goes quiet. I’m wondering what’s the best approach here.

Any help would be greatly appreciated!

Thanks!

1 Like