| from fastapi import FastAPI, HTTPException |
| from fastapi.middleware.cors import CORSMiddleware |
| import os |
| import json |
| import httpx |
| import subprocess |
| |
| |
| |
| import aiohttp |
| |
|
|
| app = FastAPI() |
|
|
| |
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_credentials=True, |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
| |
| @app.on_event("startup") |
| async def startup_event(): |
| global codigos, TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID |
| codigos_json = os.environ.get("codigos") |
| print("codigos recogidos pero no chequeados") |
| if codigos_json: |
| codigos = json.loads(codigos_json) |
| print("Códigos cargados correctamente") |
| else: |
| codigos = {} |
| print("No se encontraron códigos en las variables de entorno") |
| |
| TELEGRAM_BOT_TOKEN = os.environ.get("T_bot") |
| TELEGRAM_CHAT_ID = os.environ.get("nroChat") |
|
|
| if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID: |
| print("Faltan configuraciones de Telegram") |
| else: |
| print("Claves de telegram cargados correctamente") |
|
|
| async def send_telegram_message(message): |
| |
| subprocess.run(["python", "/code/envia_mensaje.py", message]) |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| @app.get("/verificar/{codigo}") |
| async def verificar_codigo(codigo: str): |
| if codigo in codigos: |
| if codigos[codigo] > 0: |
|
|
| |
| mensaje = f"Código utilizado: {codigo}\nConsultas restantes: {codigos[codigo]}" |
| print(f"\nmensaje:") |
| print(mensaje, end="\n\n") |
| |
| |
|
|
| |
| print("averrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr") |
| print(os.getcwd()) |
| print(os.listdir()) |
| print("averrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr") |
|
|
| |
| result = subprocess.run(["ls"], capture_output=True, text=True) |
| result = subprocess.run(["./envia_mensaje.sh", mensaje], capture_output=True, text=True) |
| |
| print("STDOUT:", result.stdout) |
| print("STDERR:", result.stderr) |
| if result.returncode != 0: |
| print(f"Ocurrió un Error: {result.returncode}") |
| |
| return {"mensaje": "código encontrado", "consultas_restantes": codigos[codigo]} |
| else: |
| return {"mensaje": "código caducado"} |
| else: |
| return {"mensaje": "código no encontrado"} |
|
|
| if __name__ == "__main__": |
| import uvicorn |
| uvicorn.run(app, host="0.0.0.0", port=7860) |
|
|