Meta description: Learn how to build a Telegram bot with Python in 2026, from BotFather to deployment, with a working echo bot example.
Introduction
You can build a Telegram bot with Python and put it in front of real users without a large team. The same bot can answer questions, take orders, send reminders, collect payments, and automate work inside a chat.
This guide goes from BotFather to deployment, with a working echo bot and buttons.
Step 1: create your bot with BotFather
Open Telegram and search for @botfather. It is the official bot for creating bots. Send /newbot, pick a display name, then pick a username ending in bot. BotFather gives you a token.
Treat that token like a password. Do not share it in a screenshot, a public repo, or a chat. Put it in an environment variable or a secrets file, not in your code.
Step 2: choose your Python library
Start with python-telegram-bot if you are new to this. It follows the official Bot API, the docs are clear, and it fits most projects.
Pyrogram uses the MTProto API. That lets it do things the Bot API cannot, such as working with user accounts. It is a good choice if you need account-based features, but it is more complex.
Telethon is similar. It is also based on MTProto, and it fits automation and account-based tools.
For this guide, use python-telegram-bot. Install it with pip install python-telegram-bot, in a virtual environment.
Step 3: build a simple echo bot
An echo bot replies with the same message a user sends. The pattern is the same one every bot uses. Receive an update, run a handler, and send a reply.
Save this as bot.py. Replace the placeholder with your token, preferably through an environment variable.
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CommandHandler, CallbackQueryHandler, MessageHandler, filters
TOKEN = "YOUR_BOT_TOKEN"
async def start(update: Update, context):
keyboard = [[InlineKeyboardButton("Say hello", callback_data="hello")]]
await update.message.reply_text(
"Hello! Send me a message and I'll echo it back.",
reply_markup=InlineKeyboardMarkup(keyboard),
)
async def button(update: Update, context):
query = update.callback_query
await query.answer()
await query.edit_message_text("Hello from your first bot!")
async def echo(update: Update, context):
await update.message.reply_text(update.message.text)
def main():
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CallbackQueryHandler(button))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
app.run_polling()
if __name__ == "__main__":
main()
Run it with python bot.py, then find your bot in Telegram and send /start. Tap the button, then send a normal message. If it replies, your bot works.
CommandHandler handles /start. CallbackQueryHandler handles button taps. MessageHandler handles plain text.
Step 4: add inline keyboards
Buttons are easier to use than typed commands. Keep labels short, one to three words. Give every button a clear callback_data value, such as hello, and handle every value in your code. Always call query.answer() when a button is tapped. If you skip it, Telegram shows a loading spinner that never stops.
Group related actions together, for example "View products", "My orders", and "Support". Do not put ten buttons on one screen. Use a second menu if you need more options.
Step 5: polling vs webhook
Polling means your bot asks Telegram, again and again, "Do you have anything new?" It is simple, it works on your own computer, and it is the right choice while you are learning. The code above uses polling.
A webhook means Telegram sends each new message to your server as soon as it arrives. It is faster at scale, and it needs a public HTTPS URL and a server that is always online.
Start with polling. Move to a webhook when your bot has real users or needs faster replies.
Step 6: deploy it so it stays online
A bot that only runs on your laptop goes offline when you close the lid, lose Wi-Fi, or restart your computer. Users then see silence, and they rarely try again.
For a real bot, use a small server or dedicated bot hosting. Run it as a background service that restarts automatically if it crashes. Add logging and monitoring, keep dependencies in requirements.txt, and use Docker for the same setup on your laptop and server.
Common mistakes to avoid
- Sharing your token. Rotate it in BotFather immediately if it leaks.
- No error handling. A single bad message should not crash the whole bot.
- Too many features at once. Start with one clear job, such as answering FAQs or taking bookings.
- No database plan. If your bot stores users, orders, or settings, choose MongoDB or PostgreSQL early. Files and in-memory lists break on restart.
- Ignoring privacy. Only store the data you need, explain why, and let users delete it.
FAQ
How long does it take to build a Telegram bot with Python?
A simple bot can be ready in a day. A bot with payments, a dashboard, or integrations usually takes one to four weeks.
Do I need a server to run a Telegram bot?
Yes, for anything beyond testing. A server or bot hosting keeps it online around the clock.
Can a Telegram bot take payments?
Yes, through supported providers. Many bots combine a chat flow with a Mini App for checkout.
The risk when AI writes the bot for you
AI can write a working echo bot in minutes. That part is easy. Keeping a bot secure, online, and working when real customers use it is the hard part, and that is where AI-built bots tend to fail.
Veracode's 2025 GenAI Code Security Report found AI-generated code introduced a security flaw in 45% of cases, with Python failing in 38% of cases. Missing input checks, open databases, and hardcoded tokens show up a lot.
AI can also recommend libraries that do not exist. Install one, and you may install a malicious package registered under that made-up name.
AI-generated code has no architect. Each new feature gets stacked on the last, with duplicated logic, until every change risks breaking the bot. When Telegram updates its API, there is no one who knows what to update.
It can also go offline and no one notices. Without hosting, auto-restart, logging, and monitoring, an AI-built bot dies quietly when the laptop closes or the server restarts.
Use AI to learn and to draft. For a customer-facing bot, have a human review, test, and own the code, and have someone to call when it breaks.
BDBOTS builds bots by hand in Python, with Pyrogram and Telethon, plus React and Node.js. Hosting is 99.9% uptime, with auto-restart and monitoring. Projects built by BDBOTS also have lifetime free support. If it breaks, if Telegram changes, or if you need help, you are not starting over alone.
Conclusion
For a side project, this guide is enough to get a bot running. For a bot that handles customers, orders, or payments, you need someone who will keep it running after launch.
BDBOTS is a custom Telegram bot and web studio founded in 2019, with 5+ years, 1000+ projects, 500+ clients, and a typical first reply within 24 hours. The team uses Python (Pyrogram and Telethon), React, Node.js, MongoDB and PostgreSQL, Docker, and hosting with 99.9% uptime, auto-restart, and monitoring. To discuss your idea, visit BDBOTS.
Top comments (0)