{
  "name": "Debounce Telegram support messages and aggregate replies with OpenAI",
  "nodes": [
    {
      "id": "2b135de7-4e4c-4cce-8654-9d988dde7764",
      "name": "Incoming Message Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -1072,
        176
      ]
    },
    {
      "id": "344a2a08-a088-482d-bf85-d3ea5ee776ad",
      "name": "Extract User Data",
      "type": "n8n-nodes-base.set",
      "position": [
        -848,
        272
      ]
    },
    {
      "id": "9e46901e-9ff6-4023-8be6-be35bc9f1f22",
      "name": "Check Active Session",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -624,
        272
      ]
    },
    {
      "id": "76ae5ad8-cfb9-4cec-a639-df635ceef428",
      "name": "Create New Session",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -176,
        176
      ]
    },
    {
      "id": "dac56742-199a-4ebc-b210-90b3bec26d1a",
      "name": "Append Message",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -176,
        368
      ]
    },
    {
      "id": "61534e84-91dd-4b2c-a23e-96554430543a",
      "name": "Fetch All Messages",
      "type": "n8n-nodes-base.postgres",
      "position": [
        496,
        176
      ]
    },
    {
      "id": "5fd74cd8-85c6-422e-8ba3-8312e68bac0e",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        944,
        176
      ]
    },
    {
      "id": "0a5b2d34-2391-4908-b9e8-fd790aa3bb89",
      "name": "Clear Session",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1520,
        176
      ]
    },
    {
      "id": "996930f3-2820-416c-802a-5432f1751df1",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        48,
        368
      ]
    },
    {
      "id": "0400571f-1068-475d-89bd-155fdf472159",
      "name": "Store Resume URL",
      "type": "n8n-nodes-base.postgres",
      "position": [
        48,
        176
      ]
    },
    {
      "id": "eb95a84c-3b73-4a4c-a304-7fd29f859969",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1016,
        400
      ]
    },
    {
      "id": "e017160d-204e-4b19-8d73-61a42fca7516",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -1072,
        368
      ]
    },
    {
      "id": "f7ca54c1-4dce-4cab-b356-a842b6a8367e",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -400,
        272
      ]
    },
    {
      "id": "5c8b2fc9-df5a-4683-8d36-b635c827afb3",
      "name": "Send a text message",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1296,
        176
      ]
    },
    {
      "id": "dbe5598d-58c5-45e0-824e-5aa9d53f02e9",
      "name": "Format All Messages",
      "type": "n8n-nodes-base.code",
      "position": [
        720,
        176
      ]
    },
    {
      "id": "d648d2ca-dc9e-47b8-8e61-91ae980d886b",
      "name": "Wait 60s (New Session)",
      "type": "n8n-nodes-base.wait",
      "position": [
        272,
        176
      ]
    },
    {
      "id": "954de179-2d69-4ac4-955a-c60544105050",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -384,
        -976
      ],
      "parameters": {
        "width": 1024,
        "height": 768,
        "content": "# Intelligent Message Debouncing for Telegram Support Bot\n\nThis workflow prevents your AI support bot from responding to every single message by intelligently aggregating rapid-fire messages from user"
      }
    },
    {
      "id": "7df45fe2-1701-4d70-9efb-c847c6e53d9f",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1344,
        576
      ],
      "parameters": {
        "width": 384,
        "height": 240,
        "content": "## Step 1: Receive Message\n\nTelegram Trigger listens for incoming messages from users.\n\nExtract user data including:\n- User ID\n- Message text\n- Timestamp"
      }
    },
    {
      "id": "77dbff3b-c29e-4663-831b-60e3f9e0747b",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        -48
      ],
      "parameters": {
        "width": 400,
        "height": 272,
        "content": "## Step 2: Check for Active Session\n\nQueries PostgreSQL to see if user has an active session within the last 60 seconds.\n\n- If NO session → Create new session\n- If session EXISTS → Append message to e"
      }
    },
    {
      "id": "58c8be2e-72f6-45af-a9a4-b232801386f8",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        -80
      ],
      "parameters": {
        "width": 416,
        "height": 208,
        "content": "## Step 3: Wait for More Messages\n\n⏱️ 60-second window to collect all user messages\n\nThis prevents the bot from responding immediately and allows users to fully explain their issue.\n\nAdjust timer here"
      }
    },
    {
      "id": "60a49ffa-2695-438f-baba-bb14d40277f2",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        -32
      ],
      "parameters": {
        "width": 368,
        "height": null,
        "content": "## Step 4: Process with AI\n\n1. Fetch all messages from the session\n2. Format into a single conversation string\n3. Send to OpenAI for comprehensive response\n\n💡 Tip: Customize the AI prompt in the Agent"
      }
    },
    {
      "id": "4de80290-6e55-4585-a884-5ec6be204e6b",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1312,
        400
      ],
      "parameters": {
        "width": 352,
        "height": 176,
        "content": "## Step 5: Deliver Response\n\nSend AI-generated response back to user via Telegram, then clear the session status to COMPLETED.\n\nReady for the next conversation! 🎉"
      }
    },
    {
      "id": "961b011b-8c26-4b21-aee7-95c35361b2c4",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2048,
        -192
      ],
      "parameters": {
        "width": 640,
        "height": 1440,
        "content": "⚠️ IMPORTANT: Database Setup Required\n\nBefore running this workflow, create the PostgreSQL table:\n```sql\n-- Debounced AI Support Agent - Database Schema\n-- PostgreSQL 13+ required for JSONB support\n\n-"
      }
    },
    {
      "id": "6ea7452f-ba0d-4108-9d1e-08497396c779",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1264,
        -336
      ],
      "parameters": {
        "width": 400,
        "height": null,
        "content": "🔐 Security Checklist\n\nBefore publishing:\n✅ Remove test credentials\n✅ Clear any personal Telegram chat IDs\n✅ Remove database connection strings\n✅ Use credential parameters, not hardcoded values"
      }
    }
  ],
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Create New Session",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Append Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append Message": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store Resume URL": {
      "main": [
        [
          {
            "node": "Wait 60s (New Session)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Extract User Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract User Data": {
      "main": [
        [
          {
            "node": "Check Active Session",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Create New Session": {
      "main": [
        [
          {
            "node": "Store Resume URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch All Messages": {
      "main": [
        [
          {
            "node": "Format All Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format All Messages": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message": {
      "main": [
        [
          {
            "node": "Clear Session",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Active Session": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 60s (New Session)": {
      "main": [
        [
          {
            "node": "Fetch All Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Incoming Message Webhook": {
      "main": [
        [
          {
            "node": "Extract User Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}