Build an AI Telegram Bot with LangChain & n8n

Learn how to create a Telegram AI bot using n8n, LangChain nodes, and OpenAI to chat, remember context, and generate images with Dall-E 3. Automate personalized user interaction with smart memory and multi-tool AI responses.
lmChatOpenAi
memoryBufferWindow
telegramTrigger
+5
Workflow Identifier: 1649
NODES in Use: lmChatOpenAi, memoryBufferWindow, telegramTrigger, stickyNote, telegram, telegramTool, toolHttpRequest, agent
Automate Telegram AI bot with n8n and LangChain

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

Learn how to Build this Workflow with AI:

What this workflow does

This workflow listens to messages on Telegram and answers them using AI inside n8n.

It helps users by giving smart replies that remember what was said before.

Users can also ask for pictures, and the bot will make and send AI images back automatically on Telegram.

The bot talks using the OpenAI GPT-4o model and uses LangChain to keep memory and choose between chatting or making images.

If something goes wrong sending a Telegram message, the workflow keeps running without stopping.


Who should use this workflow

Content creators or small business owners with many Telegram followers can use this.

The workflow saves time by automating answers and images to Telegram messages.

It is for people who want to improve user chats without answering every question manually.


Tools and services used

  • Telegram Bot API: Receives user messages.
  • OpenAI GPT-4o Chat Model: Generates smart text replies.
  • Dall-E 3 API: Produces AI-generated images.
  • LangChain AI Agent Node: Controls if the bot chats or makes images.
  • LangChain Window Buffer Memory: Stores recent chat context.
  • n8n Workflow Engine: Runs nodes and connects the steps.

Inputs, Processing Steps, and Outputs

Inputs

  • Telegram messages from any user or chat.
  • User names and chat IDs from Telegram messages.
  • Commands or text that ask questions or request images.

Processing Steps

  • Telegram Trigger Node: Listens for all new Telegram messages.
  • AI Agent Node: Receives message text and user first name; decides if reply or image generation needed.
  • OpenAI Chat Model Node: Generates text answers based on GPT-4o with adjustable creativity and memory penalties.
  • Window Buffer Memory Node: Saves recent messages (up to 10) per Telegram chat for context in replies.
  • Dall-E Image Generation Node: Called by AI Agent to create images when requested by the user.
  • Telegram Tool Node: Sends generated images back as documents via Telegram.
  • Telegram Final Reply Node: Sends text answers to users, continues running even on sending errors.

Outputs

  • Personalized text replies sent back to Telegram users, including their first name.
  • AI-generated images sent automatically in Telegram chats when asked.
  • Continuous workflow operation despite errors in sending messages.

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

Import and setup

  1. Download the workflow file using the Download button on this page.
  2. Open the n8n editor and choose “Import from File”, then select the downloaded file.
  3. After import, add all required credentials: Telegram Bot API Token and OpenAI API Key with GPT-4o and Dall-E access.
  4. If needed, update the chat ID or other IDs in the Telegram nodes.
  5. Check the AI Agent node’s system message to keep or customize the greeting:
    You are a helpful assistant. You are communicating with a user named {{ $json.message.from.first_name }}.
    
  6. Test the workflow by sending messages in the linked Telegram chat and see the replies.
  7. Activate the workflow by enabling the “Active” switch in n8n for live running.
  8. Make sure your n8n instance has HTTPS if you want Telegram webhooks to work properly—for example, with self-host n8n.

Handling errors and edge cases

  • If Telegram messages fail to send, enable “Continue On Error” in the final Telegram node to keep the workflow running.
  • Check the chat ID used in Telegram nodes matches the user or chat messaging the bot.
  • Update OpenAI API keys if any “authentication failed” errors appear.
  • Ensure the Window Buffer Memory session keys match the Telegram chat ID format exactly to save conversation context properly.

Customization ideas

  • Change the AI Agent node’s greeting in the system message to add new user data or languages.
  • Adjust the Window Buffer Memory node to keep more or fewer past messages to change chat memory length.
  • Tweak temperature and frequency penalty in the OpenAI Chat Model node to make responses more creative or strict.
  • Add more parameters to the Dall-E image generation prompt for different visual styles or content control.
  • Use multi-language instructions within the AI Agent to answer in languages based on user messages.

Summary of benefits and outcomes

✓ Saves time by automating Telegram message replies and image creation.

✓ Sends personalized answers that remember past conversation context.

✓ Automatically creates and sends AI images on demand.

✓ Keeps running even when message sending errors happen.

→ Gives users faster, smarter replies with less manual work.

→ Helps manage social media chats with advanced AI tools inside n8n.


Automate Telegram AI bot with n8n and LangChain

Visit through Desktop to Interact with the Workflow.

Frequently Asked Questions

The workflow uses LangChain Window Buffer Memory node to store recent messages per chat. This memory keeps context up to 10 messages to make replies relevant.
Enable “Continue On Error” in the final Telegram node to keep the workflow running. Also, check that the chat ID used matches the user sending messages and that the bot is not blocked.
The AI Agent node decides if the reply should be a text chat or AI-generated image. It sends the user message to appropriate tools based on the request.
Download and import the workflow file into n8n, add the required Telegram and OpenAI API credentials, test by sending messages, and then switch on the “Active” toggle in the editor.

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.