What a Telegram bot is and what you can build
A Telegram bot is a program that runs on Telegram's servers and responds to messages in chats. You write the code, Telegram handles delivering your messages to users. Bots can do simple things — repeat what you say, send you weather updates, remind you of tasks — or complex things like process payments or search databases. You do not need to run a server yourself; Telegram's infrastructure handles that part.
This guide covers building a basic bot that responds to text commands. You will write Python code, register your bot with Telegram, and connect it to Telegram's messaging system. The whole process takes an hour or two if you have never done it before.
Key Takeaways
- You create a bot by messaging BotFather on Telegram, which gives you a unique token that proves your bot is yours.
- Python with the python-telegram-bot library is the most straightforward way to write the code, and you can run it on your own computer or a free hosting service.
- Your code connects to Telegram using the token, then waits for messages and sends responses based on rules you write.
- Testing happens in a real Telegram chat with your bot before you share it with others.
Register your bot with BotFather
Before you write any code, you need to register your bot with Telegram. Open Telegram and search for the user BotFather — this is Telegram's official bot for creating new bots. Start a chat with BotFather and send the message /newbot.
BotFather will ask you two things: a display name for your bot (this is what appears in chat) and a username (this must end with "bot" and be unique across all Telegram). Choose something simple like "MyTestBot" for the name and "my_test_bot" for the username. BotFather will then send you a long string of characters called a token. This token is how your code proves it is your bot. Save this token somewhere safe — do not share it with anyone.
Install Python and the telegram bot library
You will write your bot in Python, a programming language that is straightforward to learn. If you do not have Python installed, go to python.org, download the latest version, and run the installer. Choose the option to add Python to your PATH during installation.
Once Python is installed, open a command prompt or terminal and run this command:
pip install python-telegram-bot
This installs the python-telegram-bot library, which handles all the communication between your code and Telegram's servers. You do not have to write that communication yourself; the library does it.
Write your first bot code
Create a new file called bot.py and paste this code into it:
from telegram import Update from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text("Hello! I am your bot. Send me any message and I will repeat it back.") async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await update.message.reply_text(update.message.text) def main() -> None: application = Application.builder().token("YOUR_TOKEN_HERE").build() application.add_handler(CommandHandler("start", start)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) application.run_polling() if __name__ == '__main__': main()
Replace YOUR_TOKEN_HERE with the actual token BotFather gave you. This code does three things: it defines a start function that runs when someone sends /start, it defines an echo function that repeats back any message, and it tells the library to listen for messages and run the right function when they arrive.
Run your bot and test it
Save the file and open a command prompt in the same folder. Type python bot.py and press Enter. If there are no error messages, your bot is now running and listening for messages. The command prompt will stay open and show activity as messages arrive.
Open Telegram and search for your bot by the username you chose (the one ending in "bot"). Start a chat with it and send /start. Your bot should reply with the greeting message. Then send any text message — your bot should repeat it back to you. If it does, your bot is working.
To stop the bot, press Ctrl+C in the command prompt. You can start it again the same way.
Add more commands and responses
Once the basic bot works, you can add more commands. Here is how to add a command that returns the current time:
from datetime import datetime async def time_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: current_time = datetime.now().strftime("%H:%M:%S") await update.message.reply_text(f"The current time is {current_time}") def main() -> None: application = Application.builder().token("YOUR_TOKEN_HERE").build() application.add_handler(CommandHandler("start", start)) application.add_handler(CommandHandler("time", time_command)) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo)) application.run_polling()
The new time_command function gets the current time and sends it back. The line application.add_handler(CommandHandler("time", time_command)) tells your bot to run this function when someone sends /time. You can add as many commands as you want by following this same pattern.
Keep your bot running after you close your computer
The code above only runs while your computer is on and the command prompt is open. If you want your bot to stay active all the time, you need to run it on a server that is always on. Free options include Heroku (now paid but still has free alternatives), Railway, or Replit. These services let you upload your code and run it continuously without keeping your own computer on.
Each service has its own steps for uploading and running Python code. Most require you to create an account, upload your bot.py file, and set an environment variable with your token so you do not have to paste it into the code. The documentation for each service walks you through this process.
Frequently Asked Questions
What if my bot does not respond to messages?
Check three things: the token is correct and pasted exactly as BotFather gave it, the command prompt shows no error messages when you run the code, and you are messaging the right bot (search by username, not display name). If the command prompt shows an error, read it carefully — it usually points to the exact line that is broken.
Can I make my bot do something when I send a specific word, not just a command?
Yes. Instead of CommandHandler, use MessageHandler with a filter. For example, MessageHandler(filters.Regex("hello"), your_function) will run your function whenever someone sends a message containing "hello". You can filter by text, by user, by chat type, or by combinations of these.
How do I make my bot send a message without waiting for someone to message it first?
You need the chat ID of the person or group you want to message. You can get this by sending a message to your bot and reading the update object in your code. Once you have the chat ID, use context.bot.send_message(chat_id=your_chat_id, text="Your message") to send a message whenever you want.
Is there a limit to how many people can use my bot?
Telegram does not limit the number of users. Your bot can serve thousands of people. The only limit is the server you run it on — if you use a free hosting service, it may have restrictions on how much processing power or bandwidth you can use.
What happens if I share my token with someone else?
They can control your bot and send messages as if they are you. Never paste your token in public or share it in code you upload to GitHub. If you think someone has your token, go back to BotFather, send /revoke, choose your bot, and it will generate a new token.