Upload 5 files
Browse files
main.py
CHANGED
|
@@ -2,6 +2,7 @@ import os, json
|
|
| 2 |
from fastapi import FastAPI, Request, HTTPException, Query
|
| 3 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
| 4 |
import httpx
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -26,12 +27,48 @@ async def receive_update(req: Request):
|
|
| 26 |
wa_id = change["contacts"][0]["wa_id"]
|
| 27 |
msg = change["messages"][0]
|
| 28 |
|
| 29 |
-
# 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if msg["type"] != "text":
|
| 31 |
print(f"Ignoring message type: {msg['type']}")
|
| 32 |
return JSONResponse({"status": "ignored", "type": msg["type"]})
|
| 33 |
|
| 34 |
-
# 2) Handle text as before
|
| 35 |
incoming = msg["text"]["body"]
|
| 36 |
reply = f"You said: {incoming}"
|
| 37 |
|
|
|
|
| 2 |
from fastapi import FastAPI, Request, HTTPException, Query
|
| 3 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
| 4 |
import httpx
|
| 5 |
+
import base64
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
|
|
|
| 27 |
wa_id = change["contacts"][0]["wa_id"]
|
| 28 |
msg = change["messages"][0]
|
| 29 |
|
| 30 |
+
# 1) Handle image messages by converting to base64 preview
|
| 31 |
+
if msg["type"] == "image":
|
| 32 |
+
# extract media ID
|
| 33 |
+
media_id = msg["image"]["id"]
|
| 34 |
+
# fetch media URL
|
| 35 |
+
media_url_resp = httpx.get(
|
| 36 |
+
f"https://graph.facebook.com/v23.0/{media_id}",
|
| 37 |
+
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 38 |
+
)
|
| 39 |
+
media_url_resp.raise_for_status()
|
| 40 |
+
media_url = media_url_resp.json().get("url")
|
| 41 |
+
# download the media bytes
|
| 42 |
+
media_bytes_resp = httpx.get(media_url)
|
| 43 |
+
media_bytes_resp.raise_for_status()
|
| 44 |
+
data = media_bytes_resp.content
|
| 45 |
+
# convert to base64 and take first 10 chars
|
| 46 |
+
b64_str = base64.b64encode(data).decode('utf-8')
|
| 47 |
+
preview = b64_str[:10]
|
| 48 |
+
# reply with preview
|
| 49 |
+
reply = f"Base64: {preview}"
|
| 50 |
+
# send back via WhatsApp
|
| 51 |
+
payload = {
|
| 52 |
+
"messaging_product": "whatsapp",
|
| 53 |
+
"to": wa_id,
|
| 54 |
+
"type": "text",
|
| 55 |
+
"text": {"body": reply}
|
| 56 |
+
}
|
| 57 |
+
resp = httpx.post(
|
| 58 |
+
f"https://graph.facebook.com/v23.0/{PHONE_NUMBER_ID}/messages",
|
| 59 |
+
headers={
|
| 60 |
+
"Authorization": f"Bearer {WHATSAPP_API_TOKEN}",
|
| 61 |
+
"Content-Type": "application/json"
|
| 62 |
+
},
|
| 63 |
+
json=payload
|
| 64 |
+
)
|
| 65 |
+
resp.raise_for_status()
|
| 66 |
+
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 67 |
+
# 2) Existing text handler
|
| 68 |
if msg["type"] != "text":
|
| 69 |
print(f"Ignoring message type: {msg['type']}")
|
| 70 |
return JSONResponse({"status": "ignored", "type": msg["type"]})
|
| 71 |
|
|
|
|
| 72 |
incoming = msg["text"]["body"]
|
| 73 |
reply = f"You said: {incoming}"
|
| 74 |
|