Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import logging
|
|
| 4 |
from contextlib import asynccontextmanager
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
-
import
|
| 8 |
from fastapi import FastAPI, Request, Response
|
| 9 |
import uvicorn
|
| 10 |
from pyrogram import Client, filters
|
|
@@ -45,32 +45,38 @@ app = Client(
|
|
| 45 |
sleep_threshold=0,
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
@asynccontextmanager
|
| 50 |
async def lifespan(fastapi_app: FastAPI):
|
| 51 |
-
#
|
| 52 |
await app.start()
|
| 53 |
logger.info("Pyrogram client started")
|
| 54 |
|
| 55 |
-
#
|
| 56 |
webhook_url = f"{WEBHOOK_URL}{WEBHOOK_PATH}"
|
| 57 |
-
async with
|
| 58 |
-
|
| 59 |
f"https://api.telegram.org/bot{TOKEN}/setWebhook",
|
| 60 |
params={"url": webhook_url},
|
| 61 |
-
)
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
yield
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
async with
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
await app.stop()
|
| 71 |
|
|
|
|
| 72 |
fastapi_app = FastAPI(lifespan=lifespan)
|
| 73 |
|
|
|
|
| 74 |
# ---------- BOT HANDLERS ----------
|
| 75 |
@app.on_message(filters.command("start"))
|
| 76 |
async def start_command(client: Client, message: Message):
|
|
@@ -79,6 +85,7 @@ async def start_command(client: Client, message: Message):
|
|
| 79 |
"Use /files to see your uploaded files and download them again."
|
| 80 |
)
|
| 81 |
|
|
|
|
| 82 |
@app.on_message(filters.document | filters.photo)
|
| 83 |
async def handle_file(client: Client, message: Message):
|
| 84 |
user_id = message.from_user.id
|
|
@@ -105,6 +112,7 @@ async def handle_file(client: Client, message: Message):
|
|
| 105 |
f"✅ File saved as `{original_name}`\nUse /files to list your uploads."
|
| 106 |
)
|
| 107 |
|
|
|
|
| 108 |
@app.on_message(filters.command("files"))
|
| 109 |
async def list_files(client: Client, message: Message):
|
| 110 |
user_id = message.from_user.id
|
|
@@ -131,6 +139,7 @@ async def list_files(client: Client, message: Message):
|
|
| 131 |
reply_markup=InlineKeyboardMarkup(keyboard),
|
| 132 |
)
|
| 133 |
|
|
|
|
| 134 |
@app.on_callback_query()
|
| 135 |
async def download_file_callback(client: Client, callback_query: CallbackQuery):
|
| 136 |
user_id = callback_query.from_user.id
|
|
@@ -168,6 +177,7 @@ async def download_file_callback(client: Client, callback_query: CallbackQuery):
|
|
| 168 |
|
| 169 |
await callback_query.edit_message_text(f"📥 Sent: {real_path.name}")
|
| 170 |
|
|
|
|
| 171 |
# ---------- WEBHOOK ENDPOINT ----------
|
| 172 |
@fastapi_app.post(WEBHOOK_PATH)
|
| 173 |
async def telegram_webhook(request: Request):
|
|
@@ -179,10 +189,12 @@ async def telegram_webhook(request: Request):
|
|
| 179 |
logger.error(f"Error processing update: {e}")
|
| 180 |
return Response(status_code=200)
|
| 181 |
|
|
|
|
| 182 |
@fastapi_app.get("/health")
|
| 183 |
async def health():
|
| 184 |
return {"status": "ok"}
|
| 185 |
|
|
|
|
| 186 |
# ---------- MAIN ----------
|
| 187 |
if __name__ == "__main__":
|
| 188 |
uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)
|
|
|
|
| 4 |
from contextlib import asynccontextmanager
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
+
import aiohttp
|
| 8 |
from fastapi import FastAPI, Request, Response
|
| 9 |
import uvicorn
|
| 10 |
from pyrogram import Client, filters
|
|
|
|
| 45 |
sleep_threshold=0,
|
| 46 |
)
|
| 47 |
|
| 48 |
+
|
| 49 |
+
# ---------- LIFESPAN (manual webhook via aiohttp) ----------
|
| 50 |
@asynccontextmanager
|
| 51 |
async def lifespan(fastapi_app: FastAPI):
|
| 52 |
+
# Start Pyrogram client (required for dispatcher)
|
| 53 |
await app.start()
|
| 54 |
logger.info("Pyrogram client started")
|
| 55 |
|
| 56 |
+
# Set webhook using Telegram Bot API directly
|
| 57 |
webhook_url = f"{WEBHOOK_URL}{WEBHOOK_PATH}"
|
| 58 |
+
async with aiohttp.ClientSession() as session:
|
| 59 |
+
async with session.get(
|
| 60 |
f"https://api.telegram.org/bot{TOKEN}/setWebhook",
|
| 61 |
params={"url": webhook_url},
|
| 62 |
+
) as resp:
|
| 63 |
+
data = await resp.json()
|
| 64 |
+
logger.info(f"Webhook set: {data}")
|
| 65 |
|
| 66 |
yield
|
| 67 |
|
| 68 |
+
# Clean shutdown
|
| 69 |
+
async with aiohttp.ClientSession() as session:
|
| 70 |
+
async with session.get(
|
| 71 |
+
f"https://api.telegram.org/bot{TOKEN}/deleteWebhook"
|
| 72 |
+
) as resp:
|
| 73 |
+
logger.info("Webhook deleted")
|
| 74 |
await app.stop()
|
| 75 |
|
| 76 |
+
|
| 77 |
fastapi_app = FastAPI(lifespan=lifespan)
|
| 78 |
|
| 79 |
+
|
| 80 |
# ---------- BOT HANDLERS ----------
|
| 81 |
@app.on_message(filters.command("start"))
|
| 82 |
async def start_command(client: Client, message: Message):
|
|
|
|
| 85 |
"Use /files to see your uploaded files and download them again."
|
| 86 |
)
|
| 87 |
|
| 88 |
+
|
| 89 |
@app.on_message(filters.document | filters.photo)
|
| 90 |
async def handle_file(client: Client, message: Message):
|
| 91 |
user_id = message.from_user.id
|
|
|
|
| 112 |
f"✅ File saved as `{original_name}`\nUse /files to list your uploads."
|
| 113 |
)
|
| 114 |
|
| 115 |
+
|
| 116 |
@app.on_message(filters.command("files"))
|
| 117 |
async def list_files(client: Client, message: Message):
|
| 118 |
user_id = message.from_user.id
|
|
|
|
| 139 |
reply_markup=InlineKeyboardMarkup(keyboard),
|
| 140 |
)
|
| 141 |
|
| 142 |
+
|
| 143 |
@app.on_callback_query()
|
| 144 |
async def download_file_callback(client: Client, callback_query: CallbackQuery):
|
| 145 |
user_id = callback_query.from_user.id
|
|
|
|
| 177 |
|
| 178 |
await callback_query.edit_message_text(f"📥 Sent: {real_path.name}")
|
| 179 |
|
| 180 |
+
|
| 181 |
# ---------- WEBHOOK ENDPOINT ----------
|
| 182 |
@fastapi_app.post(WEBHOOK_PATH)
|
| 183 |
async def telegram_webhook(request: Request):
|
|
|
|
| 189 |
logger.error(f"Error processing update: {e}")
|
| 190 |
return Response(status_code=200)
|
| 191 |
|
| 192 |
+
|
| 193 |
@fastapi_app.get("/health")
|
| 194 |
async def health():
|
| 195 |
return {"status": "ok"}
|
| 196 |
|
| 197 |
+
|
| 198 |
# ---------- MAIN ----------
|
| 199 |
if __name__ == "__main__":
|
| 200 |
uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)
|