Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,10 +9,7 @@ import os
|
|
9 |
import re
|
10 |
import signal
|
11 |
import psutil
|
12 |
-
from fastapi import FastAPI
|
13 |
-
from fastapi.responses import RedirectResponse
|
14 |
-
from fastapi.responses import HTMLResponse
|
15 |
-
|
16 |
|
17 |
|
18 |
# Initialize bot with your token
|
@@ -454,150 +451,17 @@ def execute_command(message):
|
|
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 |
-
|
467 |
|
468 |
-
from
|
469 |
-
from fastapi.responses import HTMLResponse, StreamingResponse
|
470 |
-
import requests, os, io
|
471 |
|
472 |
app = FastAPI()
|
473 |
|
474 |
-
#
|
475 |
-
|
476 |
-
FALLBACK_IMG = "https://telegram.org/img/t_logo.png"
|
477 |
-
|
478 |
-
if not TOKEN:
|
479 |
-
raise RuntimeError("TOKEN environment variable not set")
|
480 |
-
|
481 |
-
TELEGRAM_API = f"https://api.telegram.org/bot{TOKEN}"
|
482 |
-
|
483 |
-
|
484 |
-
def get_description() -> str:
|
485 |
-
"""Try getMyShortDescription β fallback to getMyDescription β """""
|
486 |
-
try:
|
487 |
-
r = requests.get(f"{TELEGRAM_API}/getMyShortDescription").json()
|
488 |
-
if desc := r["result"].get("short_description"):
|
489 |
-
return desc
|
490 |
-
except: pass
|
491 |
-
|
492 |
-
try:
|
493 |
-
r = requests.get(f"{TELEGRAM_API}/getMyDescription").json()
|
494 |
-
if desc := r["result"].get("description"):
|
495 |
-
return desc
|
496 |
-
except: pass
|
497 |
-
|
498 |
-
return ""
|
499 |
-
|
500 |
-
|
501 |
-
# def fetch_avatar_bytes() -> bytes | None:
|
502 |
-
from typing import Optional
|
503 |
-
|
504 |
-
def fetch_avatar_bytes() -> Optional[bytes]:
|
505 |
-
"""Get latest profile photo of the bot as bytes (or None if unavailable)"""
|
506 |
-
try:
|
507 |
-
me = requests.get(f"{TELEGRAM_API}/getMe", timeout=5).json()
|
508 |
-
user_id = me["result"]["id"]
|
509 |
-
|
510 |
-
photos = requests.get(
|
511 |
-
f"{TELEGRAM_API}/getUserProfilePhotos",
|
512 |
-
params={"user_id": user_id, "limit": 1},
|
513 |
-
timeout=5
|
514 |
-
).json()
|
515 |
-
|
516 |
-
if photos["result"]["total_count"] == 0:
|
517 |
-
return None
|
518 |
-
|
519 |
-
file_id = photos["result"]["photos"][0][-1]["file_id"] # highest res
|
520 |
-
file_obj = requests.get(
|
521 |
-
f"{TELEGRAM_API}/getFile",
|
522 |
-
params={"file_id": file_id},
|
523 |
-
timeout=5
|
524 |
-
).json()
|
525 |
-
|
526 |
-
file_path = file_obj["result"]["file_path"]
|
527 |
-
file_url = f"https://api.telegram.org/file/bot{TOKEN}/{file_path}"
|
528 |
-
response = requests.get(file_url, timeout=10)
|
529 |
-
return response.content if response.status_code == 200 else None
|
530 |
-
|
531 |
-
except Exception as e:
|
532 |
-
print("Avatar fetch failed:", e)
|
533 |
-
return None
|
534 |
-
|
535 |
-
|
536 |
-
@app.get("/", include_in_schema=False)
|
537 |
-
def preview():
|
538 |
-
avatar_url = "/avatar.jpg"
|
539 |
-
description = get_description()
|
540 |
-
|
541 |
-
html = f"""
|
542 |
-
<html>
|
543 |
-
<head>
|
544 |
-
<title>@{BOT_USERNAME}</title>
|
545 |
-
<style>
|
546 |
-
body {{
|
547 |
-
font-family: sans-serif;
|
548 |
-
display: flex;
|
549 |
-
justify-content: center;
|
550 |
-
padding: 40px;
|
551 |
-
background: #f7f7f7;
|
552 |
-
}}
|
553 |
-
.card {{
|
554 |
-
max-width: 420px;
|
555 |
-
background: #fff;
|
556 |
-
padding: 24px;
|
557 |
-
text-align: center;
|
558 |
-
border-radius: 12px;
|
559 |
-
box-shadow: 0 4px 12px rgba(0,0,0,.1);
|
560 |
-
}}
|
561 |
-
.avatar {{
|
562 |
-
width: 120px;
|
563 |
-
height: 120px;
|
564 |
-
border-radius: 50%;
|
565 |
-
object-fit: cover;
|
566 |
-
background: #ddd;
|
567 |
-
}}
|
568 |
-
.btn {{
|
569 |
-
display: inline-block;
|
570 |
-
margin-top: 16px;
|
571 |
-
padding: 12px 24px;
|
572 |
-
background: #2AABEE;
|
573 |
-
color: #fff;
|
574 |
-
border-radius: 8px;
|
575 |
-
text-decoration: none;
|
576 |
-
font-weight: bold;
|
577 |
-
}}
|
578 |
-
</style>
|
579 |
-
</head>
|
580 |
-
<body>
|
581 |
-
<div class="card">
|
582 |
-
<img src="{avatar_url}" alt="avatar" class="avatar">
|
583 |
-
<h2>@{BOT_USERNAME}</h2>
|
584 |
-
<p>{description}</p>
|
585 |
-
<a class="btn" href="https://t.me/{BOT_USERNAME}" target="_blank">StartΒ Bot</a>
|
586 |
-
</div>
|
587 |
-
</body>
|
588 |
-
</html>
|
589 |
-
"""
|
590 |
-
return HTMLResponse(html)
|
591 |
-
|
592 |
|
593 |
-
@app.get("/
|
594 |
-
def
|
595 |
-
|
596 |
-
if img_bytes:
|
597 |
-
return StreamingResponse(io.BytesIO(img_bytes), media_type="image/jpeg")
|
598 |
-
# fallback
|
599 |
-
fallback = requests.get(FALLBACK_IMG)
|
600 |
-
return Response(content=fallback.content, media_type="image/png")
|
601 |
|
602 |
|
603 |
@app.on_event("startup")
|
|
|
9 |
import re
|
10 |
import signal
|
11 |
import psutil
|
12 |
+
from fastapi import FastAPI
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
# Initialize bot with your token
|
|
|
451 |
|
452 |
|
453 |
# βββββββββββββββββββββββββββββββββββββββββββββββββββ FastAPI βββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
454 |
|
455 |
+
from telegram_preview import router as telegram_preview_router
|
|
|
|
|
456 |
|
457 |
app = FastAPI()
|
458 |
|
459 |
+
# Mount the preview under /bot (change prefix if you like)
|
460 |
+
app.include_router(telegram_preview_router, prefix="/bot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
461 |
|
462 |
+
@app.get("/")
|
463 |
+
def root():
|
464 |
+
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
465 |
|
466 |
|
467 |
@app.on_event("startup")
|