A Telegram bot built in n8n usually starts with a Telegram Trigger or a plain Webhook node, a few IF nodes and a reply. It works in an afternoon. What often gets skipped is the question of who else can call that webhook URL, and what happens when they do.
A webhook is just a public HTTPS endpoint. If someone finds or guesses the URL, they can post fake updates to it, and your workflow will happily process them: create leads, send messages, or call an AI model you pay for. This post walks through the small set of protections I add to every Telegram bot workflow before it goes live.
Webhook or polling?
Telegram offers two ways to receive updates: long polling (getUpdates) and webhooks (setWebhook). n8n's Telegram Trigger uses a webhook, which is the right choice for most self-hosted setups because updates arrive instantly and you don't need a process looping all day.
The trade-off is exposure. With polling, your server calls Telegram and nothing needs to be reachable from outside. With a webhook, Telegram calls you, so the endpoint must be public. That is fine, as long as you verify that each request really came from Telegram.
Step 1: set a secret token when registering the webhook
When you call setWebhook, Telegram lets you pass a secret_token parameter. Telegram then sends that value in the X-Telegram-Bot-Api-Secret-Token header with every update. Anyone who doesn't know the secret can't produce a valid request.
If you register the webhook yourself (for example when using a generic Webhook node instead of the Telegram Trigger), the call looks like this:
curl -s "https://api.telegram.org/bot<BOT_TOKEN>/setWebhook" \ -d "url=https://n8n.example.com/webhook/telegram-support" \ -d "secret_token=<LONG_RANDOM_STRING>" \ -d "allowed_updates=[\"message\",\"callback_query\"]" Generate the secret with something like openssl rand -hex 32 and store it as an n8n credential or environment variable, not in a Set node where it ends up in every export.
allowed_updates matters too: if your bot only handles messages and button presses, don't subscribe to everything. Fewer update types means fewer surprises.
Step 2: check the header as the very first node
Right after the Webhook node, add an IF node that compares the incoming header with your stored secret:
- Value 1:
{{ $json.headers["x-telegram-bot-api-secret-token"] }} - Operation: equals
- Value 2:
{{ $env.TELEGRAM_WEBHOOK_SECRET }}
On the false branch, respond with a 200 and stop. Don't return an error message that explains what failed, and don't run anything else. On the true branch, continue with the real logic.
If you use the built-in Telegram Trigger node, check how your n8n version registers the webhook and whether it sets a secret for you. If you can't confirm it, a generic Webhook node with your own setWebhook call gives you full control.
Step 3: allow-list chats for internal bots
Many business bots are not meant for the public. An internal bot that posts new orders to a sales group, or lets staff look up stock, should only respond to known chat IDs.
Keep a small list of allowed chat.id values (a Data Table, a Google Sheet or an environment variable all work) and check {{ $json.body.message.chat.id }} against it before doing anything. Unknown chats get a polite fixed reply, or nothing at all. Log the attempt with the chat ID and timestamp so you can see if someone is probing the bot.
For public bots, such as a customer support bot, an allow-list doesn't fit, but you can still rate-limit per chat ID: store the time of the last few requests and stop answering if one chat sends dozens of messages a minute.
Step 4: treat message text as untrusted input
Everything a user types is untrusted. That sounds obvious, but in n8n it is easy to drop {{ $json.body.message.text }} straight into a SQL query, an HTTP request URL or an AI prompt.
A few habits help:
- Use parameterised queries in database nodes instead of building SQL strings.
- URL-encode user input before putting it in a query string.
- If the bot passes text to an AI model, keep your instructions in the system message and the user's text clearly separated, and never let the model's output trigger actions (refunds, deletions, outgoing emails) without a deterministic check.
- Truncate very long messages before processing them.
Step 5: keep replies and secrets out of execution logs
n8n stores execution data by default. For a Telegram bot that can mean customer names, phone numbers and message history sitting in your database indefinitely.
Set execution data pruning to a sensible retention period, and consider saving only failed executions for high-volume bot workflows. Never echo the bot token or the webhook secret into a message, a Set node or an error notification.
Step 6: answer fast, work later
Telegram expects a quick response from your webhook. If your workflow calls a slow API or an AI model before replying, Telegram may retry the update, and you end up processing the same message twice.
Respond to the webhook immediately (Respond to Webhook node, or the "respond immediately" option), then do the slow work and send the reply with a separate Telegram node. Store the update_id of processed updates for a short time and skip duplicates. That one check prevents double replies and double records.
A short pre-launch checklist
- Webhook registered with a
secret_token, and the header checked first. -
allowed_updateslimited to what the bot uses. - Chat allow-list (internal bots) or per-chat rate limit (public bots).
- User text never concatenated into SQL, URLs or privileged prompts.
- Execution data retention set; no secrets in logs.
- Immediate webhook response and
update_idde-duplication.
None of this takes long, and it turns a bot that "works" into one you can leave running for months.
I build Telegram bots on n8n for small businesses, from order notifications to AI-assisted support with human handoff. If you want to see how I approach them, there's an overview on my AI and Telegram bot services page.
Alec Asgari has spent 16 years in IT, the last five in Dubai, building automations, bots, websites and ERP setups for small businesses. More at alecasgari.com.

