Build a Telegram AI Bot with Langchain & OpenAI GPT-4

Automate intelligent conversation and image generation in Telegram using OpenAI GPT-4 and Langchain. This workflow streamlines chat and visual responses with advanced AI tools, saving hours of manual customer support and creative tasks.
telegramTrigger
agent
lmChatOpenAi
+5
Workflow Identifier: 1338
NODES in Use: telegramTrigger, agent, lmChatOpenAi, memoryBufferWindow, toolWorkflow, httpRequest, telegram, set

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 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.


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 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