Build a Telegram AI Bot with LangChain & OpenAI GPT-4 in n8n [2026]

Create a smart Telegram AI bot using LangChain, OpenAI GPT-4, and n8n. Includes conversation memory, tool use, and full workflow JSON.
telegramTrigger
agent
lmChatOpenAi
+5
Workflow Identifier: 1338
NODES in Use: telegramTrigger, agent, lmChatOpenAi, memoryBufferWindow, toolWorkflow, httpRequest, telegram, set
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 Automation Does

This workflow listens to Telegram messages and uses AI to answer questions or make pictures.

It saves time by replying fast with smart text or images based on user messages.

The Telegram Bot API sends the user message to n8n.

Then OpenAI GPT-4 via Langchain decides the best answer or if an image is needed.

If an image is asked, Dall-E 3 creates one using the prompt.

The bot sends text replies or photos back to Telegram chats.


Who Should Use This Workflow

This workflow fits people who manage Telegram groups and get many similar questions.

It helps non-technical users save hours by automating chat replies and improves chat activity.

Users without coding skills can use this with some setup in n8n.


Tools and Services Used


Beginner Step-by-Step: How to Use This Workflow in n8n

1. Import Workflow

  1. Download the workflow file using the Download button on this page.
  2. Open n8n editor and click “Import from File” to upload the workflow.

2. Configure Credentials

  1. Open each node that needs credentials.
  2. Provide your Telegram Bot API token and OpenAI API Key.

3. Update IDs if Needed

  1. Check if any chat IDs, channel names, or folder IDs in nodes require your own values.
  2. Replace placeholder values with your actual information.

4. Review Prompts and Expressions

  1. Look inside the AI Agent and OpenAI Chat Model nodes.
  2. Copy the prompt texts or expressions to adjust wording if needed.

5. Test the Workflow

  1. Send test messages to your Telegram bot account.
  2. Check that the workflow triggers and replies with text or images.

6. Activate Workflow

  1. Turn on the workflow toggle in n8n to run it live.
  2. Make sure your n8n instance is running stable and connected to the internet.

If hosting n8n yourself, consider a self-host n8n setup.


Inputs, Processing Steps, and Outputs

Inputs

  • New text messages or commands from Telegram group chats.

Processing Steps

  • Telegram Trigger catches incoming messages.
  • AI Agent reads message and decides if reply or image needed.
  • OpenAI Chat Model (GPT-4) generates text answer when needed.
  • Window Buffer Memory keeps last 10 messages to remember conversation.
  • Dall-E 3 Tool detects image requests and triggers image creation.
  • Generate Image in Dall-E 3 HTTP Request calls OpenAI image API.
  • Telegram node sends text or photo responses back.
  • Correct errors node cleans special characters for Telegram display.

Outputs

  • AI-generated text replies to user questions or chats.
  • AI-created images based on user prompts delivered inside Telegram.

Common Edge Cases and Failures

  • If the Telegram bot does not receive messages, verify webhook URL is set and bot privacy settings allow message access.
  • If AI replies are empty or off-topic, check that session keys in Window Buffer Memory use dynamic chat IDs.
  • Invalid OpenAI API keys cause message generation failures—renew or correct keys immediately.
  • Image generation may fail if Dall-E API quota is reached or prompt is empty.

Customization Ideas

  • Change text model parameters inside OpenAI Chat Model node to adjust creativity or response style.
  • Modify Window Buffer Memory‘s number of stored messages to keep longer or shorter conversation history.
  • Add tools such as Wikipedia search or weather lookup nodes to widen user support.
  • Change Telegram message parsing mode from HTML to Markdown for different text formatting.
  • Translate system and human prompts inside AI Agent node to support multiple languages.

Sample Prompt in AI Agent Node

The following prompt directs AI response and when to create images.


System: You are a helpful assistant. Reply to users or generate images if asked.
Human: {{$json.message.text}}

Adjust this prompt inside the AI Agent node under parameters for message processing.


Sample Expression for Window Buffer Memory session key

Use this to separate conversations per chat in memory node:

=chat_with_{{ $('Listen for incoming events').first().json.message.chat.id }}

This prevents mixing histories from different Telegram chats.


Special Character Escaping in Telegram Messages

Use this expression to fix AI reply message so Telegram shows it right in HTML mode:

{{ $('AI Agent').item.json.output.replace(/&/g, "&").replace(/>/g, ">").replace(/

Summary of Results

✓ Saves 3-4 hours weekly by automating responses and image sharing.

✓ Uses AI to understand chat and provide helpful answers or pictures.

✓ Keeps chat context per user for natural conversations.

✓ Easily imported and customized inside n8n with simple steps.


Automate Telegram AI bot with n8n and LangChain

Visit through Desktop to Interact with the Workflow.

Frequently Asked Questions

Download the workflow file. Open n8n editor and use Import from File. Add Telegram and OpenAI API keys. Update any IDs as needed. Test with Telegram messages. Activate the workflow.
If webhook URL is not registered properly or Telegram bot privacy settings block messages, the trigger will not fire.
Set dynamic session keys in Window Buffer Memory node using chat IDs from incoming Telegram messages to isolate conversation history.
Yes. Change the model parameter in OpenAI Chat Model node to use GPT-3.5 or other supported models.

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.