Telegram Bot with Supabase Memory and OpenAI Assistant Integration
### Video Guide I prepared a detailed guide that showed the whole process of building an AI bot, from the simplest version to the most complex in a template. [.png)](https://www.youtube.com/watch?v=QrZxuWgFqBI) ### Who is this for? This workflow is ideal for developers, chatbot enthusiasts, and businesses looking to build a dynamic Telegram bot with memory capabilities. The bot leverages OpenAI's assistant to interact with users and stores user data in Supabase for personalized conversations. ### What problem does this workflow solve? Many simple chatbots lack context awareness and user memory. This workflow solves that by integrating Supabase to keep track of user sessions (via `telegram_id` and `openai_thread_id`), allowing the bot to maintain continuity and context in conversations, leading to a more human-like and engaging experience. ### What this workflow does This Telegram bot template connects with OpenAI to answer user queries while storing and retrieving user information from a Supabase database. The memory component ensures that the bot can reference past interactions, making it suitable for use cases such as customer support, virtual assistants, or any application where context retention is crucial. 1. **Receive New Message:** The bot listens for incoming messages from users in Telegram. 2. **Check User in Database:** The workflow checks if the user is already in the Supabase database using the `telegram_id`. 3. **Create New User (if necessary):** If the user does not exist, a new record is created in Supabase with the telegram_id and a unique `openai_thread_id`. 4. **Start or Continue Conversation with OpenAI:** Based on the user's context, the bot either creates a new thread or continues an existing one using the stored `openai_thread_id`. 5. **Merge Data:** User-specific data and conversation context are merged. 6. **Send and Receive Messages:** The message is sent to OpenAI, and the response is received and processed. 7. **Reply to User:** The bot sends OpenAI's response back to the user in Telegram. ### Setup 1. **Create a Telegram Bot** using the [Botfather](https://t.me/botfather) and obtain the bot token. 2. **Set up Supabase:** 1. Create a new project and generate a `SUPABASE_URL` and `SUPABASE_KEY`. 2. Create a new table named `telegram_users` with the following SQL query: ``` create table public.telegram_users ( id uuid not null default gen_random_uuid(), date_created timestamp with time zone not null default (now() at time zone 'utc'), telegram_id bigint null, openai_thread_id text null, constraint telegram_users_pkey primary key (id) ) tablespace pg_default; ``` 3. **OpenAI Setup:** 1. Create an OpenAI assistant and obtain the `OPENAI_API_KEY`. 2. Customize your assistant's personality or use cases according to your requirements. 4. **Environment Configuration in n8n:** 1. Configure the Telegram, Supabase, and OpenAI nodes with the appropriate credentials. 2. Set up triggers for receiving messages and handling conversation logic. 3. Set up OpenAI assistant ID in **OPENAI - Run assistant** node.
New buyers can create an account from the cart to unlock a controlled $10 first-purchase credit on eligible orders of $25+.
Related bundle
Content Repurposing Engine
8 hand-picked workflows for $29.00.
That is $3.63 each, vs $14.99 for this one alone.
View bundleSecure checkout powered by Stripe
Support
How to import this workflow into n8n
- 1Purchase or download the workflow to get the n8n workflow JSON file.
- 2In your n8n instance, open Workflows and choose "Import from File" (or paste the JSON with Ctrl+V on the canvas).
- 3Open each node marked with a credential warning and connect your own accounts and API keys.
- 4Run the workflow once manually to verify the data flow, then toggle it to Active.
Related AI workflows
- Launch Your First AI-Powered Chatbot with Actionable Tools$9.99
- Automate AI Video Creation and YouTube Upload with Google Sheets$14.99
- Build a WhatsApp Assistant with Memory, Google Suite, Multi-AI, Research, and Imaging$24.99
- Automate Blog Post Creation and Publishing with GPT, Leonardo AI, and WordPress$14.99
- Automate SEO Keyword Generation with ChatGPT from Google Sheets$3.99
- Email Agent$500.99
More from Mark Shcherbakov
Need this deployed? We'll set it up for you.
Our automation experts deploy this workflow in your stack, connect your accounts, and verify it works — or build a custom solution from scratch.