Telegram AI Chatbot with Message Buffering and Supabase Queue - n8n Workflow

Use this advanced n8n workflow template to build a reliable Telegram chatbot that queues rapid user messages via Supabase, ensuring the OpenAI agent receives a single, coherent prompt and provides one unified response.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Automation specialists designing resilient chatbots.
Developers needing to handle high-frequency messaging in conversational AI applications.
Users looking for advanced n8n templates integrating Supabase for state management.
Anyone seeking a custom n8n node solution for Telegram message buffering.

Overview

When building AI assistants using platforms like Telegram or WhatsApp, users often send several short messages instead of a single long paragraph. If the n8n workflow processes each message individually, it leads to rapid, often incoherent, multiple responses from the AI. This advanced n8n workflow solves this by implementing a robust buffering system using a Supabase database.

Every incoming message is initially stored in a queue. A subsequent wait period (configurable, default 10 seconds) allows time for more messages to arrive. Only when the queue stabilizes does the main processing begin. This ensures the AI agent receives a complete context, leading to a much more accurate and human-like response delivered via a single Telegram reply. This powerful n8n template showcases complex conditional logic and database interaction within the n8n automation environment.

How it Works

This powerful n8n workflow follows a five-step logic:


  1. Receive and Queue: The process starts with the Telegram n8n trigger, which fires immediately upon receiving a user message. This message is quickly stored in a Supabase table (message_queue) using the Supabase n8n node, along with the user's ID and message ID.

  2. Buffer Window: The n8n workflow then pauses execution using the Wait n8n node for 10 seconds, creating a buffering window. If the user sends subsequent messages during this time, new executions start and also queue their messages.

  3. Retrieve and Check: After the wait period, the workflow retrieves all queued messages for that user from Supabase. The crucial Check Most Recent Message If n8n node verifies if the message that initiated this specific thread is the last one in the queue. If it's not, a newer message has arrived, and the current execution branch terminates using the No Operation, do nothing n8n node to prevent duplicate processing.

  4. Process and Aggregate: If the current thread holds the most recent message, the workflow first uses a Supabase n8n node to delete all aggregated messages from the queue. It then uses the Aggregate n8n node to combine all the queued messages into one large string, which forms the core prompt.

  5. AI Response and Reply: The combined prompt is passed to the AI Agent n8n node, which utilizes the OpenAI Chat Model (GPT-4o-mini) and Postgres Chat Memory (for conversational context via Supabase's PostgreSQL). The final generated reply is then sent back to the user using the Telegram Reply n8n node, completing this automated n8n workflow.

Installation Guide

To import and run this n8n template, follow these steps:


  1. Import: Copy the provided JSON data and import it into your n8n instance via the 'Workflows' menu using the 'Import from JSON' option.

  2. Supabase Setup: Ensure you have a Supabase instance connected. Create a table named messagequeue with the following columns: userid (e.g., uint8), message (text), and message_id (uint8).

  3. Credential Setup:

Set up credentials for the Telegram API (required for the n8n trigger and Reply n8n node).
Set up credentials for Supabase API (required for the Supabase n8n node operations).
Set up credentials for the OpenAI API (required for the OpenAI Chat Model n8n node).
Set up PostgreSQL credentials, specifically pointing to your Supabase instance, for the Postgres Chat Memory n8n node.

  1. Configuration: If desired, modify the Wait 10 Seconds n8n node to adjust the buffering window. You can also customize the AI Agent prompt via the LangChain Agent n8n node.

  2. Activation: Once credentials are set, activate the n8n workflow to start monitoring your Telegram bot for messages.

Node Details

This intricate n8n workflow leverages several specialized n8n nodes:

Receive Message (Telegram Trigger n8n node):
Function: Serves as the primary n8n trigger, initiating the workflow upon receiving any new message in the linked Telegram bot.
Key Configuration: Configured to watch for message updates.
Add to Queued Messages (Supabase n8n node):
Function: Inserts the incoming message data (user ID, message text, and message ID) into the message_queue table for temporary storage.
Wait 10 Seconds (Wait n8n node):
Function: Introduces a crucial 10-second delay, defining the message buffering window to aggregate rapid sequential inputs.
Check Most Recent Message (If n8n node):
Function: Compares the message ID of the initiating event with the latest message ID fetched from the queue. It ensures only the execution thread associated with the most recent buffered message continues processing.
Delete Queued Messages (Supabase n8n node):
Function: Executed only after the conditional check passes, clearing all buffered messages for the user ID to ensure a clean slate before aggregation.
Aggregate (Aggregate n8n node):
Function: Takes the multiple items (messages) retrieved from Supabase and joins their content (message field) into a single unified input string for the AI.
AI Agent (LangChain Agent n8n node):
Function: The core processing unit. It accepts the aggregated prompt, handles the conversational logic, and generates the final intelligent response.
OpenAI Chat Model (LangChain LLM n8n node):
Function: Provides the specific large language model (GPT-4o-mini) utilized by the AI Agent for generation.
Postgres Chat Memory (LangChain Memory n8n node):
Function: Stores the conversation history linked to the session key (Telegram Chat ID) in a PostgreSQL database (Supabase), allowing the AI to maintain context across interactions.
Reply (Telegram n8n node):
* Function: Sends the final, aggregated response generated by the AI back to the original Telegram chat ID.

Related n8n Workflows

Free

Nodes: 12 Nodes
Updated: December 26 2025
View all
Created by

Founder of bsde.ai, a workflow and AI agency based in the UK.

Featured*