nvd commited on
Commit
60a1442
·
verified ·
1 Parent(s): 50bba6b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +21 -6
main.py CHANGED
@@ -153,32 +153,47 @@ app = FastAPI(lifespan=lifespan)
153
  async def status():
154
  return {"status": "running", "connected": tg_client.is_connected()}
155
 
 
156
  @app.post("/bale-webhook")
157
  async def bale_webhook(request: Request):
158
  try:
159
  data = await request.json()
160
  if "message" in data and "text" in data["message"]:
161
- command = data["message"]["text"]
162
- now = datetime.utcnow()
 
163
 
164
- if "Today" in command:
 
 
 
 
 
 
 
 
 
 
165
  start = now.replace(hour=0, minute=0, second=0)
166
  end = now
167
- elif "Yesterday" in command:
168
  start = (now - timedelta(days=1)).replace(hour=0, minute=0, second=0)
169
  end = start + timedelta(days=1)
170
  else:
 
 
171
  return {"ok": True}
172
 
 
173
  results = messages_collection.find({"timestamp": {"$gte": start, "$lt": end}})
174
  count = 0
175
  for msg in results:
176
  send_to_bale(msg["encrypted_text"])
177
  count += 1
178
- await asyncio.sleep(0.5) # Prevents spamming Bale
179
 
180
  if count == 0:
181
- send_to_bale(encrypt_message("No messages found for this period."))
182
 
183
  except Exception as e:
184
  print(f"Webhook Error: {e}")
 
153
  async def status():
154
  return {"status": "running", "connected": tg_client.is_connected()}
155
 
156
+ @app.post("/bale-webhook")
157
  @app.post("/bale-webhook")
158
  async def bale_webhook(request: Request):
159
  try:
160
  data = await request.json()
161
  if "message" in data and "text" in data["message"]:
162
+ user_text = data["message"]["text"]
163
+ # This captures the ID of whoever is talking to the bot
164
+ sender_id = str(data["message"]["chat"]["id"])
165
 
166
+ print(f"Message received from Bale ID: {sender_id}. Text: {user_text}")
167
+
168
+ # 1. Handle /start or unknown commands by showing the menu
169
+ if user_text == "/start":
170
+ welcome_text = encrypt_message("Welcome! Monitoring is active. Use the buttons to fetch history.")
171
+ send_to_bale(welcome_text, show_keyboard=True)
172
+ return {"ok": True}
173
+
174
+ # 2. Handle History Requests
175
+ now = datetime.utcnow()
176
+ if "Today" in user_text:
177
  start = now.replace(hour=0, minute=0, second=0)
178
  end = now
179
+ elif "Yesterday" in user_text:
180
  start = (now - timedelta(days=1)).replace(hour=0, minute=0, second=0)
181
  end = start + timedelta(days=1)
182
  else:
183
+ # If they type anything else, just send the keyboard back
184
+ send_to_bale(encrypt_message("Menu updated."), show_keyboard=True)
185
  return {"ok": True}
186
 
187
+ # Query Database for history
188
  results = messages_collection.find({"timestamp": {"$gte": start, "$lt": end}})
189
  count = 0
190
  for msg in results:
191
  send_to_bale(msg["encrypted_text"])
192
  count += 1
193
+ await asyncio.sleep(0.3)
194
 
195
  if count == 0:
196
+ send_to_bale(encrypt_message("No messages found in database for this period."))
197
 
198
  except Exception as e:
199
  print(f"Webhook Error: {e}")