Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -454,34 +454,64 @@ def execute_command(message):
|
|
454 |
|
455 |
|
456 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββ FastAPI βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
457 |
app = FastAPI()
|
458 |
|
459 |
-
@app.get("/")
|
460 |
-
def root():
|
461 |
-
|
462 |
-
|
463 |
-
|
464 |
-
|
465 |
-
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
|
|
|
|
485 |
|
486 |
@app.on_event("startup")
|
487 |
def startup():
|
|
|
454 |
|
455 |
|
456 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββ FastAPI βββ
|
457 |
+
# app = FastAPI()
|
458 |
+
|
459 |
+
# @app.get("/")
|
460 |
+
# def root(): # β‘ healthβcheck hits this β must return 200 quickly
|
461 |
+
# # return {"status": "ok"}
|
462 |
+
# return RedirectResponse(
|
463 |
+
# url="https://t.me/python3463_bot",
|
464 |
+
# status_code=status.HTTP_302_FOUND # 302 is fine too
|
465 |
+
# )
|
466 |
+
CACHE_TTL = 60 * 60 # 1Β hour
|
467 |
+
CACHE = {}
|
468 |
+
|
469 |
+
BOT_USERNAME = "python3463_bot"
|
470 |
+
TME_URL = f"https://t.me/{BOT_USERNAME}"
|
471 |
+
|
472 |
+
def get_avatar_url() -> str | None:
|
473 |
+
now = time.time()
|
474 |
+
if "photo" in CACHE and now - CACHE["ts"] < CACHE_TTL:
|
475 |
+
return CACHE["photo"]
|
476 |
+
|
477 |
+
html = requests.get(TME_URL, timeout=5).text
|
478 |
+
m = re.search(r'<meta property="og:image" content="([^"]+)"', html)
|
479 |
+
if m:
|
480 |
+
CACHE["photo"] = m.group(1)
|
481 |
+
CACHE["ts"] = now
|
482 |
+
return CACHE["photo"]
|
483 |
+
return None
|
484 |
+
|
485 |
app = FastAPI()
|
486 |
|
487 |
+
@app.get("/", include_in_schema=False)
|
488 |
+
def root():
|
489 |
+
photo = get_avatar_url() or "/static/telegram.png"
|
490 |
+
html = f"""
|
491 |
+
<html>
|
492 |
+
<head>
|
493 |
+
<title>Pythonβ―Bot</title>
|
494 |
+
<style>
|
495 |
+
body{{font-family:sans-serif;display:flex;justify-content:center;}}
|
496 |
+
.card{{max-width:420px;padding:24px;border-radius:12px;
|
497 |
+
box-shadow:0 4px 12px rgba(0,0,0,.1);text-align:center;}}
|
498 |
+
img{{border-radius:50%;width:120px;height:120px;object-fit:cover;}}
|
499 |
+
a.btn{{background:#2AABEE;color:#fff;padding:12px 24px;border-radius:8px;
|
500 |
+
text-decoration:none;font-weight:bold;display:inline-block;margin-top:12px;}}
|
501 |
+
</style>
|
502 |
+
</head>
|
503 |
+
<body>
|
504 |
+
<div class="card">
|
505 |
+
<img src="{photo}" alt="Bot Avatar">
|
506 |
+
<h2>PythonΒ Bot</h2>
|
507 |
+
<p><strong>@{BOT_USERNAME}</strong></p>
|
508 |
+
<p>The fastest way to practise PythonΒ 3.</p>
|
509 |
+
<a class="btn" href="https://t.me/{BOT_USERNAME}" target="_blank">StartΒ Bot</a>
|
510 |
+
</div>
|
511 |
+
</body>
|
512 |
+
</html>
|
513 |
+
"""
|
514 |
+
return HTMLResponse(html)
|
515 |
|
516 |
@app.on_event("startup")
|
517 |
def startup():
|