What a Telegram bot is and what you can build
A Telegram bot is a program that runs inside Telegram and responds to messages automatically. You write the code, Telegram's servers run it, and users message your bot like they would message a person. Bots can answer questions, send reminders, fetch information from websites, or control other services — all without the user leaving Telegram.
You do not need to be a professional programmer to build one. Telegram provides a free interface called the Bot API that handles all the messaging work. You write the logic in Python, JavaScript, or another language, and Telegram delivers messages to your code and sends your replies back to users.
The simplest bots take a few hours to build. More complex ones — that remember user preferences, connect to databases, or run scheduled tasks — take longer but follow the same basic pattern.
Key Takeaways
- You create a bot by messaging BotFather on Telegram, which gives you an API token that identifies your bot to Telegram's servers.
- You write code that receives messages and sends replies, using a library like python-telegram-bot or Telegraf to handle the connection.
- Your code runs on your own computer, a server you rent, or a free hosting service, and Telegram sends incoming messages to it automatically.
- Testing happens in Telegram itself — you message your bot, see what it does, and fix the code until it works the way you want.
Getting your API token from BotFather
The first step is to register your bot with Telegram. Open Telegram and search for the account named BotFather — this is Telegram's official bot for creating other bots. Message it with /start to see the menu.
Type /newbot and BotFather will ask for a name (what users see) and a username (what they type to find you). The username must end with bot and be unique across all of Telegram. If someone already chose your first choice, try adding a number or descriptor.
BotFather then sends you an API token — a long string of numbers and letters. This token is your bot's password. Anyone with it can control your bot, so treat it like a password: never paste it in public, never commit it to GitHub without hiding it, never share it in screenshots. Save it somewhere safe on your computer.
Choosing a programming language and library
You need two things: a programming language you know or want to learn, and a library that speaks Telegram's language for you. The library handles the boring work of connecting to Telegram, parsing messages, and formatting replies.
Python with python-telegram-bot is the most common choice for beginners. Python reads like English, the library is well-documented, and thousands of example bots exist online. Install it by opening your terminal and typing pip install python-telegram-bot.
JavaScript with Telegraf is popular if you already know JavaScript or Node.js. Install it with npm install telegraf. Go with telebot, C# with Telegram.Bot, and Java with TelegramBots are also solid choices if you prefer those languages. Pick whichever you know best — the concepts are the same in all of them.
Writing your first bot in Python
Create a new file called bot.py and paste this code:
from telegram import Update from telegram.ext import Application, CommandHandler, ContextTypes async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text("Hello! I'm a bot.") async def main() -> None: application = Application.builder().token("YOUR_API_TOKEN_HERE").build() application.add_handler(CommandHandler("start", start)) await application.run_polling() if __name__ == '__main__': import asyncio asyncio.run(main())
Replace YOUR_API_TOKEN_HERE with the token BotFather gave you. This bot listens for the /start command and replies with "Hello! I'm a bot."
Open your terminal, navigate to the folder where you saved bot.py, and type python bot.py. If it runs without errors, you should see a message saying the bot is polling. Open Telegram, find your bot by its username, and type /start. It should reply immediately.
Congratulations — your bot is live. Stop it by pressing Ctrl+C in the terminal.
Adding more commands and responses
The example bot only responds to /start. To make it do more, add more handlers. Here is a bot that responds to /hello and /time:
from telegram import Update from telegram.ext import Application, CommandHandler, ContextTypes from datetime import datetime async def hello(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text("Hi there!") async def current_time(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: now = datetime.now().strftime("%H:%M:%S") await update.message.reply_text(f"The time is {now}") async def main() -> None: application = Application.builder().token("YOUR_API_TOKEN_HERE").build() application.add_handler(CommandHandler("start", hello)) application.add_handler(CommandHandler("hello", hello)) application.add_handler(CommandHandler("time", current_time)) await application.run_polling() if __name__ == '__main__': import asyncio asyncio.run(main())
Each add_handler line adds a new command. The first argument is the command name (what the user types), and the second is the function that runs. You can add as many as you want.
To respond to regular messages instead of commands, use MessageHandler instead of CommandHandler. The library's documentation shows examples of filtering by text, photo, or other message types.
Keeping your bot running all the time
The bot only works while your computer is on and the terminal is open. For a bot people actually use, you need it running 24/7. You have three options.
Run it on your computer with a service. On Windows, use NSSM (Non-Sucking Service Manager) to run your Python script as a background service. On Mac or Linux, use systemd or launchd. This works if your computer stays on all the time, but uses electricity and ties up your machine.
Rent a server. Services like DigitalOcean, Linode, or AWS let you rent a small computer in the cloud for a few dollars a month. You upload your bot code, run it there, and it stays on forever. This is the most reliable option for bots people depend on.
Use free hosting. Platforms like Replit, Railway, or Heroku's free tier let you run code for free, though with limits on how much computing power you get. These are good for learning and testing, but may shut down your bot if it uses too many resources.
Most beginners start with their own computer, then move to a paid server once they know the bot works.
Frequently Asked Questions
Do I need to pay Telegram to create a bot?
No. Telegram's Bot API is free. You only pay if you use a paid hosting service to keep your bot running, but many free options exist.
Can my bot send messages without the user messaging it first?
Yes, but only if the user has messaged your bot at least once. Telegram does not let bots spam random users. Once someone messages your bot, you can send them messages anytime using their user ID.
What if I want my bot to do something every hour or every day?
Use a scheduler library like APScheduler for Python or node-schedule for JavaScript. These let you run a function at a specific time or interval. The library documentation has examples of scheduling tasks.
How do I hide my API token so it does not leak on GitHub?
Store it in a file called .env and load it with the python-dotenv library. Add .env to your .gitignore file so Git never uploads it. This way your code is public but your token stays secret.
Can I make my bot work in groups or channels, not just direct messages?
Yes. Add your bot to a group or channel, and it receives messages there too. You can filter which messages it responds to — for example, only messages that mention it by name, or only messages starting with a slash.