How to build a Telegram bot
A clear, honest, step-by-step path from zero to a working bot, plus the fast lane if you'd rather skip the build.
To build a Telegram bot you create it in BotFather to get an API token, pick a Bot API library such as python-telegram-bot or Telegraf, write a handler that replies to messages, then host and deploy the code. Bots built on the official Bot API are fully within Telegram's Terms of Service. None of the steps cost money on their own, but a bot you can rely on 24/7 is real engineering work, not a five-minute job.
What you need before you start
- A Telegram account (to talk to BotFather and test the bot).
- Basic comfort with one language, Python or JavaScript are the easiest starts.
- A computer to write and run code; hosting can come later.
If the concepts are new, read what a Telegram bot is first, it explains the Bot API and what bots can and can't do, then come back here.
Step 1 — Create the bot in BotFather and get a token
Open Telegram and start a chat with @BotFather, Telegram's official
bot for creating bots. Send /newbot, choose a display name, then a
unique username that ends in bot (for example my_demo_bot).
BotFather replies with an API token that looks like
123456789:AAH.... That token is the credential your code uses to act
as the bot, so keep it private and never commit it to a public repo. Our
get a bot token guide walks through every
prompt with screenshots.
Step 2 — Pick a library or framework
You won't talk to the raw HTTP API by hand; a library handles that for you. Pick one for a language you already know and read its quickstart end to end:
- Python —
python-telegram-bot(beginner-friendly) oraiogram(async-first). - Node.js —
TelegraforgrammY, both clean and well documented. - Go, PHP, Rust, etc. — community libraries exist; the pattern below is the same everywhere.
There's no "best" choice, the right one is the one in a language you can debug. All of the above are free and open source.
Step 3 — Write a basic handler
Here is a minimal Python bot that replies to /start. Install the
library first with pip install python-telegram-bot:
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
async def start(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hello! I'm alive 🤖")
app = ApplicationBuilder().token("YOUR_TOKEN_HERE").build()
app.add_handler(CommandHandler("start", start))
app.run_polling()
Run it, open your bot in Telegram, send /start, and you should get a
reply. That's a working Telegram bot. The same idea in Telegraf (Node.js) is just
a few lines too, the building blocks are always token → register handler →
listen.
Tip: read the token from an environment variable
(os.environ["BOT_TOKEN"]) instead of pasting it in code, so you never
leak it.
Step 4 — Host and deploy it
The script above only runs while your terminal is open. To keep the bot online, you need a host that stays running:
- A small VPS — a $4–6/month server; run the bot under a process manager (
systemd,pm2) so it restarts on crash or reboot. - A container / app platform — deploy a Docker image or a buildpack app to a managed host.
- Serverless — a cloud function triggered per update (pairs well with webhooks, see below).
Wherever you deploy, set the token as a secret/environment variable, and add logging so you can see what the bot is doing in production.
Step 5 (optional) — Switch to webhooks
The example uses long polling: your code repeatedly asks Telegram "any new
messages?". That's perfect for development. For production, webhooks
are usually better: you give Telegram an HTTPS URL with
setWebhook, and Telegram pushes each update to you instantly. Webhooks
cut latency and idle cost, but they need a public HTTPS endpoint with a valid
certificate. Start with polling, move to webhooks once the bot does something real.
Be honest with yourself: this is real work
A "hello world" bot takes minutes. A bot that takes payments, runs a subscription paywall, talks to an AI model, or powers channel search means databases, state, error handling, security and uptime. That's where a weekend project quietly turns into weeks. There's no shame in it, it's just software, but it helps to plan for it. When it's running, our add & use a bot guide covers getting it into your group or channel.
Frequently asked questions
How do I build a Telegram bot for free?
Do I need to know how to code to make a Telegram bot?
What is BotFather and why do I need it?
@BotFather, run /newbot, pick a name and username, and it returns an API token. That token is the credential your code uses to act as the bot, so there is no way to skip this step. See our bot token guide for the exact prompts.Which language or library should I use to create a Telegram bot?
Are Telegram bots allowed by Telegram?
More Telegram bot guides
Bot development serviceSubscription botPayment botAI / ChatGPT botMusic botChannel search botBest Telegram botsWhat is a Telegram bot?How to add & use a botGet a bot token