Why Telegram + n8n is the Perfect Combination
Telegram has over 900 million active users and one of the best bot APIs in the world. Bots are first-class citizens in Telegram — they can receive messages, send images, handle buttons, process voice notes, and even accept payments. Combined with n8n, you get a visual automation platform that makes building complex bot logic as easy as dragging and dropping nodes.
Telegram bots built with n8n and ChatGPT are being used for:
- Customer support that handles 80% of inquiries automatically
- Personal AI assistants that remember your preferences
- Business tools that look up data, generate reports, and notify teams
- Lead qualification bots that qualify and route prospects
- Internal tools for companies — HR bots, IT support, knowledge bases
Time to build: This tutorial takes about 30 minutes from start to a working bot. No VPS needed for testing — you can run everything locally.
Step 1: Create Your Telegram Bot with BotFather
Every Telegram bot starts with BotFather — Telegram's official bot management tool.
- Open Telegram and search for @BotFather
- Start a conversation and send
/newbot - Follow the prompts: enter a name (e.g., "My AI Assistant") and a username (must end in "bot", e.g.,
my_ai_assistant_bot) - BotFather will give you an HTTP API token — copy it immediately
- Optionally set a description with
/setdescriptionand a profile photo with/setuserpic
Your bot token looks like this:
7312845691:AAF3g8_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Keep this secret! Anyone with this token can control your bot.Step 2: Set Up n8n and Add Telegram Credential
If you don't have n8n yet, the fastest way to start is with Docker:
docker run -it --rm \
--name n8n \
-p 5678:5678 \
-v ~/.n8n:/home/node/.n8n \
docker.n8n.io/n8nio/n8nThen open http://localhost:5678. Now add the Telegram credential:
- Go to Settings → Credentials → Add Credential
- Search for Telegram API
- Paste your bot token and save
Step 3: Build the Webhook Trigger
Telegram bots work in two ways: polling (n8n asks Telegram for new messages every few seconds) or webhooks (Telegram sends messages to n8n instantly). Webhooks are faster and more efficient.
For webhooks to work, n8n must be accessible from the internet. If you're running locally, use ngrok to create a public URL:
# Install ngrok and expose port 5678
ngrok http 5678
# You'll get a URL like:
# https://abc123.ngrok.ioIn n8n, create a new workflow and add a Telegram Trigger node:
- Credential: your Telegram API credential
- Updates: select Message
- Copy the webhook URL that n8n generates
Register the webhook with Telegram by visiting:
https://api.telegram.org/bot{YOUR_TOKEN}/setWebhook?url={YOUR_N8N_WEBHOOK_URL}Step 4: Add the OpenAI Node
After the Telegram Trigger, add an OpenAI node:
- Operation: Message a Model
- Model: gpt-4o-mini
- System Message:
You are a helpful assistant. Be concise and friendly. - User Message:
{{$json.message.text}}
Note: The expression{{$json.message.text}}extracts the user's message text from the Telegram webhook payload. Every message Telegram sends contains amessageobject with atextfield.
Step 5: Send the Reply Back to Telegram
Add a Telegram action node after OpenAI:
- Operation: Send Message
- Chat ID:
{{$("Telegram Trigger").item.json.message.chat.id}} - Text:
{{$json.choices[0].message.content}}
Activate the workflow. Send your bot a message on Telegram — it should reply within 1–2 seconds with a GPT-4o-mini response. You've just built an AI Telegram bot!
Adding Conversation Memory
The basic bot forgets context after each message. To enable multi-turn conversations, switch to theAI Agent node with memory:
- Replace the OpenAI node with an AI Agent node
- Connect a Chat Model sub-node (OpenAI GPT-4o-mini)
- Connect a Simple Memory sub-node
- Set Session ID to
{{$("Telegram Trigger").item.json.message.chat.id}} - Set Prompt (chat input) to
{{$json.message.text}}
Now each Telegram chat has its own memory. Ask the bot "What's 2+2?", then ask "Multiply that by 5" — it remembers the context. This is what makes bots feel like real assistants.
Handling Images from Users
When a user sends a photo to your bot, the Telegram payload contains a photo array instead of a textfield. Here's how to handle it:
- Add an IF node after the Telegram Trigger:
- Condition:
{{$json.message.photo}}exists
- Condition:
- On the "true" branch: get the file URL via Telegram's getFile API, then pass the image URL to GPT-4o (not mini) with vision enabled
- On the "false" branch: standard text processing with GPT-4o-mini
// Get the largest photo from the array
// Telegram sends multiple sizes, last one is highest res
{{$json.message.photo[$json.message.photo.length - 1].file_id}}
// Telegram getFile endpoint
https://api.telegram.org/bot{TOKEN}/getFile?file_id={FILE_ID}
// Download URL format
https://api.telegram.org/file/bot{TOKEN}/{file_path}Handling Voice Messages
Voice notes are extremely common in Telegram. Processing them requires Whisper (OpenAI's speech-to-text):
- Detect voice message:
{{$json.message.voice}}exists - Download the OGG audio file using the Telegram getFile API
- Pass the audio to the OpenAI Whisper node (Transcribe Recording)
- Use the transcribed text as the user message for GPT
This makes your bot accessible to people who prefer speaking over typing — a huge usability improvement, especially for mobile users.
Deploy to Production
When your bot is working locally, it's time to deploy it properly for 24/7 operation.
Option A: n8n Cloud ($20/month)
Sign up at n8n.io, import your workflow, and activate it. n8n handles hosting, SSL, and uptime. Best for non-technical users or when you want zero maintenance.
Option B: Self-hosted VPS ($4-5/month)
Rent a VPS from Hetzner (CX11 at €3.49/month) or DigitalOcean. Install Docker and run:
# docker-compose.yml for production n8n
version: "3.8"
services:
n8n:
image: docker.n8n.io/n8nio/n8n
restart: always
ports:
- "5678:5678"
environment:
- N8N_HOST=your-domain.com
- N8N_PROTOCOL=https
- WEBHOOK_URL=https://your-domain.com/
volumes:
- ~/.n8n:/home/node/.n8nAdd Nginx as a reverse proxy with SSL from Let's Encrypt. Your bot will be live 24/7 for under €5/month.
Want to build and sell Telegram bots professionally? Our AI Chatbot Development course (€59) covers building, packaging and selling chatbot solutions to local businesses — with real client scripts and pricing templates.
Common Bot Commands to Add
Improve UX by adding command handling. Use an IF or Switch node to route commands to different branches:
/start— welcome message and instructions/help— list available commands/reset— clear conversation memory/language— switch response language
// Check if message starts with "/"
// In the IF node:
{{$json.message.text.startsWith('/')}}
// Get the command name:
{{$json.message.text.split(' ')[0].slice(1)}}