N8N Bot Mixing User Interactions

I have built a customer support bot on telegram that is meant to respond to different community members or customers who interact with it. The challenge I am facing is that the Bot mixes up user interactions especially when different customers are interacting with it at the same time.

I have added a code to fetch customers ID so as to keep track of each user activities and keep track of it as it separate the sessions also adding a session expiry mechanism which does not seem to work.

// Access the static data
const staticData = this.getWorkflowStaticData('global');

// Use dynamic values for userId and chatId
const userId = $json["message"]["from"]["id"]; // Extract user ID from the incoming message
const chatId = $json["message"]["chat"]["id"]; // Extract chat ID from the incoming message

// Check if session data exists for the user
if (!staticData[userId]) {
  staticData[userId] = {
    chatId: chatId,
    state: 'awaiting_input',
    locked: false,
    timestamp: Date.now(),
  };
}

// Retrieve session data
const sessionData = staticData[userId];

// Check for session expiry (e.g., 30 minutes)
const sessionExpiryTime = 30 * 60 * 1000; // 30 minutes in milliseconds
if (Date.now() - sessionData.timestamp > sessionExpiryTime) {
  // Reset session data
  staticData[userId] = {
    chatId: chatId,
    state: 'awaiting_input',
    locked: false,
    timestamp: Date.now(),
  };
}

// Check if the session is locked
if (sessionData.locked) {
  // If locked, skip processing or handle accordingly
  return [{ json: { message: 'Session is currently locked.' } }];
}

// Lock the session
staticData[userId].locked = true;

// Update session data
staticData[userId].state = 'processing_input';
staticData[userId].timestamp = Date.now();

// Simulate processing (e.g., wait for user input)
// ...

// Unlock the session after processing
staticData[userId].locked = false;

// Return the session data for further use in the workflow
return [{ json: sessionData }];```

Any assistance will be of help :)

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Running n8n via : Desktop
n8n version : Web Vesrion
Operating system : Windoew 8 64 Bit

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.