nvd commited on
Commit
5af969c
·
verified ·
1 Parent(s): f3ce9e6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -11
main.py CHANGED
@@ -101,24 +101,40 @@ async def handler(event):
101
 
102
  @asynccontextmanager
103
  async def lifespan(app: FastAPI):
104
- # Set Auto-Delete for MongoDB (7 days)
105
- await messages_collection.create_index("timestamp", expireAfterSeconds=604800)
106
-
107
- # Start Telegram
108
- await tg_client.start()
109
- asyncio.create_task(tg_client.run_until_disconnected())
110
-
111
- # Start Rubika Bot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  if rub_bot:
113
- print("🚀 Rubika Bot Client Initialized")
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}