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

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

Learn how to Build this Workflow with AI:

Visit through Desktop for Best experience

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.


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 Viral UGC Video Creation Using n8n + Degaus (Beginner-Friendly Guide)

Learn how to automate viral UGC video creation using n8n, AI prompts, and Degaus. This beginner-friendly guide shows how to import, configure, and run the workflow without technical complexity.
Form Trigger
Google Sheets
Gmail
+37
Free

AI SEO Blog Writer Automation Workflows in n8n

A complete beginner guide to building an AI SEO blog writer automation using n8n.
AI Agent
Google Sheets
httpRequest
+5
Free

Automate CrowdStrike Alerts with VirusTotal, Jira & Slack

This workflow automates processing of CrowdStrike detections by enriching threat data via VirusTotal, creating Jira tickets for incident tracking, and notifying teams on Slack for quick response. Save hours daily by transforming complex threat data into actionable alerts effortlessly.
scheduleTrigger
httpRequest
jira
+5
Free

Automate Telegram Invoices to Notion with AI Summaries & Reports

Save hours on financial tracking by automating invoice extraction from Telegram photos to Notion using Google Gemini AI. This workflow extracts data, records transactions, and generates detailed spending reports with charts sent on schedule via Telegram.
lmChatGoogleGemini
telegramTrigger
notion
+9
Free

Automate Email Replies with n8n and AI-Powered Summarization

Save hours managing your inbox with this n8n workflow that uses IMAP email triggers, AI summarization, and vector search to draft concise replies requiring minimal review. Automate business email processing efficiently with AI guidance and Gmail integration.
emailReadImap
vectorStoreQdrant
emailSend
+12
Free

Automate Email Campaigns Using n8n with Gmail & Google Sheets

This n8n workflow automates personalized email outreach campaigns by integrating Gmail and Google Sheets, saving hours of manual follow-up work and reducing errors in email sequences. It ensures timely follow-ups based on previous email interactions, optimizing communication efficiency.
googleSheets
gmail
code
+5
Free