Build an AI Telegram Bot with OpenAI & Supabase Memory

Automate Telegram conversations with this workflow using OpenAI for responses and Supabase for user memory, solving the problem of chatbots lacking context. Maintain continuity across chats and deliver more natural, engaging interactions.
telegramTrigger
httpRequest
supabase
+4
Workflow Identifier: 1921
NODES in Use: Telegram Trigger, HTTP Request, Supabase, Merge, If, Telegram, Sticky Note
Automate AI Telegram Bot with n8n and OpenAI

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What this workflow does

This workflow creates a Telegram chatbot that remembers past chat history using OpenAI and saves user data in Supabase.

The chatbot keeps conversation context by linking Telegram users to OpenAI chat threads stored in a database.

When users send messages, the bot replies with answers that consider previous chats, avoiding repetitive or generic responses.

This saves time and makes conversations better and more natural.


Who should use this workflow

People who manage many Telegram groups where users ask lots of questions and want quick, smart help.

Users looking to add memory and context to Telegram chatbots without coding everything themselves.

Anyone who wants a simple system to keep conversations linked using Supabase and OpenAI inside n8n.


Tools and services used

  • Telegram Bot API: Receives user messages.
  • OpenAI Assistant API: Creates and runs chat threads with memory.
  • Supabase Database: Stores Telegram user data and OpenAI thread IDs.
  • n8n Automation Platform: Links the Telegram, OpenAI, and Supabase nodes.

How the workflow works: Inputs → Process → Outputs

Inputs

  • New Telegram messages from users or groups via the Telegram Trigger node.
  • Telegram user chat ID to identify returning or new users.

Processing steps

  • Check Supabase database to find if the Telegram user exists and get their OpenAI thread ID.
  • If user not found, create a new OpenAI chat thread via HTTP Request node and save user info with thread ID into Supabase.
  • Merge data so following steps always have user and thread info.
  • Send the user’s message into the correct OpenAI thread to keep the chat context.
  • Run the OpenAI assistant on the updated thread to create a response based on conversation history.
  • Fetch the assistant’s latest message.

Output


Beginner step-by-step: How to use this workflow in n8n

Step 1: Import workflow

  1. Download the workflow file using the Download button on this page.
  2. Open n8n editor and choose “Import from File” to add the workflow.

Step 2: Configure credentials

  1. In n8n, go to Credentials and add your Telegram Bot API Key, Supabase project URL and API Key, and OpenAI API Key.
  2. Make sure to enter the correct OpenAI assistant ID inside the HTTP Request node named “OPENAI – Run assistant”.

Step 3: Adjust IDs and tables (if needed)

  1. Verify the Supabase table name matches “telegram_users” or update node configurations accordingly.
  2. Check if the Telegram chat ID and OpenAI thread ID use the correct data paths for your setup.

Step 4: Test and activate

  1. Send a test message to your Telegram bot and see if the workflow runs and replies correctly.
  2. If tests are successful, activate the workflow to start running in production.

For those using self-host n8n, see this resource to host the workflow yourself.


Customization ideas

  • Change the OpenAI assistant ID to use a different assistant personality.
  • Add user fields like first name or username to stored Supabase data to personalize responses.
  • Include the user’s first name in bot replies by referencing {{$json.message.chat.first_name}}.
  • Modify Telegram Trigger node to handle messages inside groups by allowing different update types.

Common edge cases and how to fix them

Telegram messages do not start the workflow

Check if the Telegram webhook URL from the Telegram Trigger node is set in Botfather or Telegram correctly.

Make sure the webhook URL is public and reachable.

OpenAI API returns errors or unauthorized

Confirm the OpenAI API key is correctly entered in n8n and the required header OpenAI-Beta: assistants=v2 is included in requests.

Supabase node returns no user found

Check the Supabase filter is comparing telegram_id to the correct incoming chat ID value.

Ensure API key has read and write permissions on telegram_users table.


Summary

→ Create a Telegram bot that keeps memory of conversations using OpenAI and Supabase.

→ Save user and thread info to keep context across chats.

→ Answer user messages with AI that knows past questions.

✓ Easy to use with n8n importing and configuring credentials.

✓ Improves chat quality and saves time for community managers.

Automate AI Telegram Bot with n8n and OpenAI

Visit through Desktop to Interact with the Workflow.

Frequently Asked Questions

Yes, but users must replace Supabase nodes with appropriate database nodes and update queries.
Yes, because each thread run and message consumes tokens for context-aware conversations.
Supabase stores user IDs and thread IDs securely, but users should keep API keys and database credentials private.
It checks the Supabase database for a matching telegram_id and creates a new OpenAI thread if no user is found.

Promoted by BULDRR AI

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.