Upload main.py
Browse files
main.py
CHANGED
|
@@ -41,7 +41,7 @@ async def receive_update(req: Request):
|
|
| 41 |
wa_id = change["contacts"][0]["wa_id"]
|
| 42 |
msg = change["messages"][0]
|
| 43 |
|
| 44 |
-
# IMAGE: convert to Base64 preview
|
| 45 |
if msg["type"] == "image":
|
| 46 |
media_id = msg["image"]["id"]
|
| 47 |
# 1) get media URL
|
|
@@ -51,23 +51,25 @@ async def receive_update(req: Request):
|
|
| 51 |
)
|
| 52 |
media_resp.raise_for_status()
|
| 53 |
media_url = media_resp.json()["url"]
|
| 54 |
-
# 2) download bytes
|
| 55 |
media_bytes = httpx.get(
|
| 56 |
media_url,
|
| 57 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 58 |
).content
|
| 59 |
# 3) encode & preview
|
| 60 |
preview = base64.b64encode(media_bytes).decode("utf-8")[:10]
|
|
|
|
| 61 |
send_whatsapp_message(wa_id, f"Base64: {preview}")
|
| 62 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 63 |
|
| 64 |
# TEXT: simple echo
|
| 65 |
if msg["type"] == "text":
|
| 66 |
incoming = msg["text"]["body"]
|
| 67 |
-
|
|
|
|
| 68 |
return JSONResponse({"status": "replied"})
|
| 69 |
|
| 70 |
-
#
|
| 71 |
return JSONResponse({"status": "ignored", "type": msg["type"]})
|
| 72 |
|
| 73 |
# health-check endpoint for token
|
|
|
|
| 41 |
wa_id = change["contacts"][0]["wa_id"]
|
| 42 |
msg = change["messages"][0]
|
| 43 |
|
| 44 |
+
# IMAGE: if user sent an image, convert to Base64 preview
|
| 45 |
if msg["type"] == "image":
|
| 46 |
media_id = msg["image"]["id"]
|
| 47 |
# 1) get media URL
|
|
|
|
| 51 |
)
|
| 52 |
media_resp.raise_for_status()
|
| 53 |
media_url = media_resp.json()["url"]
|
| 54 |
+
# 2) download bytes with auth
|
| 55 |
media_bytes = httpx.get(
|
| 56 |
media_url,
|
| 57 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 58 |
).content
|
| 59 |
# 3) encode & preview
|
| 60 |
preview = base64.b64encode(media_bytes).decode("utf-8")[:10]
|
| 61 |
+
# send back preview
|
| 62 |
send_whatsapp_message(wa_id, f"Base64: {preview}")
|
| 63 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 64 |
|
| 65 |
# TEXT: simple echo
|
| 66 |
if msg["type"] == "text":
|
| 67 |
incoming = msg["text"]["body"]
|
| 68 |
+
reply = f"You said: {incoming}"
|
| 69 |
+
send_whatsapp_message(wa_id, reply)
|
| 70 |
return JSONResponse({"status": "replied"})
|
| 71 |
|
| 72 |
+
# OTHER: ignore
|
| 73 |
return JSONResponse({"status": "ignored", "type": msg["type"]})
|
| 74 |
|
| 75 |
# health-check endpoint for token
|