Telegram bots are one of the fastest ways to build a user-facing automation tool. This guide goes from zero to a working bot with commands, keyboards, and webhook support.
Create Your Bot
- Open Telegram, search for @BotFather
- Send /newbot
- Choose a name and username (must end in 'bot')
- Copy the API token BotFather gives you
Install python-telegram-bot
pip install python-telegram-bot --upgrade
Basic Bot With Commands
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
async def start(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Hello! I am your bot 🤖')
async def help_cmd(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Commands: /start /help')
app = Application.builder().token('YOUR_BOT_TOKEN').build()
app.add_handler(CommandHandler('start', start))
app.add_handler(CommandHandler('help', help_cmd))
app.run_polling()
Inline Keyboards
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
keyboard = [[
InlineKeyboardButton('Option A', callback_data='a'),
InlineKeyboardButton('Option B', callback_data='b'),
]]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text('Choose:', reply_markup=reply_markup)
Message Handlers
from telegram.ext import MessageHandler, filters
async def handle_text(update: Update, ctx: ContextTypes.DEFAULT_TYPE):
user_msg = update.message.text
await update.message.reply_text(f'You said: {user_msg}')
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_text))
Webhook Deployment (Production)
app.run_webhook(
listen='0.0.0.0',
port=8443,
secret_token='YOUR_SECRET',
webhook_url='https://yourdomain.com/webhook'
)
Set the webhook with: https://api.telegram.org/bot{TOKEN}/setWebhook?url=https://yourdomain.com/webhook
Need a custom Telegram bot? Contact ZeroPhantom on Kwork →