Chandima Prabhath commited on
Commit
94856b9
Β·
1 Parent(s): 8ac1fbd

Refactor startup message handling and improve trivia JSON parsing; remove startup message function and add JSON extraction utility.

Browse files
Files changed (1) hide show
  1. app.py +10 -14
app.py CHANGED
@@ -38,15 +38,6 @@ app = FastAPI()
38
  # Global inactivity tracker
39
  last_message_time = time.time()
40
 
41
- # --- Startup Announcement ---
42
- def send_startup_message():
43
- if BOT_STATUS_CHAT:
44
- msg = "🌟 Hi! I'm Eve, your friendly AI assistant. I'm now live and ready to help you with images, voice replies, and more!"
45
- # Use a dummy message_id "startup"
46
- send_message("startup", BOT_STATUS_CHAT, msg)
47
- else:
48
- logging.warning("BOT_STATUS_CHAT is not set; startup message not sent.")
49
-
50
  # --- Inactivity Monitor ---
51
  def inactivity_monitor():
52
  global last_message_time
@@ -282,20 +273,27 @@ async def whatsapp_webhook(request: Request):
282
  "Generate a trivia question and answer in JSON format like this: "
283
  "{\"question\": \"What is the capital of France?\", \"answer\": \"Paris\"}"
284
  )
 
 
 
 
 
 
 
 
285
  try:
286
- obj = json.loads(raw)
 
287
  if "question" in obj and "answer" in obj:
288
  trivia_store[chat] = obj
289
  send_message(mid, chat, f"❓ {obj['question']}\nReply with `/answer` to see the answer.")
290
  else:
291
  raise ValueError("Missing expected keys.")
292
  except Exception as e:
293
- # Fallback: notify the user and log the raw response
294
  logging.error("Trivia JSON parse error: %s, raw response: %s", e, raw)
295
  send_message(mid, chat, "Failed to generate trivia. Please try again.")
296
  return {"success": True}
297
 
298
-
299
  if low == "/answer":
300
  if chat in trivia_store:
301
  ans = trivia_store.pop(chat)["answer"]
@@ -392,7 +390,5 @@ def index():
392
  return "Server is running!"
393
 
394
  if __name__ == "__main__":
395
- # Send startup message on launch
396
- send_startup_message()
397
  import uvicorn
398
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
38
  # Global inactivity tracker
39
  last_message_time = time.time()
40
 
 
 
 
 
 
 
 
 
 
41
  # --- Inactivity Monitor ---
42
  def inactivity_monitor():
43
  global last_message_time
 
273
  "Generate a trivia question and answer in JSON format like this: "
274
  "{\"question\": \"What is the capital of France?\", \"answer\": \"Paris\"}"
275
  )
276
+ def extract_json(text):
277
+ # Try to extract content between ```json ... ```
278
+ import re
279
+ match = re.search(r"```(?:json)?\s*(\{.*?\})\s*```", text, re.DOTALL)
280
+ if match:
281
+ return match.group(1)
282
+ return text # fallback to entire text
283
+
284
  try:
285
+ json_text = extract_json(raw)
286
+ obj = json.loads(json_text)
287
  if "question" in obj and "answer" in obj:
288
  trivia_store[chat] = obj
289
  send_message(mid, chat, f"❓ {obj['question']}\nReply with `/answer` to see the answer.")
290
  else:
291
  raise ValueError("Missing expected keys.")
292
  except Exception as e:
 
293
  logging.error("Trivia JSON parse error: %s, raw response: %s", e, raw)
294
  send_message(mid, chat, "Failed to generate trivia. Please try again.")
295
  return {"success": True}
296
 
 
297
  if low == "/answer":
298
  if chat in trivia_store:
299
  ans = trivia_store.pop(chat)["answer"]
 
390
  return "Server is running!"
391
 
392
  if __name__ == "__main__":
 
 
393
  import uvicorn
394
  uvicorn.run(app, host="0.0.0.0", port=7860)