AI Chatbot with Conversational Memory using Telegram and Supabase - n8n Workflow

Deploy a persistent, context-aware AI chatbot using this advanced n8n workflow. Integrate Telegram and Supabase memory with the OpenAI Assistant API. Find robust n8n templates for context-aware bots.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Developers seeking robust n8n workflow examples for AI integration.
Businesses needing customer support chatbots with memory retention.
Users looking for advanced n8n templates utilizing Supabase for persistent data storage.
Automation specialists deploying production-ready AI services that require conversational context.

Overview

Many simple chatbots fail to maintain context across sessions. This robust n8n workflow solves that problem by implementing a durable memory layer. Every incoming Telegram message is managed through the initial n8n trigger. The subsequent n8n node sequence first checks Supabase to see if the user has an existing OpenAI thread ID. If they do, the conversation continues seamlessly, leveraging the history stored by the OpenAI Assistant. If they are new, this n8n workflow creates a new OpenAI thread and saves the necessary identifiers to the Supabase database. This architecture, built using powerful n8n templates, ensures that the OpenAI assistant maintains full conversational history, providing a superior, context-aware user experience directly within Telegram. This is a crucial n8n workflow for anyone building production-grade AI interfaces.

How it Works

The automation begins with the Get New Message Telegram n8n trigger, which activates upon receiving a new message.


  1. User Lookup: The Find User n8n node queries the Supabase table (telegramusers) using the Telegram Chat ID to check for existing records.

  2. Conditional Branching: The If User exists n8n node directs the flow based on the Supabase lookup result.

New User (False Path): The flow creates a new conversation. An HTTP Request n8n node (OPENAI - Create thread) generates a fresh OpenAI thread ID. Subsequently, the Create User Supabase n8n node stores the user's telegramid and the new openaithreadid.
Existing User (True Path): The workflow proceeds immediately, utilizing the stored openaithreadid.

  1. Data Consolidation: The Merge n8n node unifies the two possible paths, ensuring that the critical openaithreadid is available for the next steps.

  2. Send Message: The OPENAI - Send message HTTP Request n8n node posts the user's original message into the designated OpenAI thread.

  3. Run Assistant: The OPENAI - Run assistant HTTP Request n8n node initiates the processing of the message using the pre-configured OpenAI Assistant ID, leveraging the context of the entire thread.

  4. Retrieve Response: The OPENAI - Get messages HTTP Request n8n node fetches the thread history, isolating the latest response generated by the assistant.

  5. Delivery: Finally, the Send Message to User Telegram n8n node sends the structured AI reply back to the user's Telegram chat, successfully completing the execution of this complex n8n workflow.

Installation Guide

To deploy this powerful n8n workflow, follow these setup steps:


  1. Import: Copy the provided JSON and import it into your n8n instance.

  2. Credentials: Configure the necessary credentials:

Telegram API: Set up the credentials for the Telegram n8n trigger and action nodes using the token obtained from Botfather.
Supabase API: Configure credentials using your Supabase URL and API key.
* OpenAI API: Set up credentials for the HTTP Request n8n nodes, ensuring they have access to the Assistants API (v2).

  1. Supabase Database Setup: Create a table named telegramusers with the following columns to store the conversational memory:

create table
public.telegram
users (
id uuid not null default genrandomuuid (),
datecreated timestamp with time zone not null default (now() at time zone 'utc'::text),
telegram
id bigint null,
openaithreadid text null,
constraint telegramuserspkey primary key (id)
) tablespace pgdefault;

  1. OpenAI Assistant ID: In the OPENAI - Run assistant n8n node, replace the placeholder assistantid (asst_b0QhuzySG6jofHFdzPZD7WEz) with your actual OpenAI Assistant ID.

Node Details

Get New Message (Telegram Trigger): This n8n trigger is the starting point of the n8n workflow, configured to listen for new 'message' updates from the linked Telegram bot.
Find User (Supabase node): Executes a getAll operation on the telegramusers table, filtering by the incoming user's telegramid to check for prior conversations.
If User exists (If node): A core n8n node for flow control. It checks if the id field from the Supabase output exists, determining if the user is new or returning.
OPENAI - Create thread (HTTP Request n8n node): Used only for new users. It sends a POST request to the OpenAI API (/v1/threads) to initialize a new persistent conversational thread.
Create User (Supabase node): Saves the new telegramid and the newly created openaithreadid into the database, establishing the user's conversational memory key for future runs of this n8n workflow.
Merge (Merge node): Combines the data streams from the 'New User' path and the 'Existing User' path, ensuring consistency before engaging the OpenAI Assistant.
OPENAI - Send message (HTTP Request n8n node): Sends the user's latest text message into the associated OpenAI thread, preparing it for processing.
OPENAI - Run assistant (HTTP Request n8n node): Executes the specific OpenAI Assistant (assistant
id) on the thread, generating the AI response. Note the hardcoded assistant_id configuration within this n8n node.
OPENAI - Get messages (HTTP Request n8n node): Retrieves the final list of messages from the thread, extracting the assistant's response which is always the latest message in the list.
Send Message to User (Telegram node): The final n8n node, sending the resulting AI text back to the original Telegram chat ID.

Related n8n Workflows

Free

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

I am a business analyst with a development background, dedicated to helping small businesses and entrepreneurs leverage cloud services for increased efficiency. My expertise lies in automating manual workflows, integrating data from multiple cloud service providers, creating insightful dashboards, and building custom CRM systems.

Featured*