Upload main.py
Browse files
main.py
CHANGED
|
@@ -12,6 +12,8 @@ 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 |
async def forward_to_rumas(b64_str: str, wa_id: str):
|
| 16 |
"""Fire-and-forget call to RUMAS /api/process_id."""
|
| 17 |
payload = {
|
|
@@ -51,6 +53,7 @@ async def forward_to_rumas(b64_str: str, wa_id: str):
|
|
| 51 |
f"*Gender*: {det.get('gender','?')}"
|
| 52 |
)
|
| 53 |
send_whatsapp_message(wa_id, reply)
|
|
|
|
| 54 |
|
| 55 |
except Exception as e:
|
| 56 |
print(f"[RUMAS] ✘ error while sending '{short}…' →", e)
|
|
@@ -113,6 +116,12 @@ async def receive_update(req: Request):
|
|
| 113 |
print("Malformed webhook payload:", e)
|
| 114 |
return JSONResponse({"status": "ignored", "reason": "malformed payload"})
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# IMAGE: if user sent an image, convert to Base64 preview
|
| 117 |
if msg["type"] == "image":
|
| 118 |
media_id = msg["image"]["id"]
|
|
@@ -136,7 +145,6 @@ async def receive_update(req: Request):
|
|
| 136 |
asyncio.create_task(forward_to_rumas(full_b64, wa_id))
|
| 137 |
|
| 138 |
# 5) echo preview to the user
|
| 139 |
-
send_whatsapp_message(wa_id, f"Base64: {preview}")
|
| 140 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 141 |
|
| 142 |
# TEXT: simple echo
|
|
|
|
| 12 |
WHATSAPP_API_TOKEN = os.getenv("WHATSAPP_API_TOKEN", "")
|
| 13 |
RUMAS_API_URL = os.getenv("RUMAS_API_URL", "")
|
| 14 |
|
| 15 |
+
processed_message_ids = set()
|
| 16 |
+
|
| 17 |
async def forward_to_rumas(b64_str: str, wa_id: str):
|
| 18 |
"""Fire-and-forget call to RUMAS /api/process_id."""
|
| 19 |
payload = {
|
|
|
|
| 53 |
f"*Gender*: {det.get('gender','?')}"
|
| 54 |
)
|
| 55 |
send_whatsapp_message(wa_id, reply)
|
| 56 |
+
send_whatsapp_message(wa_id, f"Full RUMAS response:\n{json.dumps(data, indent=2)[:4000]}")
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
print(f"[RUMAS] ✘ error while sending '{short}…' →", e)
|
|
|
|
| 116 |
print("Malformed webhook payload:", e)
|
| 117 |
return JSONResponse({"status": "ignored", "reason": "malformed payload"})
|
| 118 |
|
| 119 |
+
msg_id = msg.get("id")
|
| 120 |
+
if msg_id in processed_message_ids:
|
| 121 |
+
print(f"Duplicate message {msg_id}, skipping.")
|
| 122 |
+
return JSONResponse({"status": "duplicate", "id": msg_id})
|
| 123 |
+
processed_message_ids.add(msg_id)
|
| 124 |
+
|
| 125 |
# IMAGE: if user sent an image, convert to Base64 preview
|
| 126 |
if msg["type"] == "image":
|
| 127 |
media_id = msg["image"]["id"]
|
|
|
|
| 145 |
asyncio.create_task(forward_to_rumas(full_b64, wa_id))
|
| 146 |
|
| 147 |
# 5) echo preview to the user
|
|
|
|
| 148 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 149 |
|
| 150 |
# TEXT: simple echo
|