Upload 5 files
Browse files
main.py
CHANGED
|
@@ -55,19 +55,19 @@ async def receive_update(req: Request):
|
|
| 55 |
# IMAGE: if user sent an image, convert to Base64 preview
|
| 56 |
if msg["type"] == "image":
|
| 57 |
media_id = msg["image"]["id"]
|
| 58 |
-
|
| 59 |
media_resp = httpx.get(
|
| 60 |
f"https://graph.facebook.com/v23.0/{media_id}",
|
| 61 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 62 |
)
|
| 63 |
-
media_resp.
|
| 64 |
-
media_url = media_resp.json()
|
| 65 |
-
|
| 66 |
media_bytes = httpx.get(
|
| 67 |
media_url,
|
| 68 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 69 |
).content
|
| 70 |
-
|
| 71 |
# full Base64 string
|
| 72 |
b64_str = base64.b64encode(media_bytes).decode("utf-8")
|
| 73 |
# prepare payload for ID processing API
|
|
@@ -80,13 +80,13 @@ async def receive_update(req: Request):
|
|
| 80 |
"binaryString": "1111111011110011"
|
| 81 |
}
|
| 82 |
}
|
| 83 |
-
# call external ID processing service
|
| 84 |
api_resp = httpx.post(
|
| 85 |
"https://meerkat-trusted-sturgeon.ngrok-free.app/api/process_id",
|
| 86 |
json=api_payload
|
| 87 |
)
|
| 88 |
-
api_resp.
|
| 89 |
result = api_resp.json()
|
|
|
|
| 90 |
# format WhatsApp reply
|
| 91 |
if result.get("status") == "success" and result["results"]:
|
| 92 |
det = result["results"][0]["detections"][0]
|
|
@@ -98,6 +98,7 @@ async def receive_update(req: Request):
|
|
| 98 |
)
|
| 99 |
else:
|
| 100 |
reply = f"ID processing failed: {result.get('status', 'error')}"
|
|
|
|
| 101 |
send_whatsapp_message(wa_id, reply)
|
| 102 |
return JSONResponse({"status": "processed", "api_status": result.get("status")})
|
| 103 |
|
|
|
|
| 55 |
# IMAGE: if user sent an image, convert to Base64 preview
|
| 56 |
if msg["type"] == "image":
|
| 57 |
media_id = msg["image"]["id"]
|
| 58 |
+
print(f"[DEBUG] Received media_id: {media_id}")
|
| 59 |
media_resp = httpx.get(
|
| 60 |
f"https://graph.facebook.com/v23.0/{media_id}",
|
| 61 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 62 |
)
|
| 63 |
+
print(f"[DEBUG] Media metadata response status: {media_resp.status_code}")
|
| 64 |
+
media_url = media_resp.json().get("url")
|
| 65 |
+
print(f"[DEBUG] Media URL: {media_url}")
|
| 66 |
media_bytes = httpx.get(
|
| 67 |
media_url,
|
| 68 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 69 |
).content
|
| 70 |
+
print(f"[DEBUG] Downloaded {len(media_bytes)} bytes from media URL")
|
| 71 |
# full Base64 string
|
| 72 |
b64_str = base64.b64encode(media_bytes).decode("utf-8")
|
| 73 |
# prepare payload for ID processing API
|
|
|
|
| 80 |
"binaryString": "1111111011110011"
|
| 81 |
}
|
| 82 |
}
|
|
|
|
| 83 |
api_resp = httpx.post(
|
| 84 |
"https://meerkat-trusted-sturgeon.ngrok-free.app/api/process_id",
|
| 85 |
json=api_payload
|
| 86 |
)
|
| 87 |
+
print(f"[DEBUG] ID API POST status: {api_resp.status_code}")
|
| 88 |
result = api_resp.json()
|
| 89 |
+
print(f"[DEBUG] ID API response body: {json.dumps(result)[:300]}")
|
| 90 |
# format WhatsApp reply
|
| 91 |
if result.get("status") == "success" and result["results"]:
|
| 92 |
det = result["results"][0]["detections"][0]
|
|
|
|
| 98 |
)
|
| 99 |
else:
|
| 100 |
reply = f"ID processing failed: {result.get('status', 'error')}"
|
| 101 |
+
print(f"[DEBUG] Replying with message: {reply}")
|
| 102 |
send_whatsapp_message(wa_id, reply)
|
| 103 |
return JSONResponse({"status": "processed", "api_status": result.get("status")})
|
| 104 |
|