Upload 5 files
Browse files
main.py
CHANGED
|
@@ -55,52 +55,23 @@ 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 |
-
|
| 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 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
"data": [b64_str],
|
| 76 |
-
"configurations": {
|
| 77 |
-
"iddetectionconfidence": "80",
|
| 78 |
-
"smartidconfidence": "50",
|
| 79 |
-
"greenidconfidence": "50",
|
| 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]
|
| 93 |
-
reply = (
|
| 94 |
-
f"ID Number: {det['id_num']}\n"
|
| 95 |
-
f"Name: {det['name']} {det['surname']}\n"
|
| 96 |
-
f"DOB: {det['date_of_birth']}\n"
|
| 97 |
-
f"Gender: {det['gender']}"
|
| 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 |
|
| 105 |
# TEXT: simple echo
|
| 106 |
if msg["type"] == "text":
|
|
|
|
| 55 |
# IMAGE: if user sent an image, convert to Base64 preview
|
| 56 |
if msg["type"] == "image":
|
| 57 |
media_id = msg["image"]["id"]
|
| 58 |
+
# 1) get media URL
|
| 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.raise_for_status()
|
| 64 |
+
media_url = media_resp.json()["url"]
|
| 65 |
+
# 2) download bytes with auth
|
| 66 |
media_bytes = httpx.get(
|
| 67 |
media_url,
|
| 68 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 69 |
).content
|
| 70 |
+
# 3) encode & preview
|
| 71 |
+
preview = base64.b64encode(media_bytes).decode("utf-8")[:10]
|
| 72 |
+
# send back preview
|
| 73 |
+
send_whatsapp_message(wa_id, f"Base64: {preview}")
|
| 74 |
+
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# TEXT: simple echo
|
| 77 |
if msg["type"] == "text":
|