n8n

How to Connect Telegram Chat to AI Support Replies?

Give your Telegram bot the power to answer like a person, even when users send many short messages in a row. The flow groups rapid messages from the same user, builds one clear prompt, and returns a single helpful reply. Great for customer support and FAQ help where speed and clarity matter.

Here is how it runs. A Telegram trigger listens for new messages. Each message is saved to a Supabase table with the user ID and message ID. A short wait window collects more messages from the same user. After the wait, the flow pulls that user’s queue, sorts by message ID to keep the right order, checks if newer messages arrived, and if not, merges the text into one prompt. An OpenAI chat model generates the answer and a Postgres Chat Memory keeps context across chats. The reply is sent back to Telegram and the queue is cleared.

Setup is simple. Create the message_queue table in Supabase and connect your Telegram bot, Supabase keys, OpenAI key, and Postgres connection in n8n. Expect fewer split replies, clearer answers, and faster support. Teams that get message bursts or voice-to-text streams will see a direct increase in agent efficiency and user satisfaction.

What are the key features?

  • Telegram trigger captures every incoming user message in real time
  • Supabase table stores user_id, message, and message_id for safe queuing
  • Wait node buffers for 10 seconds to collect rapid message bursts
  • Query and sort steps rebuild the exact message order for each user
  • If check prevents replies if fresh messages arrive during the wait window
  • Aggregate merges all queued texts into a single clean prompt
  • OpenAI Chat Model creates the final answer using gpt 4o mini
  • Postgres Chat Memory keeps conversation context across sessions
  • Cleanup removes processed rows so the queue stays lean
  • Telegram reply sends one unified response back to the user

What are the benefits?

  • Reduce split replies by up to 80 percent with message buffering
  • Cut manual follow ups by agents and save 10 to 20 minutes per chat
  • Handle up to 3 times more chat volume on one bot without extra staff
  • Improve answer accuracy by merging message bursts into one prompt
  • Connect Telegram, Supabase, OpenAI and PostgreSQL in one stable flow

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with Telegram, Supabase, OpenAI and PostgreSQL. See the Tools Required section above for links to create accounts with these services.
  3. Create a Telegram bot with BotFather and copy the bot token. In n8n, double click the Telegram Trigger and Reply nodes, choose 'Credential to connect with', click 'Create new credential', and paste the token to connect your bot.
  4. In Supabase, create a project and a table named message_queue with columns: user_id bigint, message text, message_id bigint. Keep column names exactly as shown.
  5. In n8n, open the Supabase nodes Add to Queued Messages, Get Queued Messages, and Delete Queued Messages. For each, create a new Supabase credential and follow the on screen steps to enter your URL and API key.
  6. For chat memory, create a PostgreSQL credential in n8n using your Supabase connection details. If available, use the session pooler host and port for better performance.
  7. Open the OpenAI Chat Model node, create a new OpenAI credential, and paste your API key from the OpenAI account API page. Keep the model set to gpt 4o mini or choose another model if needed.
  8. Check the Get Queued Messages filter. It should match user_id to the Telegram from.id from the incoming message. Confirm the Delete Queued Messages filter uses the same user_id.
  9. Adjust the Wait node if you want a longer or shorter buffer window. Ten seconds works well for most chat bursts.
  10. Activate the workflow, send several quick messages to your Telegram bot, wait for the buffer to end, and confirm you receive one unified reply.
  11. Troubleshoot tips: if no reply arrives, check that new rows are created in the message_queue, confirm they are deleted after a reply, and verify the bot token and chatId mapping in the Reply node.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

OpenAI

Sign up

Pay-as-you-go: GPT-5 at $1.25 per 1M input tokens and $10 per 1M output tokens

PostgreSQL

Sign up

Free: $0 (open-source PostgreSQL License; self-hosted)

Supabase

Sign up

Free: $0 / mo — unlimited API requests; 500 MB database; 5 GB bandwidth; 1 GB storage; 50,000 MAUs.

Telegram

Sign up

Free: $0, Telegram Bot API usage is free for developers

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.