Upload 5 files
Browse files
main.py
CHANGED
|
@@ -3,12 +3,36 @@ 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 |
|
| 9 |
VERIFY_TOKEN = os.getenv("VERIFY_TOKEN", "") # set in HF Secrets
|
| 10 |
PHONE_NUMBER_ID = os.getenv("PHONE_NUMBER_ID", "")
|
| 11 |
WHATSAPP_API_TOKEN = os.getenv("WHATSAPP_API_TOKEN", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
@app.get("/webhook") # verification
|
| 14 |
def verify(
|
|
@@ -67,9 +91,14 @@ async def receive_update(req: Request):
|
|
| 67 |
media_url,
|
| 68 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 69 |
).content
|
| 70 |
-
# 3) encode & preview
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
send_whatsapp_message(wa_id, f"Base64: {preview}")
|
| 74 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 75 |
|
|
|
|
| 3 |
from fastapi.responses import PlainTextResponse, JSONResponse
|
| 4 |
import httpx
|
| 5 |
import base64
|
| 6 |
+
import asyncio
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
VERIFY_TOKEN = os.getenv("VERIFY_TOKEN", "") # set in HF Secrets
|
| 11 |
PHONE_NUMBER_ID = os.getenv("PHONE_NUMBER_ID", "")
|
| 12 |
WHATSAPP_API_TOKEN = os.getenv("WHATSAPP_API_TOKEN", "")
|
| 13 |
+
RUMAS_API_URL = os.getenv(
|
| 14 |
+
"RUMAS_API_URL",
|
| 15 |
+
"https://meerkat-trusted-sturgeon.ngrok-free.app/api/process_id"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
async def forward_to_rumas(b64_str: str):
|
| 19 |
+
"""Fire-and-forget call to RUMAS /api/process_id."""
|
| 20 |
+
payload = {
|
| 21 |
+
"data": [b64_str],
|
| 22 |
+
"configurations": {
|
| 23 |
+
"iddetectionconfidence": "80",
|
| 24 |
+
"smartidconfidence": "50",
|
| 25 |
+
"greenidconfidence": "50",
|
| 26 |
+
"binaryString": "1111111011110011"
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
try:
|
| 30 |
+
async with httpx.AsyncClient(timeout=30) as client:
|
| 31 |
+
resp = await client.post(RUMAS_API_URL, json=payload)
|
| 32 |
+
resp.raise_for_status()
|
| 33 |
+
# Optional: print or log resp.json()
|
| 34 |
+
except Exception as e:
|
| 35 |
+
print("RUMAS API error:", e)
|
| 36 |
|
| 37 |
@app.get("/webhook") # verification
|
| 38 |
def verify(
|
|
|
|
| 91 |
media_url,
|
| 92 |
headers={"Authorization": f"Bearer {WHATSAPP_API_TOKEN}"}
|
| 93 |
).content
|
| 94 |
+
# 3) encode full & preview
|
| 95 |
+
full_b64 = base64.b64encode(media_bytes).decode("utf-8")
|
| 96 |
+
preview = full_b64[:10]
|
| 97 |
+
|
| 98 |
+
# 4) forward full string to RUMAS (non-blocking)
|
| 99 |
+
asyncio.create_task(forward_to_rumas(full_b64))
|
| 100 |
+
|
| 101 |
+
# 5) echo preview to the user
|
| 102 |
send_whatsapp_message(wa_id, f"Base64: {preview}")
|
| 103 |
return JSONResponse({"status": "image_preview_sent", "preview": preview})
|
| 104 |
|