In this blog post, I will guide you step-by-step on how to create a simple Telegram bot using Python, along with a working example.
Telegram bots are a powerful way to automate tasks, create custom notifications, handle commands, and much more. If you’ve ever wondered how to make your own Telegram bot, you’re in the right place.
What You Need Before We Start
Before we dive into the code, makesure you have:
- Telegram account
- Python installed on your computer (version 3.6+ recommended)
- A code editor ( like vs code or notepad++ )
- Basic understanding of Python and command line usage.
Step 1: Create a Bot Using BotFather
Telegram has a special bot called BotFather to help you create bots. Follow these steps:
- Open Telegram and search for @BotFather
- Start the chat and send /start
- Send /newbot to create a new bot
- Give your bot a name (e.g., My First Bot)
- Then give it a username (must end with bot, like myfirst_test_bot)
- BotFather will give you a token – copy that.
BotFather will give you a token like this
123456789:ABCdefGhIJklMnOpQRsTUVwxyZ
Note: This token is like a password for your bot. Don’t share it with anyone!
Step 2: Install Required Python Library
We’ll use the popular library
python-telegram-bot to interact
with Telegram.
- Open your terminal or command prompt and run:
- pip install python-telegram-bot --upgrade
Step 3: Write Your First Telegram Bot in Python
Create a file called bot.py and paste the following code:
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
# Replace this with your actual token
BOT_TOKEN = 'YOUR_BOT_TOKEN_HERE'
# Command: /start
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Hello! I am your first Telegram bot 😎')
# Command: /help
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Available commands:\n/start - Start the bot\n/help - Get help')
def main():
app = ApplicationBuilder().token(BOT_TOKEN).build()
app.add_handler(CommandHandler('start', start))
app.add_handler(CommandHandler('help', help_command))
print("Bot is running...")
app.run_polling()
if __name__ == '__main__':
main()
Note: Don’t forget to replace 'YOUR_BOT_TOKEN_HERE' with your own token from BotFather.
Step 4: Run Your Bot
- Now open terminal, navigate to your project folder, and run: python bot.py
- You should see: Bot is running...
- Go to Telegram, open your bot, and type /start. You’ll get a friendly reply!
Extra
Add More Commands You can add more commands like /about, /quote, or anything you want. Just define a new async function and use app.add_handler(CommandHandler('yourcommand', yourfunction)).
Pro Tips
Never share your bot token publicly Host your bot on a server or use services like Heroku, Render, or Railway to keep it running 24/7 Add inline keyboards, buttons, or APIs to make your bot more powerful
Conclusion
Creating a Telegram bot is easier than you might think. With just a few lines of Python, you can automate replies, build custom assistants, or even create fun games inside Telegram. Let me know in the comments if you'd like a guide on how to: Deploy your bot to the cloud Use webhook instead of polling Add database support (like SQLite or Firebase)
0 Comments