Who is this workflow for? This workflow guides you through creating a Telegram bot that leverages Supabase for memory management and OpenAI for intelligent responses. By integrating these tools using n8n, you can develop a bot that maintains context and provides personalized interactions..

What does this workflow do?

  • Receive New Message: The Telegram bot listens for incoming messages from users.
  • Check User in Database: The workflow queries the Supabase database to determine if the user exists using their telegram_id.
  • Create New User (if necessary): If the user is not found, a new record is created in Supabase with a unique openai_thread_id.
  • Start or Continue Conversation with OpenAI: Depending on the user’s context, the bot either initiates a new thread or continues an existing one using the openai_thread_id.
  • Merge Data: User-specific data and conversation context are combined to maintain continuity.
  • Send and Receive Messages: The user’s message is sent to OpenAI, and the response is received and processed.
  • Reply to User: The bot sends OpenAI’s response back to the user on Telegram.

Setup Steps

  • Create a Telegram Bot:
  • Use the Botfather to create a new Telegram bot and obtain the bot token.
  • Set Up Supabase:
  • Create a new Supabase project to generate a SUPABASE_URL and SUPABASE_KEY.

  • Create a telegram_users table with the following SQL:

     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, openai_thread_id TEXT, CONSTRAINT telegram_users_pkey PRIMARY KEY (id) ) TABLESPACE pg_default;
  • OpenAI Setup:
  • Create an OpenAI assistant and obtain the OPENAI_API_KEY.
  • Customize the assistant’s personality or use cases as needed.
  • Configure Environment in n8n:
  • Set up Telegram, Supabase, and OpenAI nodes with the respective credentials.
  • Configure triggers for receiving messages and managing conversation flow.
  • Specify the OpenAI assistant ID in the “Run Assistant” node.

🤖 Why Use This Automation Workflow?

  • Enhanced User Experience: Maintain conversation context for more meaningful interactions.
  • Scalable Data Management: Use Supabase to efficiently store and retrieve user data.
  • Intelligent Responses: Harness OpenAI’s capabilities to provide accurate and relevant answers.
  • Customizable Integration: Easily adapt the workflow to fit specific business or personal needs.
  • Automated Processes: Streamline bot operations with automated triggers and actions in n8n.

👨‍💻 Who is This Workflow For?

This workflow is ideal for:

  • Developers looking to build sophisticated chatbots.
  • Chatbot Enthusiasts aiming to enhance their bot’s capabilities.
  • Businesses seeking to implement customer support or virtual assistants with personalized interactions.

🎯 Use Cases

  1. Customer Support: Provide consistent and informed responses to customer inquiries.
  2. Virtual Assistants: Create a personal assistant that remembers user preferences and past interactions.
  3. Interactive Marketing: Engage users with personalized conversations to enhance marketing efforts.

TL;DR

This workflow enables you to build a Telegram bot that offers intelligent, context-aware interactions by integrating Supabase for memory management and OpenAI for dynamic responses. Ideal for developers and businesses, this template facilitates the creation of personalized and engaging user experiences.

Help us find the best n8n templates

About

A curated directory of the best n8n templates for workflow automations.