Tại sao nên tạo Telegram Bot?
Telegram Bot là dự án thực tế tuyệt vời: nhắc lịch, theo dõi giá crypto, quản lý to-do list, gửi báo cáo tự động.
Cài đặt
pip install python-telegram-bot
# Tạo bot qua @BotFather trên Telegram
Code bot cơ bản
from telegram import Update
from telegram.ext import Application, CommandHandler
TOKEN = "YOUR_BOT_TOKEN"
async def start(update: Update, context):
await update.message.reply_text(
"Xin chào! Tôi là bot Python Vietnam 🐍\n"
"Gõ /help để xem danh sách lệnh."
)
async def quote(update: Update, context):
import random
quotes = [
"Code is poetry. — WordPress",
"Talk is cheap. Show me the code. — Linus Torvalds",
"Python is executable pseudocode. — Bruce Eckel",
]
await update.message.reply_text(random.choice(quotes))
app = Application.builder().token(TOKEN).build()
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("quote", quote))
app.run_polling()
Từ đây bạn có thể mở rộng bot với database, API bên ngoài và scheduled tasks.