Build a Telegram AI Chatbot with n8n and GPT-4o

Struggling to manage multi-format Telegram chats manually? This n8n workflow integrates Telegram with OpenAI’s GPT-4o model to automate intelligent responses to text and voice messages, saving you time and enhancing user interaction.
telegramTrigger
agent
lmChatOpenAi
+5
Workflow Identifier: 1272
NODES in Use: Telegram Trigger, Switch, Telegram, OpenAI OpenAI, Set, LangChain Window Buffer Memory, LangChain AI Agent, Telegram

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

Learn how to Build this Workflow with AI:

Visit through Desktop for Best experience

1. Opening Problem Statement

Meet Ted, an educator and tech enthusiast who runs a busy Telegram channel where users ask questions and share voice notes about technology tutorials. Ted spends countless hours every day manually reading text messages and listening to voice messages to provide personalized responses. This tedious back-and-forth often leads to missed questions, delayed replies, and burnout. On top of that, Ted has noticed some users lose interest when replies come too late or when their voice messages are ignored. Ted needs a hands-off way to keep the conversation lively and engaging while ensuring no message goes unanswered or misunderstood.

But automating responses for a multi-format chat—handling both text and audio voice messages seamlessly—is tricky. Ted often struggles to find a single solution that can intelligently transcribe audio voice notes, remember past interactions to maintain context, and provide meaningful AI-driven replies that can be sent back via Telegram. Without this, Ted loses valuable engagement and wastes hours every day in manual chat management.

2. What This Automation Does ⚙️

This unique n8n workflow acts as a smart Telegram AI chatbot that engages with users by receiving both text and voice messages, transcribing voice to text, processing conversations with OpenAI’s powerful GPT-4o model, maintaining conversational memory, and sending back intelligent replies as HTML-formatted messages. Here’s exactly what happens when this workflow runs:

  • Listens continuously for new Telegram messages including text and voice notes.
  • Automatically detects the message type: text or voice.
  • Downloads voice audio messages and uses OpenAI’s transcription function to convert speech to text.
  • Combines user message content for consistent AI interpretation.
  • Maintains session-based conversational memory to provide contextually relevant replies.
  • Processes the consolidated input through GPT-4o AI chat model to generate detailed, formatted responses in Telegram-friendly HTML.
  • Replies to the user with polite, personalized messages acknowledging message source (text or forwarded voice).
  • Sends typing indicators while processing to keep users engaged.

With this workflow running, Ted regains back multiple hours each day and can focus on content creation instead of message management.

3. Prerequisites ⚙️

  • Telegram bot and API access 📧 (you’ll need a Telegram bot token and chat permissions)
  • OpenAI API key with access to GPT-4o (for both chat completion and audio transcription) 🔑
  • n8n account to create and run workflows (self-hosting available for advanced users for better control) ⏱️
  • Basic familiarity with n8n interface and node setup

4. Step-by-Step Guide ✏️

Step 1: Set up Telegram Trigger to Listen for Messages

In n8n, create a new workflow and add the Telegram Trigger node from the nodes list.

Click on the Telegram Trigger node and select your Telegram API credentials. Configure it to listen for all update types (“*”) to handle text, voice, and other message types.

After saving, you should see a webhook URL generated for Telegram to send message updates. Ted’s Telegram bot uses this URL to send incoming messages to n8n.

Common mistake: Not ensuring your Telegram bot is linked correctly or the webhook URL is registered in Telegram BotFather.

Step 2: Add a Switch Node to Determine Message Type

Add the Switch node called Determine content type. Configure rules to check if the incoming message contains message.text to classify as text, or message.voice to classify as voice message.

Set fallback rule to handle unsupported message types, directing those to an error message reply.

Outcome: The workflow now branches for text or voice message processing.

Common mistake: Incorrectly setting condition expressions or not using strict type validation.

Step 3: Download Voice Messages

For voice messages, add the Telegram node called Download voice file. Configure it to download the voice file_id from the Telegram message payload.

Ensure Telegram API credentials are assigned.

Outcome: The audio file is downloaded to n8n for transcription.

Common mistake: Forgetting to update the file ID expression to dynamically extract from message.

Step 4: Convert Audio to Text with OpenAI

Add the OpenAI Audio Transcription node Convert audio to text from LangChain’s OpenAI package.

Configure it for transcribe operation, no specific language required, set temperature to 0.7 for some transcription creativity.

Set the audio file from the previous node download as input.

Outcome: The voice note is transcribed into a text string that can be processed by the AI chat model.

Common mistake: Disconnecting input or using incompatible audio formats.

Step 5: Combine Content and Set Properties

Add a Set node Combine content and set properties. Use expressions to assign variables:

  • CombinedMessage: Use incoming message.text if present, otherwise the transcribed text from audio.
  • Message Type: Determine 'text query' or 'voice message' or 'unknown type message'.
  • Source Type: Indicate if the message is forwarded.

This acts as a normalized input for the AI Agent.

Common mistake: Misconfiguring the fallback logic causing blanks in CombinedMessage.

Step 6: Maintain Conversational Memory

