Skip to content
Home/Resources/Build guides/Telegram bot on Cloudflare Workers
Build guide

Build a Telegram Bot for Free on Cloudflare Workers, Step by Step

By Younus FardeenUpdated 31 Oct 20263 min read
Short answer

Create a bot token with BotFather, write a Cloudflare Worker that receives Telegram updates by webhook and replies through the Bot API, store the token as a Worker secret, deploy with Wrangler and point Telegram's webhook at your Worker URL. The Workers free plan allows 100,000 requests a day, far more than a hobby bot needs.

You will buildA bot that replies to messages, with one useful command such as a daily reminder, a quote or a lookup against an API.
TimeAbout 2 hours
Cost₹0 on the Workers free plan
LevelBeginner (basic JavaScript)

Tools you need

The 10 steps

01

Create the bot

In Telegram, message BotFather, send /newbot, choose a name and a username. Copy the token it gives you.

02

Create a Worker project

Run npm create cloudflare@latest my-bot, choose a Hello World Worker in JavaScript, then cd my-bot.

03

Store the token as a secret

Run npx wrangler secret put BOT_TOKEN and paste the token. It is now available as env.BOT_TOKEN and never in your code.

04

Write the handler

In src/index.js:

export default {
  async fetch(request, env) {
    if (request.method !== 'POST') return new Response('ok');
    const update = await request.json();
    const msg = update.message;
    if (!msg || !msg.text) return new Response('ok');
    const reply = msg.text === '/start' ? 'Hello! Send me any text.' : `You said: ${msg.text}`;
    await fetch(`https://api.telegram.org/bot${env.BOT_TOKEN}/sendMessage`, {
      method: 'POST', headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ chat_id: msg.chat.id, text: reply })
    });
    return new Response('ok');
  }
}
05

Deploy

Run npx wrangler deploy. Copy the workers.dev URL it prints.

06

Set the webhook

Open in a browser (with your values): https://api.telegram.org/bot<TOKEN>/setWebhook?url=https://<your-worker>.workers.dev. Telegram replies with "ok": true.

07

Test

Message your bot in Telegram. Send /start and a sentence; you should get replies within a second.

08

Add one real feature

For example, call a public API (weather, exchange rate, a quote) and format the result, or store user notes in Workers KV (free tier).

09

Protect the webhook

Set a secret token with setWebhook's secret_token parameter and check the X-Telegram-Bot-Api-Secret-Token header in the Worker, so only Telegram can call it.

10

Document

Add the commands, the deploy steps and the free-plan limits to your README.

Make it yours

  • A reminder bot using Cron Triggers (also free).
  • A group moderation bot that deletes links from new members.
  • A study bot that quizzes you from a Google Sheet.

Troubleshooting

ProblemFix
Bot does not replyCheck the webhook info at getWebhookInfo for errors, and read Worker logs with npx wrangler tail.
401 from TelegramThe token is wrong or the secret was not set. Re-run wrangler secret put.
Replies arrive twiceYour handler returned an error so Telegram retried. Always return 200 quickly.

Frequently asked questions

Are Telegram bots free to run?

Yes. The Bot API is free and Cloudflare Workers' free plan covers 100,000 requests a day.

Can I build a WhatsApp bot the same way?

WhatsApp requires the Meta Cloud API with business verification and has conversation pricing; Telegram is the free way to learn bots.

Do I need a server?

No. The Worker runs on demand when Telegram sends an update.

Learn this with mentors

Programmes for builders and engineers with admissions open: live classes, mentors and project work.

Continue learning

Project guide · Software

Software projects

Full-stack apps with auth, tests, real-time features and CI.

Open guide →23 ideas
Free tools

Free cloud credits and hosting

Credits and always-free tiers to host real projects.

See the list →12 tools
About 3 hours

First web app with Cloudflare and Supabase

A React app with sign-in and a database, live on Cloudflare Pages.

Step by step →11 steps