Telegram bots are powerful tools for automation, customer service, and productivity. In this tutorial, I will show you how to build a feature-rich Telegram bot with Python in just 15 minutes.
Why Telegram Bots?
Telegram bots are perfect for:
- Customer support automation
- Daily notifications and reminders
- Data collection and surveys
- File sharing and processing
- E-commerce order tracking
Step 1: Create Your Bot
First, create a bot with BotFather on Telegram:
- Open Telegram and search for
@BotFather - Send
/newbot - Choose a name and username for your bot
- Copy your bot token (keep it secret!)
Step 2: Install python-telegram-bot
pip install python-telegram-bot
Step 3: Build a Basic Bot
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
TOKEN = "YOUR_BOT_TOKEN_HERE"
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(
"Hello! I am your AI assistant bot.\n\n"
"Commands:\n"
"/start - Show this message\n"
"/help - Get help\n"
"/weather - Get weather info\n"
"Or just send me any message!"
)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Send me any text and I will respond intelligently!")
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_text = update.message.text
user_name = update.effective_user.first_name
response = f"Hi {user_name}! You said: {user_text}"
await update.message.reply_text(response)
def main():
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("help", help_command))
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
print("Bot is running...")
app.run_polling()
if __name__ == "__main__":
main()
Step 4: Add AI Responses with OpenAI
Make your bot intelligent:
import openai
openai_client = openai.OpenAI(api_key="YOUR_OPENAI_KEY")
async def ai_response(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_message = update.message.text
await update.message.reply_text("Thinking...")
response = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant bot on Telegram."},
{"role": "user", "content": user_message}
]
)
ai_reply = response.choices[0].message.content
await update.message.reply_text(ai_reply)
# Replace the echo handler with AI response handler
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, ai_response))
Step 5: Add Inline Buttons
Create interactive menus:
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
async def menu(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [
[InlineKeyboardButton("Option 1", callback_data="opt1"),
InlineKeyboardButton("Option 2", callback_data="opt2")],
[InlineKeyboardButton("Contact Support", url="https://t.me/yoursupport")]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text("Choose an option:", reply_markup=reply_markup)
Step 6: Deploy to a Server
Use a Dockerfile for easy deployment:
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY bot.py .
CMD ["python", "bot.py"]
Deploy free on Railway.app or Render.com!
Common Use Cases for Businesses
- E-commerce: Order status updates, tracking notifications
- Customer Support: Auto-answer FAQs, ticket creation
- Content: Daily news digests, blog post notifications
- HR: Leave requests, attendance tracking
- Restaurants: Menu ordering, reservations
Want a Custom Telegram Bot Built for You?
Building a production-ready Telegram bot with AI features, database integration, and business logic takes expertise. Instead of spending days learning, let me build it for you:
- WhatsApp and Telegram Bot Development starting at $20
- Fast delivery, clean code, full documentation included
Got questions about Telegram bot development? Drop a comment below!
Top comments (0)