Upload main.py
Browse files
main.py
CHANGED
|
@@ -107,10 +107,11 @@ def send_whatsapp_message(wa_id, reply):
|
|
| 107 |
async def receive_update(req: Request):
|
| 108 |
data = await req.json()
|
| 109 |
try:
|
| 110 |
-
|
| 111 |
-
contacts =
|
| 112 |
-
messages =
|
| 113 |
if not contacts or not messages:
|
|
|
|
| 114 |
print("Ignoring update, no contacts or messages field")
|
| 115 |
return JSONResponse({"status": "ignored", "reason": "no contacts/messages"})
|
| 116 |
|
|
@@ -120,43 +121,46 @@ async def receive_update(req: Request):
|
|
| 120 |
print("Malformed webhook payload:", e)
|
| 121 |
return JSONResponse({"status": "ignored", "reason": "malformed payload"})
|
| 122 |
|
| 123 |
-
|
| 124 |
-
if
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
# 3) if user sends an image, process it once
|
| 133 |
-
if msg.get("type") == "image":
|
| 134 |
-
msg_id = msg.get("id")
|
| 135 |
-
if not msg_id:
|
| 136 |
-
return JSONResponse({"status": "ignored", "reason": "no message id"})
|
| 137 |
-
if msg_id in processed_message_ids:
|
| 138 |
-
print(f"Duplicate image message {msg_id}, skipping.")
|
| 139 |
-
return JSONResponse({"status": "duplicate", "id": msg_id})
|
| 140 |
-
processed_message_ids.add(msg_id)
|
| 141 |
-
# (Optional: persist msg_id if you want across restarts)
|
| 142 |
media_id = msg["image"]["id"]
|
| 143 |
-
#
|
| 144 |
media_resp = httpx.get(
|
| 145 |
f"https://graph.facebook.com/v23.0/{media_id}",
|
| 146 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 147 |
)
|
| 148 |
media_resp.raise_for_status()
|
| 149 |
media_url = media_resp.json()["url"]
|
|
|
|
| 150 |
media_bytes = httpx.get(
|
| 151 |
media_url,
|
| 152 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 153 |
).content
|
|
|
|
| 154 |
full_b64 = base64.b64encode(media_bytes).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
| 155 |
asyncio.create_task(forward_to_rumas(full_b64, wa_id))
|
| 156 |
-
return JSONResponse({"status":"processed_id"})
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# health-check endpoint for token
|
| 162 |
@app.post("/send")
|
|
|
|
| 107 |
async def receive_update(req: Request):
|
| 108 |
data = await req.json()
|
| 109 |
try:
|
| 110 |
+
change = data["entry"][0]["changes"][0]["value"]
|
| 111 |
+
contacts = change.get("contacts")
|
| 112 |
+
messages = change.get("messages")
|
| 113 |
if not contacts or not messages:
|
| 114 |
+
# Missing expected payload—ignore
|
| 115 |
print("Ignoring update, no contacts or messages field")
|
| 116 |
return JSONResponse({"status": "ignored", "reason": "no contacts/messages"})
|
| 117 |
|
|
|
|
| 121 |
print("Malformed webhook payload:", e)
|
| 122 |
return JSONResponse({"status": "ignored", "reason": "malformed payload"})
|
| 123 |
|
| 124 |
+
msg_id = msg.get("id")
|
| 125 |
+
if msg_id in processed_message_ids:
|
| 126 |
+
print(f"Duplicate message {msg_id}, skipping.")
|
| 127 |
+
return JSONResponse({"status": "duplicate", "id": msg_id})
|
| 128 |
+
processed_message_ids.add(msg_id)
|
| 129 |
+
|
| 130 |
+
# IMAGE: if user sent an image, convert to Base64 preview
|
| 131 |
+
if msg["type"] == "image":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
media_id = msg["image"]["id"]
|
| 133 |
+
# 1) get media URL
|
| 134 |
media_resp = httpx.get(
|
| 135 |
f"https://graph.facebook.com/v23.0/{media_id}",
|
| 136 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 137 |
)
|
| 138 |
media_resp.raise_for_status()
|
| 139 |
media_url = media_resp.json()["url"]
|
| 140 |
+
# 2) download bytes with auth
|
| 141 |
media_bytes = httpx.get(
|
| 142 |
media_url,
|
| 143 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 144 |
).content
|
| 145 |
+
# 3) encode full & preview
|
| 146 |
full_b64 = base64.b64encode(media_bytes).decode("utf-8")
|
| 147 |
+
preview = full_b64[:10]
|
| 148 |
+
|
| 149 |
+
# 4) forward full string to RUMAS (non-blocking)
|
| 150 |
asyncio.create_task(forward_to_rumas(full_b64, wa_id))
|
|
|
|
| 151 |
|
| 152 |
+
# 5) echo preview to the user
|
| 153 |
+
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 154 |
+
|
| 155 |
+
# TEXT: simple echo
|
| 156 |
+
if msg["type"] == "text":
|
| 157 |
+
incoming = msg["text"]["body"]
|
| 158 |
+
reply = f"You said: {incoming}"
|
| 159 |
+
send_whatsapp_message(wa_id, reply)
|
| 160 |
+
return JSONResponse({"status": "replied"})
|
| 161 |
+
|
| 162 |
+
# OTHER: ignore
|
| 163 |
+
return JSONResponse({"status": "ignored", "type": msg["type"]})
|
| 164 |
|
| 165 |
# health-check endpoint for token
|
| 166 |
@app.post("/send")
|