Spaces:
Paused
Paused
Commit ·
3b5901f
1
Parent(s): 8f16c3d
fix: integrate telegram bot into fastapi lifecycle and improve logging
Browse files
Dockerfile
CHANGED
|
@@ -24,5 +24,5 @@ RUN mkdir -p /app/uploads && chmod 777 /app/uploads
|
|
| 24 |
# Expose the port the app runs on
|
| 25 |
EXPOSE 7860
|
| 26 |
|
| 27 |
-
# Run
|
| 28 |
-
CMD uvicorn app:app --host 0.0.0.0 --port 7860
|
|
|
|
| 24 |
# Expose the port the app runs on
|
| 25 |
EXPOSE 7860
|
| 26 |
|
| 27 |
+
# Run FastAPI app (which also starts the bot)
|
| 28 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -3,28 +3,44 @@ import shutil
|
|
| 3 |
import time
|
| 4 |
import uuid
|
| 5 |
import threading
|
|
|
|
| 6 |
from typing import List, Optional
|
| 7 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Header, Request
|
| 8 |
from fastapi.responses import FileResponse
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
| 10 |
from config import UPLOAD_DIR, BASE_URL
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI(title="NexusShare Web API")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
def
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
if os.path.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
|
| 29 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 30 |
|
|
|
|
| 3 |
import time
|
| 4 |
import uuid
|
| 5 |
import threading
|
| 6 |
+
import asyncio
|
| 7 |
from typing import List, Optional
|
| 8 |
from fastapi import FastAPI, UploadFile, File, HTTPException, Header, Request
|
| 9 |
from fastapi.responses import FileResponse
|
| 10 |
from fastapi.staticfiles import StaticFiles
|
| 11 |
from config import UPLOAD_DIR, BASE_URL
|
| 12 |
+
from bot import bot as telegram_bot
|
| 13 |
|
| 14 |
app = FastAPI(title="NexusShare Web API")
|
| 15 |
|
| 16 |
+
@app.on_event("startup")
|
| 17 |
+
async def startup_event():
|
| 18 |
+
# Start Cleanup Task
|
| 19 |
+
def cleanup_old_files():
|
| 20 |
+
while True:
|
| 21 |
+
now = time.time()
|
| 22 |
+
if os.path.exists(UPLOAD_DIR):
|
| 23 |
+
for filename in os.listdir(UPLOAD_DIR):
|
| 24 |
+
file_path = os.path.join(UPLOAD_DIR, filename)
|
| 25 |
+
if os.path.isfile(file_path):
|
| 26 |
+
if now - os.path.getmtime(file_path) > 86400: # 24h
|
| 27 |
+
try:
|
| 28 |
+
os.remove(file_path)
|
| 29 |
+
except: pass
|
| 30 |
+
time.sleep(3600)
|
| 31 |
+
|
| 32 |
+
threading.Thread(target=cleanup_old_files, daemon=True).start()
|
| 33 |
+
|
| 34 |
+
# Start Telegram Bot
|
| 35 |
+
try:
|
| 36 |
+
await telegram_bot.start()
|
| 37 |
+
print("🚀 Telegram Bot started successfully!")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"❌ Failed to start Telegram Bot: {e}")
|
| 40 |
|
| 41 |
+
@app.on_event("shutdown")
|
| 42 |
+
async def shutdown_event():
|
| 43 |
+
await telegram_bot.stop()
|
| 44 |
|
| 45 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 46 |
|
bot.py
CHANGED
|
@@ -1,16 +1,27 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import uuid
|
|
|
|
|
|
|
| 4 |
from pyrogram import Client, filters, idle
|
| 5 |
from pyrogram.types import Message
|
| 6 |
-
import pyromod
|
| 7 |
-
import asyncio
|
| 8 |
from config import API_ID, API_HASH, BOT_TOKEN, UPLOAD_DIR, BASE_URL
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if not all([API_ID, API_HASH, BOT_TOKEN]):
|
| 11 |
-
print("Bot configuration
|
| 12 |
exit(1)
|
| 13 |
|
|
|
|
|
|
|
| 14 |
bot = Client(
|
| 15 |
"nexus_bot",
|
| 16 |
api_id=int(API_ID),
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import uuid
|
| 4 |
+
import asyncio
|
| 5 |
+
import pyromod
|
| 6 |
from pyrogram import Client, filters, idle
|
| 7 |
from pyrogram.types import Message
|
|
|
|
|
|
|
| 8 |
from config import API_ID, API_HASH, BOT_TOKEN, UPLOAD_DIR, BASE_URL
|
| 9 |
|
| 10 |
+
print("--- Starting Bot Initialization ---")
|
| 11 |
+
|
| 12 |
+
if not API_ID:
|
| 13 |
+
print("ERROR: API_ID is missing!")
|
| 14 |
+
if not API_HASH:
|
| 15 |
+
print("ERROR: API_HASH is missing!")
|
| 16 |
+
if not BOT_TOKEN:
|
| 17 |
+
print("ERROR: BOT_TOKEN/TELEGRAM_TOKEN is missing!")
|
| 18 |
+
|
| 19 |
if not all([API_ID, API_HASH, BOT_TOKEN]):
|
| 20 |
+
print("Bot configuration incomplete. Exiting...")
|
| 21 |
exit(1)
|
| 22 |
|
| 23 |
+
print(f"Config loaded: API_ID={API_ID}, API_HASH={API_HASH[:4]}..., BOT_TOKEN={BOT_TOKEN[:4]}...")
|
| 24 |
+
|
| 25 |
bot = Client(
|
| 26 |
"nexus_bot",
|
| 27 |
api_id=int(API_ID),
|
config.py
CHANGED
|
@@ -2,7 +2,9 @@ import os
|
|
| 2 |
|
| 3 |
API_ID = os.getenv("API_ID")
|
| 4 |
API_HASH = os.getenv("API_HASH")
|
| 5 |
-
BOT_TOKEN = os.getenv("TELEGRAM_TOKEN")
|
| 6 |
-
BASE_URL = "https://ttnikal-filesharing.hf.space"
|
| 7 |
UPLOAD_DIR = "uploads"
|
| 8 |
-
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
API_ID = os.getenv("API_ID")
|
| 4 |
API_HASH = os.getenv("API_HASH")
|
| 5 |
+
BOT_TOKEN = os.getenv("BOT_TOKEN") or os.getenv("TELEGRAM_TOKEN")
|
| 6 |
+
BASE_URL = os.getenv("BASE_URL", "https://ttnikal-filesharing.hf.space")
|
| 7 |
UPLOAD_DIR = "uploads"
|
| 8 |
+
|
| 9 |
+
if not os.path.exists(UPLOAD_DIR):
|
| 10 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|