Add the Window Buffer Memory node from LangChain to track user session messages.

Use the Telegram chat.id as the session key to create a context window of up to 10 previous messages. This keeps the AI aware of conversation history and user intent.

Common mistake: Incorrect expression for session key leading to mixed-up context.

Step 7: Add the OpenAI Chat Model Node

Add the OpenAI Chat Model node from LangChain configured to GPT-4o.

Set temperature to 0.7 and frequency penalty 0.2 for balanced, engaging responses.

Connect conversational memory output to keep replies contextually on point.

Common mistake: Using the wrong model or credentials error.

Step 8: Configure The AI Agent Node

Add the AI Agent node from LangChain.

Configure it to receive the combined message text.

Set a system message template that addresses users by their name, uses Telegram-supported HTML formatting tags, and includes date-time context.

Configure the agent with instructions to respond only with JSON commands that the workflow understands.

Common mistake: Forgetting to add HTML formatting instructions causes Telegram to display raw tags.

Step 9: Send Typing Action

Add a Telegram node called Send Typing action to notify the user that the bot is processing.

Set chat ID from incoming message.

Outcome: User sees the "typing..." indicator in the chat, improving experience.

Step 10: Send Replies Back to User

Add a Telegram node called Send final reply.

Configure it to send the AI Agent's output text to the user with HTML parse mode enabled.

Include a polite thank you message referencing if the message was forwarded or a voice note.

Common mistake: Sending responses to the wrong chat ID or missing parse_mode causing messy formatted replies.

Step 11: Handle Unsupported Commands Gracefully

Add a Telegram node called Send error message.

Use it to reply politely when a user sends unsupported message types (like stickers or images), asking them to send text or voice.

Outcome: Maintains friendly user engagement without confusion.

Step 12: Correct Any HTML Encoding Issues

Add a Telegram node named Correct errors that sanitizes AI-generated output by replacing unsafe characters (<, >, &, etc.) with HTML entities before sending.

This prevents Telegram display errors and injection issues.

5. Customizations ✏️

  • Change AI Model Settings: In the OpenAI Chat Model node, tweak temperature or frequency penalty to make replies more creative or conservative.
  • Add More Memory History: Increase Window Buffer Memory contextWindowLength to remember more conversation turns, useful for longer chats.
  • Support More Message Types: Extend the Determine content type switch node with conditions for photos or documents and integrate respective n8n nodes to handle them.
  • Personalize Responses: Modify the AI Agent system message to customize greeting styles or add special instructions for your audience.
  • Send Media Replies: Add Telegram send photo or audio nodes after AI response for richer engagement.

6. Troubleshooting 🔧

Problem: "Error: Invalid Telegram chat ID" when sending replies.
Cause: The chat ID expression uses wrong user ID or is missing.
Solution: Check the expression in Telegram nodes using chat ID, ensure it matches message.from.id or message.chat.id correctly.

Problem: "OpenAI API request failed" during transcription or chat.
Cause: API key issues or hitting rate limits.
Solution: Verify API key correctness, current usage, and adjust workflow to handle errors gracefully.

Problem: Voice messages not transcribing.
Cause: Incorrect file download setup or unsupported audio format.
Solution: Confirm the Telegram file ID extraction is dynamic and tested; review if OpenAI supports the audio format.

7. Pre-Production Checklist ✅

  • Test Telegram webhook activations with live incoming text and voice messages.
  • Verify OpenAI API keys have permissions for chat and audio transcription.
  • Confirm expressions for chat IDs and file IDs are correctly referencing incoming data.
  • Check that the Window Buffer Memory node maintains session context per user correctly.
  • Run small batch tests replying to both text and voice inputs to ensure accurate AI responses.

8. Deployment Guide

Activate the workflow in n8n after confirming all nodes are connected and credentials are valid.

Monitor incoming Telegram messages and AI replies to ensure smooth interaction.

Enable error catching nodes or logging for production monitoring.

For high volume, consider self-hosting n8n to handle throughput and secure API keys.

9. FAQs

Q: Can I replace GPT-4o with another OpenAI model?
A: Yes, you can adjust the OpenAI Chat Model node to use GPT-3.5 or any supported model, but response quality and formatting might vary.

Q: Does this workflow consume a lot of API credits?
A: It uses OpenAI APIs for chat and transcription, so usage depends on message volume; optimizing temperature and frequency penalty helps control token use.

Q: Is my chat data secure?
A: Your data flows through OpenAI and Telegram via API; ensure your API keys are secured and the n8n environment is protected. Self-hosting can enhance security.

10. Conclusion

By following this detailed guide, you built a fully automated Telegram AI chatbot in n8n that understands both text and voice inputs, transcribes speech, maintains conversational context, and replies with rich, formatted messages using OpenAI's GPT-4o.

Ted, or anyone else managing busy Telegram channels, can reclaim hours of tedious chat handling while providing a highly engaging user experience.

Next steps could include adding multi-language support, integrating other social platforms, or enriching replies with multimedia content.

Keep experimenting, and enjoy your intelligent Telegram assistant!

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 in n8n (Beginner Guide)

A complete beginner guide to building an AI-powered 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