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.
  1. Open a terminal and run: winget install Cloudflare.cloudflared
  2. After installation, start a quick tunnel with: cloudflared tunnel --url http://localhost:5678
  3. 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.
  1. Open your docker-compose.yml file in a text editor.
  2. Under the environment section for your n8n service, add or update:
    • WEBHOOK_URL=https://your-tunnel-url.trycloudflare.com
  3. 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.
  1. In n8n, create a new workflow named T7-B2-Telegram-Chatbot.
  2. Add a Telegram Trigger node as the starting node. This registers a webhook with Telegram automatically when the workflow is activated.
  3. Add an AI Agent node (using Claude Haiku 4.5 or your preferred model) connected to the Telegram Trigger.
  4. 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.
  5. Do not add a Simple Memory node — Telegram Trigger uses a session concept, but Simple Memory caused issues in testing and was removed.
  6. 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.
  1. Make sure cloudflared is still running and your n8n instance is up with the correct WEBHOOK_URL set.
  2. In the workflow editor, click the Inactive toggle in the top-right corner to set the workflow to Active.
  3. n8n will call the Telegram API to register the webhook at your Cloudflare Tunnel URL automatically.
  4. 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.
  1. The problem: The quick tunnel URL (e.g. https://random-name.trycloudflare.com) changes on every restart of cloudflared.
  2. Each URL change requires three actions:
    • Update WEBHOOK_URL in docker-compose.yml
    • Restart n8n with docker compose down && docker compose up -d
    • Toggle the Telegram workflow Inactive then Active to re-register the webhook
  3. 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.
  4. Alternative — VPS deployment: When you deploy n8n on a VPS, the Webhook node automatically gets a public URL. You no longer need cloudflared at 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

  1. Always set WEBHOOK_URL before activating a Telegram workflow. Without it, n8n registers a localhost address that Telegram cannot reach.
  2. 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.
  3. 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.
  4. Remove Simple Memory when using a Telegram Trigger. Simple Memory added complexity without benefit in this setup and caused issues during testing.
  5. 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.
  6. 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!