Zenaight commited on
Commit
11e2002
·
verified ·
1 Parent(s): c6c1453

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -5
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
- media_resp = httpx.get(
127
- f"https://graph.facebook.com/v23.0/{media_id}",
128
- headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
129
- )
130
- media_resp.raise_for_status()
 
 
 
 
 
 
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(