git iniciado
This commit is contained in:
+114
@@ -0,0 +1,114 @@
|
||||
from fastapi import FastAPI, Request
|
||||
from telegram import Update
|
||||
from telegram.ext import (
|
||||
ApplicationBuilder, CommandHandler, MessageHandler, ContextTypes, filters
|
||||
)
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
# ====================================================
|
||||
# 🔐 TOKEN DE TELEGRAM
|
||||
# ====================================================
|
||||
TOKEN = "8280672304:AAHaHAaxNtBNothIRSFmDzzq_TLDWRpV8bU"
|
||||
|
||||
# ====================================================
|
||||
# 🧩 CONFIGURACIÓN DE MODO
|
||||
# Usa "polling" para desarrollo o "webhook" para producción
|
||||
# ====================================================
|
||||
USE_WEBHOOK = False # Cambia a True cuando despliegues en servidor
|
||||
WEBHOOK_URL = "https://tu-dominio.com/webhook" # tu dominio con HTTPS
|
||||
|
||||
# ====================================================
|
||||
# 🤖 HANDLERS DE COMANDOS
|
||||
# ====================================================
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""Comando /start"""
|
||||
await update.message.reply_text("¡Hola! Soy tu bot 🤖\nUsa /help para ver mis comandos.")
|
||||
|
||||
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""Comando /help"""
|
||||
help_text = (
|
||||
"📜 Comandos disponibles:\n"
|
||||
"/start - Inicia el bot\n"
|
||||
"/help - Muestra esta ayuda\n"
|
||||
"/info - Información sobre el bot\n"
|
||||
"/echo <texto> - Te repito lo que escribas"
|
||||
)
|
||||
await update.message.reply_text(help_text)
|
||||
|
||||
async def info_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""Comando /info"""
|
||||
await update.message.reply_text("Soy un bot creado con Python y FastAPI 🚀")
|
||||
|
||||
async def echo_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""Comando /echo"""
|
||||
text = " ".join(context.args)
|
||||
if not text:
|
||||
await update.message.reply_text("Por favor, escribe algo luego de /echo 🗣️")
|
||||
else:
|
||||
await update.message.reply_text(f"Echo: {text}")
|
||||
|
||||
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""Respuesta automática a cualquier texto"""
|
||||
text = update.message.text
|
||||
await update.message.reply_text(f"Recibí tu mensaje: {text}")
|
||||
|
||||
# ====================================================
|
||||
# ⚙️ CONFIGURACIÓN DEL BOT
|
||||
# ====================================================
|
||||
|
||||
def build_app():
|
||||
app = ApplicationBuilder().token(TOKEN).build()
|
||||
|
||||
# Comandos
|
||||
app.add_handler(CommandHandler("start", start))
|
||||
app.add_handler(CommandHandler("help", help_command))
|
||||
app.add_handler(CommandHandler("info", info_command))
|
||||
app.add_handler(CommandHandler("echo", echo_command))
|
||||
|
||||
# Mensajes de texto genéricos
|
||||
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
|
||||
|
||||
return app
|
||||
|
||||
# ====================================================
|
||||
# 🧩 MODO POLLING (DESARROLLO)
|
||||
# ====================================================
|
||||
|
||||
def run_polling():
|
||||
app = build_app()
|
||||
print("🤖 Bot ejecutándose con polling...")
|
||||
app.run_polling()
|
||||
|
||||
# ====================================================
|
||||
# 🌐 MODO WEBHOOK (PRODUCCIÓN)
|
||||
# ====================================================
|
||||
|
||||
fastapi_app = FastAPI()
|
||||
telegram_app = build_app()
|
||||
|
||||
@fastapi_app.post("/webhook")
|
||||
async def webhook(request: Request):
|
||||
"""Recibe mensajes directamente desde Telegram vía Webhook"""
|
||||
data = await request.json()
|
||||
update = Update.de_json(data, telegram_app.bot)
|
||||
await telegram_app.process_update(update)
|
||||
return {"ok": True}
|
||||
|
||||
async def setup_webhook():
|
||||
"""Configura el webhook con Telegram"""
|
||||
await telegram_app.bot.set_webhook(WEBHOOK_URL)
|
||||
print(f"Webhook configurado en: {WEBHOOK_URL}")
|
||||
|
||||
# ====================================================
|
||||
# 🏁 MAIN
|
||||
# ====================================================
|
||||
|
||||
if __name__ == "__main__":
|
||||
if USE_WEBHOOK:
|
||||
import uvicorn
|
||||
asyncio.run(setup_webhook())
|
||||
uvicorn.run(fastapi_app, host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))
|
||||
else:
|
||||
run_polling()
|
||||
Reference in New Issue
Block a user