Upload main.py
Browse files
main.py
CHANGED
|
@@ -12,9 +12,28 @@ PHONE_NUMBER_ID = os.getenv("PHONE_NUMBER_ID", "")
|
|
| 12 |
WHATSAPP_API_TOKEN = os.getenv("WHATSAPP_API_TOKEN", "")
|
| 13 |
RUMAS_API_URL = os.getenv("RUMAS_API_URL", "")
|
| 14 |
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""Fire-and-forget call to RUMAS /api/process_id."""
|
| 19 |
payload = {
|
| 20 |
"data": [b64_str],
|
|
@@ -43,7 +62,8 @@ async def forward_to_rumas(b64_str: str, wa_id: str):
|
|
| 43 |
pretty_print_dict(body)
|
| 44 |
|
| 45 |
# ββ send parsed ID back to WhatsApp ββ
|
| 46 |
-
if wa_id and data.get("status") == "success":
|
|
|
|
| 47 |
det = data["results"][0]["detections"][0]
|
| 48 |
reply = (
|
| 49 |
"β
*ID processed*\n"
|
|
@@ -59,9 +79,18 @@ async def forward_to_rumas(b64_str: str, wa_id: str):
|
|
| 59 |
f"*Age*: {det.get('age','?')}"
|
| 60 |
)
|
| 61 |
send_whatsapp_message(wa_id, reply)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
except Exception as e:
|
| 64 |
print(f"[RUMAS] β error while sending '{short}β¦' β", e)
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def pretty_print_dict(d, indent=0):
|
| 67 |
for k, v in d.items():
|
|
@@ -160,6 +189,7 @@ async def receive_update(req: Request):
|
|
| 160 |
print(f"Duplicate message {msg_id}, skipping.")
|
| 161 |
return JSONResponse({"status": "duplicate", "id": msg_id})
|
| 162 |
processed_message_ids.add(msg_id)
|
|
|
|
| 163 |
|
| 164 |
# TEXT: send welcome message
|
| 165 |
if msg.get("type") == "text":
|
|
@@ -190,7 +220,10 @@ async def receive_update(req: Request):
|
|
| 190 |
preview = full_b64[:10]
|
| 191 |
|
| 192 |
# 4) forward full string to RUMAS (non-blocking)
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
# 5) echo preview to the user
|
| 196 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
|
|
|
| 12 |
WHATSAPP_API_TOKEN = os.getenv("WHATSAPP_API_TOKEN", "")
|
| 13 |
RUMAS_API_URL = os.getenv("RUMAS_API_URL", "")
|
| 14 |
|
| 15 |
+
# Add task tracking at the top
|
| 16 |
+
active_tasks = {}
|
| 17 |
|
| 18 |
+
# Persistent deduplication using a file
|
| 19 |
+
DEDUP_FILE = "processed_message_ids.txt"
|
| 20 |
+
def load_processed_ids():
|
| 21 |
+
try:
|
| 22 |
+
with open(DEDUP_FILE, "r") as f:
|
| 23 |
+
return set(line.strip() for line in f if line.strip())
|
| 24 |
+
except FileNotFoundError:
|
| 25 |
+
return set()
|
| 26 |
+
|
| 27 |
+
def save_processed_id(msg_id):
|
| 28 |
+
try:
|
| 29 |
+
with open(DEDUP_FILE, "a") as f:
|
| 30 |
+
f.write(msg_id + "\n")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Error saving msg_id {msg_id}: {e}")
|
| 33 |
+
|
| 34 |
+
processed_message_ids = load_processed_ids()
|
| 35 |
+
|
| 36 |
+
async def forward_to_rumas(b64_str: str, wa_id: str, task_id: str):
|
| 37 |
"""Fire-and-forget call to RUMAS /api/process_id."""
|
| 38 |
payload = {
|
| 39 |
"data": [b64_str],
|
|
|
|
| 62 |
pretty_print_dict(body)
|
| 63 |
|
| 64 |
# ββ send parsed ID back to WhatsApp ββ
|
| 65 |
+
if wa_id and data.get("status") == "success" and task_id in active_tasks:
|
| 66 |
+
print(f"[TASK] Sending response for active task {task_id}")
|
| 67 |
det = data["results"][0]["detections"][0]
|
| 68 |
reply = (
|
| 69 |
"β
*ID processed*\n"
|
|
|
|
| 79 |
f"*Age*: {det.get('age','?')}"
|
| 80 |
)
|
| 81 |
send_whatsapp_message(wa_id, reply)
|
| 82 |
+
# Remove task from active list
|
| 83 |
+
active_tasks.pop(task_id, None)
|
| 84 |
+
print(f"[TASK] Completed and removed task {task_id}")
|
| 85 |
+
elif task_id not in active_tasks:
|
| 86 |
+
print(f"[TASK] Ignoring response for inactive task {task_id}")
|
| 87 |
+
else:
|
| 88 |
+
print(f"[TASK] RUMAS response not successful for task {task_id}")
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
print(f"[RUMAS] β error while sending '{short}β¦' β", e)
|
| 92 |
+
# Remove task from active list on error
|
| 93 |
+
active_tasks.pop(task_id, None)
|
| 94 |
|
| 95 |
def pretty_print_dict(d, indent=0):
|
| 96 |
for k, v in d.items():
|
|
|
|
| 189 |
print(f"Duplicate message {msg_id}, skipping.")
|
| 190 |
return JSONResponse({"status": "duplicate", "id": msg_id})
|
| 191 |
processed_message_ids.add(msg_id)
|
| 192 |
+
save_processed_id(msg_id) # Save to file
|
| 193 |
|
| 194 |
# TEXT: send welcome message
|
| 195 |
if msg.get("type") == "text":
|
|
|
|
| 220 |
preview = full_b64[:10]
|
| 221 |
|
| 222 |
# 4) forward full string to RUMAS (non-blocking)
|
| 223 |
+
task_id = str(msg_id) # Use msg_id as task_id
|
| 224 |
+
active_tasks[task_id] = True # Add task to active list
|
| 225 |
+
print(f"[TASK] Created task {task_id} for message {msg_id}")
|
| 226 |
+
asyncio.create_task(forward_to_rumas(full_b64, wa_id, task_id))
|
| 227 |
|
| 228 |
# 5) echo preview to the user
|
| 229 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|