Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import math
|
| 3 |
import logging
|
| 4 |
-
from contextlib import asynccontextmanager
|
| 5 |
from pyrogram import Client
|
| 6 |
from pyrogram.session import Session, Auth
|
| 7 |
from pyrogram import raw
|
|
@@ -16,18 +16,18 @@ API_HASH = os.environ.get("API_HASH")
|
|
| 16 |
BOT_TOKEN = os.environ.get("BOT_TOKEN")
|
| 17 |
STORAGE_CHANNEL = int(os.environ.get("STORAGE_CHANNEL"))
|
| 18 |
|
| 19 |
-
# --- BOT CLIENT ---
|
|
|
|
| 20 |
client = Client(
|
| 21 |
"worker_bot",
|
| 22 |
api_id=API_ID,
|
| 23 |
api_hash=API_HASH,
|
| 24 |
bot_token=BOT_TOKEN,
|
| 25 |
in_memory=True,
|
| 26 |
-
no_updates=True
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
-
# --- π NEW LIFESPAN LOGIC ---
|
| 30 |
-
# This replaces @app.on_event("startup")
|
| 31 |
@asynccontextmanager
|
| 32 |
async def lifespan(app: FastAPI):
|
| 33 |
print("β³ Connecting to Telegram...")
|
|
@@ -37,7 +37,7 @@ async def lifespan(app: FastAPI):
|
|
| 37 |
except Exception as e:
|
| 38 |
print(f"β Failed to connect: {e}")
|
| 39 |
|
| 40 |
-
yield
|
| 41 |
|
| 42 |
print("π Stopping Worker...")
|
| 43 |
try:
|
|
@@ -45,10 +45,9 @@ async def lifespan(app: FastAPI):
|
|
| 45 |
except:
|
| 46 |
pass
|
| 47 |
|
| 48 |
-
# Initialize FastAPI with the lifespan handler
|
| 49 |
app = FastAPI(lifespan=lifespan)
|
| 50 |
|
| 51 |
-
# --- STREAMING LOGIC
|
| 52 |
class ByteStreamer:
|
| 53 |
def __init__(self, client: Client):
|
| 54 |
self.client = client
|
|
@@ -146,3 +145,4 @@ async def stream_handler(req: Request, message_id: int, filename: str):
|
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import math
|
| 3 |
import logging
|
| 4 |
+
from contextlib import asynccontextmanager
|
| 5 |
from pyrogram import Client
|
| 6 |
from pyrogram.session import Session, Auth
|
| 7 |
from pyrogram import raw
|
|
|
|
| 16 |
BOT_TOKEN = os.environ.get("BOT_TOKEN")
|
| 17 |
STORAGE_CHANNEL = int(os.environ.get("STORAGE_CHANNEL"))
|
| 18 |
|
| 19 |
+
# --- BOT CLIENT (FIXED) ---
|
| 20 |
+
# Added 'ipv6=False' to force IPv4 connection (Fixes Timeout)
|
| 21 |
client = Client(
|
| 22 |
"worker_bot",
|
| 23 |
api_id=API_ID,
|
| 24 |
api_hash=API_HASH,
|
| 25 |
bot_token=BOT_TOKEN,
|
| 26 |
in_memory=True,
|
| 27 |
+
no_updates=True,
|
| 28 |
+
ipv6=False # <--- THIS IS THE FIX
|
| 29 |
)
|
| 30 |
|
|
|
|
|
|
|
| 31 |
@asynccontextmanager
|
| 32 |
async def lifespan(app: FastAPI):
|
| 33 |
print("β³ Connecting to Telegram...")
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
print(f"β Failed to connect: {e}")
|
| 39 |
|
| 40 |
+
yield
|
| 41 |
|
| 42 |
print("π Stopping Worker...")
|
| 43 |
try:
|
|
|
|
| 45 |
except:
|
| 46 |
pass
|
| 47 |
|
|
|
|
| 48 |
app = FastAPI(lifespan=lifespan)
|
| 49 |
|
| 50 |
+
# --- STREAMING LOGIC ---
|
| 51 |
class ByteStreamer:
|
| 52 |
def __init__(self, client: Client):
|
| 53 |
self.client = client
|
|
|
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 148 |
+
|