n8n tutorial - Lesson 27: Connect n8n to Telegram via Cloudflare Tunnel
Hi everyone, in this post we'll walk through how to connect n8n to Telegram using a Cloudflare Tunnel — making your n8n telegram chatbot reachable from the internet without a VPS. This is part of the n8n Workflow Automation Tutorial series and covers the exact setup used to activate the T7-B2-Telegram-Chatbot workflow in a real session.
How to do:
Step 1 — Install Cloudflare Tunnel (cloudflared)
You need cloudflared installed locally to create a public tunnel to your n8n instance running on localhost:5678.
- Open a terminal and run:
winget install Cloudflare.cloudflared - After installation, start a quick tunnel with:
cloudflared tunnel --url http://localhost:5678 - Copy the generated public URL — it looks like
https://random-name.trycloudflare.com. You will need this in the next step.
Note — This is a "quick tunnel." The URL changes every time you restart cloudflared. That means you must update WEBHOOK_URL and restart n8n each time. A Named Tunnel fixes this — covered at the end of this post.
Step 2 — Set WEBHOOK_URL in Docker Compose
n8n needs to know its own public URL so it can register the correct webhook address with Telegram.
- Open your
docker-compose.ymlfile in a text editor. - Under the
environmentsection for your n8n service, add or update:WEBHOOK_URL=https://your-tunnel-url.trycloudflare.com
- Save the file, then restart n8n by running:
docker compose down && docker compose up -d
Tip — If you skip this step, n8n will still register a localhost address with Telegram, and the webhook will never receive messages. Always set WEBHOOK_URL before activating any Telegram workflow.
Step 3 — Build the Telegram Chatbot Workflow (T7-B2-Telegram-Chatbot)
This workflow receives a user message from Telegram, passes it to an AI Agent, and sends the reply back — the core n8n telegram chatbot pattern.
- In n8n, create a new workflow named
T7-B2-Telegram-Chatbot. - Add a Telegram Trigger node as the starting node. This registers a webhook with Telegram automatically when the workflow is activated.
- Add an AI Agent node (using Claude Haiku 4.5 or your preferred model) connected to the Telegram Trigger.
- In the AI Agent node, set Source for Prompt to Define below, then set the prompt expression to:
{{ $json.message.text }}- This extracts the actual text the user typed in Telegram.
- Do not add a Simple Memory node — Telegram Trigger uses a session concept, but Simple Memory caused issues in testing and was removed.
- Add a Telegram node at the end to send the AI Agent's reply back to the user.
Tip — The prompt expression {{ $json.message.text }} is the correct path for Telegram Trigger output. If you use a generic {{ $json.text }} it will return empty and the agent will have no input to work with.
Step 4 — Activate the Workflow
Activating the workflow registers the webhook URL with Telegram — this is the step that makes everything live.- Make sure
cloudflaredis still running and your n8n instance is up with the correctWEBHOOK_URLset. - In the workflow editor, click the Inactive toggle in the top-right corner to set the workflow to Active.
- n8n will call the Telegram API to register the webhook at your Cloudflare Tunnel URL automatically.
- Open Telegram, find your bot, send a message, and verify the AI Agent replies.
Note — Every time the Cloudflare quick tunnel restarts and gives you a new URL, you must: update WEBHOOK_URL in docker-compose.yml, restart n8n, then toggle the workflow Inactive → Active again. This re-registers the new URL with Telegram.
Step 5 — Understand the Quick Tunnel Limitation and Next Steps
The quick tunnel works perfectly for testing your n8n workflow automation, but it has one real-world drawback you need to plan around.- The problem: The quick tunnel URL (e.g.
https://random-name.trycloudflare.com) changes on every restart ofcloudflared. - Each URL change requires three actions:
- Update
WEBHOOK_URLindocker-compose.yml - Restart n8n with
docker compose down && docker compose up -d - Toggle the Telegram workflow Inactive then Active to re-register the webhook
- Update
- Long-term fix — Named Cloudflare Tunnel: A named tunnel gives you a fixed subdomain (e.g.
https://n8n.yourdomain.com) that never changes. Setup requires a Cloudflare account with a domain attached. - Alternative — VPS deployment: When you deploy n8n on a VPS, the Webhook node automatically gets a public URL. You no longer need
cloudflaredat all.- Note: Oracle Cloud free tier signup from some regions (e.g., Vietnam) can be difficult — factor this into your planning.
Production tip — For a stable n8n telegram chatbot in production, a Named Cloudflare Tunnel or a VPS is strongly recommended. The quick tunnel is only suitable for development and testing sessions.
Key Lessons from This Session
- Always set
WEBHOOK_URLbefore activating a Telegram workflow. Without it, n8n registers alocalhostaddress that Telegram cannot reach. - Quick Cloudflare Tunnel URLs are temporary. The URL changes on every restart — you must update
WEBHOOK_URL, restart n8n, and re-activate the workflow each time. - Use
{{ $json.message.text }}for the AI Agent prompt in a Telegram Trigger workflow. This is the correct data path from the Telegram Trigger output. - Remove Simple Memory when using a Telegram Trigger. Simple Memory added complexity without benefit in this setup and caused issues during testing.
- The Webhook node in n8n does not create a public URL by itself. It only creates an endpoint — you still need
cloudflared, ngrok, or a VPS to expose it publicly. On a VPS, the public URL is automatic. - Named Cloudflare Tunnel or VPS is the correct production path. The quick tunnel is a development shortcut, not a stable deployment strategy.
Conclusion:
In this n8n tutorial, we connected a local n8n instance to Telegram using a Cloudflare Tunnel, activated the T7-B2-Telegram-Chatbot workflow, and got a real AI-powered chatbot responding to Telegram messages. The key takeaway for this n8n workflow automation pattern is that WEBHOOK_URL and tunnel stability are the two variables that determine whether your bot stays online. In the next post in this series, we'll merge the Telegram Chatbot with a RAG pipeline so the bot can answer questions from a knowledge base.
If you have any questions, feel free to leave a comment below. Thank you!
Tags: n8n telegram chatbot, n8n tutorial, n8n workflow automation, cloudflare tunnel n8n, n8n webhook setup, n8n docker webhook, n8n AI agent, n8n beginner tutorial
Maybe you are interested!
- Getting Started with n8n: Interface & Your First Manual Trigger
- n8n HTTP Request Node: Connect Any API Without Code
- Branching Workflows: IF, Switch & Merge Nodes in n8n
- n8n Expressions & Built-in Variables: The Complete Guide
- Comparing AI Models in n8n: Claude vs Gemini vs ChatGPT
- Connect Gmail to n8n: OAuth Setup & Reading Emails
- Build an AI Email Classifier with n8n
- Automated Email Digest to Telegram with n8n
- Google Sheets Automation with n8n: 4 Key Operations
- Auto-Generate Google Docs from Data with n8n