nvd commited on
Update main.py
Browse files
main.py
CHANGED
|
@@ -101,24 +101,40 @@ async def handler(event):
|
|
| 101 |
|
| 102 |
@asynccontextmanager
|
| 103 |
async def lifespan(app: FastAPI):
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# Start
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
if rub_bot:
|
| 113 |
-
print("🚀 Rubika Bot
|
| 114 |
|
| 115 |
yield
|
|
|
|
| 116 |
await tg_client.disconnect()
|
| 117 |
if rub_bot:
|
| 118 |
await rub_bot.disconnect()
|
| 119 |
|
| 120 |
-
app = FastAPI(lifespan=lifespan)
|
| 121 |
-
|
| 122 |
@app.get("/")
|
| 123 |
async def health():
|
| 124 |
return {"status": "online", "monitoring": TARGET_CHANNELS}
|
|
|
|
| 101 |
|
| 102 |
@asynccontextmanager
|
| 103 |
async def lifespan(app: FastAPI):
|
| 104 |
+
# 1. Setup MongoDB TTL (7 days)
|
| 105 |
+
try:
|
| 106 |
+
await messages_collection.create_index("timestamp", expireAfterSeconds=604800)
|
| 107 |
+
print("✅ MongoDB TTL Index active")
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(f"⚠️ Mongo Index Error: {e}")
|
| 110 |
+
|
| 111 |
+
# 2. Start Telegram (Server-Safe Mode)
|
| 112 |
+
try:
|
| 113 |
+
print("Connecting to Telegram...")
|
| 114 |
+
await tg_client.connect() # Use connect(), NOT start()
|
| 115 |
+
|
| 116 |
+
if not await tg_client.is_user_authorized():
|
| 117 |
+
print("❌ ERROR: Telegram is NOT authorized.")
|
| 118 |
+
print("Your TG_SESSION_STRING is either missing, invalid, or expired.")
|
| 119 |
+
print("The app will stay online but Telegram messages won't be captured.")
|
| 120 |
+
else:
|
| 121 |
+
print("✅ Telegram Authorized & Connected!")
|
| 122 |
+
# Run Telegram listener in the background
|
| 123 |
+
asyncio.create_task(tg_client.run_until_disconnected())
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
print(f"❌ Telegram Connection Error: {e}")
|
| 127 |
+
|
| 128 |
+
# 3. Rubika Bot Initialization
|
| 129 |
if rub_bot:
|
| 130 |
+
print("🚀 Rubika Bot Initialized")
|
| 131 |
|
| 132 |
yield
|
| 133 |
+
# Shutdown
|
| 134 |
await tg_client.disconnect()
|
| 135 |
if rub_bot:
|
| 136 |
await rub_bot.disconnect()
|
| 137 |
|
|
|
|
|
|
|
| 138 |
@app.get("/")
|
| 139 |
async def health():
|
| 140 |
return {"status": "online", "monitoring": TARGET_CHANNELS}
|