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.
Tools you need
The 10 steps
Create the bot
In Telegram, message BotFather, send /newbot, choose a name and a username. Copy the token it gives you.
Create a Worker project
Run npm create cloudflare@latest my-bot, choose a Hello World Worker in JavaScript, then cd my-bot.
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.
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');
}
}Deploy
Run npx wrangler deploy. Copy the workers.dev URL it prints.
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.
Test
Message your bot in Telegram. Send /start and a sentence; you should get replies within a second.
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).
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.
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
| Problem | Fix |
|---|---|
| Bot does not reply | Check the webhook info at getWebhookInfo for errors, and read Worker logs with npx wrangler tail. |
| 401 from Telegram | The token is wrong or the secret was not set. Re-run wrangler secret put. |
| Replies arrive twice | Your 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.








