Upload main.py
Browse files
main.py
CHANGED
|
@@ -123,11 +123,17 @@ async def receive_update(req: Request):
|
|
| 123 |
if msg["type"] == "image":
|
| 124 |
media_id = msg["image"]["id"]
|
| 125 |
# 1) get media URL
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
media_url = media_resp.json()["url"]
|
| 132 |
# 2) download bytes with auth
|
| 133 |
media_bytes = httpx.get(
|
|
|
|
| 123 |
if msg["type"] == "image":
|
| 124 |
media_id = msg["image"]["id"]
|
| 125 |
# 1) get media URL
|
| 126 |
+
try:
|
| 127 |
+
media_resp = httpx.get(
|
| 128 |
+
f"https://graph.facebook.com/v15.0/{media_id}",
|
| 129 |
+
params={"fields": "url"},
|
| 130 |
+
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 131 |
+
)
|
| 132 |
+
media_resp.raise_for_status()
|
| 133 |
+
except httpx.HTTPStatusError as e:
|
| 134 |
+
print("⚠️ Failed to fetch media URL:", e)
|
| 135 |
+
send_whatsapp_message(wa_id, "❌ Sorry, I couldn’t download your image. Please try again.")
|
| 136 |
+
return JSONResponse({"status": "error", "detail": "media fetch failed"})
|
| 137 |
media_url = media_resp.json()["url"]
|
| 138 |
# 2) download bytes with auth
|
| 139 |
media_bytes = httpx.get(
|