subo app y requeriments again
Browse files- app.py +74 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import httpx
|
6 |
+
#import subprocess
|
7 |
+
#import asyncio #alternativa a
|
8 |
+
#from waitress import serve #alternativa a
|
9 |
+
#import socket #para funcionalidad ipv6
|
10 |
+
import aiohttp
|
11 |
+
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# Configurar CORS
|
15 |
+
app.add_middleware(
|
16 |
+
CORSMiddleware,
|
17 |
+
allow_origins=["*"],
|
18 |
+
allow_credentials=True,
|
19 |
+
allow_methods=["*"],
|
20 |
+
allow_headers=["*"],
|
21 |
+
)
|
22 |
+
|
23 |
+
# Cargar los c贸digos y configuraci贸n de Telegram desde las variables de entorno
|
24 |
+
@app.on_event("startup")
|
25 |
+
async def startup_event():
|
26 |
+
global codigos, TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID
|
27 |
+
codigos_json = os.environ.get("codigos")
|
28 |
+
print("codigos recogidos pero no chequeados")
|
29 |
+
if codigos_json:
|
30 |
+
codigos = json.loads(codigos_json)
|
31 |
+
print("C贸digos cargados correctamente")
|
32 |
+
else:
|
33 |
+
codigos = {}
|
34 |
+
print("No se encontraron c贸digos en las variables de entorno")
|
35 |
+
|
36 |
+
TELEGRAM_BOT_TOKEN = os.environ.get("T_bot")
|
37 |
+
TELEGRAM_CHAT_ID = os.environ.get("nroChat")
|
38 |
+
|
39 |
+
if not TELEGRAM_BOT_TOKEN or not TELEGRAM_CHAT_ID:
|
40 |
+
print("Faltan configuraciones de Telegram")
|
41 |
+
else:
|
42 |
+
print("Claves de telegram cargados correctamente")
|
43 |
+
|
44 |
+
async def send_telegram_message(message):
|
45 |
+
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
|
46 |
+
params = {
|
47 |
+
"chat_id": TELEGRAM_CHAT_ID,
|
48 |
+
"text": message
|
49 |
+
}
|
50 |
+
return
|
51 |
+
async with httpx.AsyncClient() as client:
|
52 |
+
await client.post(url, params=params)
|
53 |
+
|
54 |
+
@app.get("/verificar/{codigo}")
|
55 |
+
async def verificar_codigo(codigo: str):
|
56 |
+
if codigo in codigos:
|
57 |
+
if codigos[codigo] > 0:
|
58 |
+
|
59 |
+
# Enviar mensaje a Telegram
|
60 |
+
mensaje = f"C贸digo utilizado: {codigo}\nConsultas restantes: {codigos[codigo]}"
|
61 |
+
print(f"\nmensaje:")
|
62 |
+
print(mensaje, end="\n\n")
|
63 |
+
await send_telegram_message(mensaje)
|
64 |
+
|
65 |
+
return {"mensaje": "c贸digo encontrado", "consultas_restantes": codigos[codigo]}
|
66 |
+
else:
|
67 |
+
return {"mensaje": "c贸digo caducado"}
|
68 |
+
else:
|
69 |
+
return {"mensaje": "c贸digo no encontrado"}
|
70 |
+
|
71 |
+
if __name__ == "__main__":
|
72 |
+
app.run(host='0.0.0.0', port=7860)
|
73 |
+
#import uvicorn
|
74 |
+
#uvicorn.run(app, host="0.0.0.0", port=7860, reload=False)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.68.0
|
2 |
+
uvicorn==0.17.0
|
3 |
+
httpx==0.27.0
|
4 |
+
aiohttp
|