AI Telegram Chatbot Agent with Supabase Memory & OpenAI
Integrates with:
Overview
Unlock Persistent, Contextual Conversations with this AI Telegram Bot Agent
This n8n workflow transforms your Telegram bot into an intelligent AI Agent, addressing the common chatbot limitation of lacking context awareness and user memory. It listens for new Telegram messages, identifies users (creating new ones in Supabase if they're first-timers), and then leverages an OpenAI Assistant (using the v2 API) to provide sophisticated, context-aware responses.
The core AI ability of this agent is contextual conversational AI. By integrating with Supabase, it stores OpenAI thread IDs linked to Telegram user IDs. This enables the bot to maintain conversation history across interactions, leading to a more human-like, engaging, and effective user experience. Stop frustrating your users with amnesiac bots; empower them with an assistant that remembers.
Key Features & Benefits
- Advanced Conversational AI: Utilizes OpenAI Assistants API (v2) for intelligent, natural language understanding and response generation.
- Persistent User Memory: Leverages Supabase to store and retrieve
openai_thread_id
for eachtelegram_id
, enabling continuous, context-aware conversations. - Automated User Session Management: Automatically creates new user records and dedicated OpenAI conversation threads for new users, ensuring personalized interactions from the start.
- Seamless Telegram Integration: Directly receives messages from and sends replies to users on Telegram, meeting them where they are.
- Scalable Automation: Perfect for solopreneurs and businesses looking to automate customer support, lead qualification, FAQ handling, or provide personalized assistance via Telegram without manual oversight.
- Customizable AI Personality: Connects to your own OpenAI Assistant, allowing you to define its knowledge base, instructions, and persona to match your brand or specific use case.
- Efficient Backend Operations: Checks for existing users to reuse conversation threads, optimizing API calls and maintaining context efficiently.
Use Cases
- B2C E-commerce: Provide 24/7 AI-driven customer support on Telegram, answer product queries with memory of past questions, and guide users through purchase processes.
- B2B SaaS: Offer instant, contextual help to users via a Telegram bot, troubleshoot common issues, and onboard new clients by remembering their setup progress.
- Solopreneurs: Automate initial client interactions, answer frequently asked questions, and qualify leads through a smart Telegram assistant that doesn't lose context.
- Community Management: Deploy an AI assistant in Telegram groups to answer common questions, share resources, and moderate discussions based on past interactions with users.
Prerequisites
- An n8n instance (Cloud or self-hosted).
- OpenAI API Key with access to the Assistants API (v2 recommended).
- Telegram Bot Token (obtained from BotFather).
- Supabase project URL and API Key (typically the
anon
key). - A Supabase table named
telegram_users
(SQL schema provided in setup instructions). - An OpenAI Assistant ID (created in the OpenAI platform).
Setup Instructions
- Download the n8n workflow JSON file.
- Import the workflow into your n8n instance.
- Telegram Bot Setup:
- Create a Telegram Bot using BotFather and obtain its API token.
- In n8n, create a 'Telegram Bot API' credential using this token.
- Assign this credential to the 'Get New Message' (Telegram Trigger) and 'Send Message to User' (Telegram Send) nodes.
- Supabase Setup:
- Create a new project in Supabase.
- Navigate to 'SQL Editor' and run the following query to create the
telegram_users
table: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'::text), telegram_id bigint null, openai_thread_id text null, constraint telegram_users_pkey primary key (id) );
- In n8n, create a 'Supabase API' credential using your Supabase Project URL and
anon
(public) key. - Assign this credential to the 'Find User' and 'Create User' (Supabase) nodes.
- OpenAI Assistant Setup:
- Ensure you have an OpenAI account and an API Key.
- Go to the OpenAI Assistants Playground and create a new Assistant. Define its instructions, choose a model (e.g., GPT-4o, GPT-3.5-turbo), and enable any necessary tools. Note the Assistant ID (e.g.,
asst_...
). - In n8n, create an 'OpenAI API' credential using your OpenAI API Key.
- Assign this credential to all HTTP Request nodes named 'OPENAI - ...' (Create thread, Send message, Run assistant, Get messages).
- In the 'OPENAI - Run assistant' node, update the
assistant_id
field in the 'Body Parameters' with your Assistant ID (the example usesasst_b0QhuzySG6jofHFdzPZD7WEz
- replace it with your own). - Ensure all 'OPENAI - ...' HTTP Request nodes have the
OpenAI-Beta
header set toassistants=v2
in 'Header Parameters'.
- Review node connections and ensure data flows correctly (e.g.,
telegram_id
from trigger to Supabase nodes,openai_thread_id
between OpenAI and Supabase nodes). - Activate the workflow. Test by sending a message to your Telegram bot.
Want your own unique AI agent?
Talk to us - we know how to build custom AI agents for your specific needs.
Schedule a Consultation