Commit ·
2013b0f
1
Parent(s): 6b063c1
"Finalize robust Meta WhatsApp webhook verification"
Browse files- Dockerfile +1 -1
- app.py +16 -54
- requirements.txt +1 -2
Dockerfile
CHANGED
|
@@ -5,8 +5,8 @@ USER user
|
|
| 5 |
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
|
| 7 |
WORKDIR /app
|
| 8 |
-
|
| 9 |
COPY --chown=user . .
|
|
|
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
|
| 12 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 5 |
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
|
| 7 |
WORKDIR /app
|
|
|
|
| 8 |
COPY --chown=user . .
|
| 9 |
+
|
| 10 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
|
| 12 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -1,71 +1,33 @@
|
|
| 1 |
from fastapi import FastAPI, Request
|
|
|
|
| 2 |
import os
|
| 3 |
-
import json
|
| 4 |
-
import httpx
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
VERIFY_TOKEN = os.getenv("VERIFY_TOKEN", "dev")
|
| 9 |
-
WHATSAPP_TOKEN = os.getenv("WHATSAPP_TOKEN", "") # Add this secret later
|
| 10 |
-
|
| 11 |
@app.get("/")
|
| 12 |
def root():
|
| 13 |
return {"message": "Hello from WhatsApp ChatBot"}
|
| 14 |
|
| 15 |
@app.get("/webhook")
|
| 16 |
-
def
|
| 17 |
params = request.query_params
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return int(challenge)
|
| 24 |
-
return {"
|
| 25 |
|
| 26 |
@app.post("/webhook")
|
| 27 |
-
async def
|
| 28 |
body = await request.json()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Optional: Auto-reply (if a message exists)
|
| 33 |
-
try:
|
| 34 |
-
entry = body["entry"][0]
|
| 35 |
-
changes = entry["changes"][0]
|
| 36 |
-
value = changes["value"]
|
| 37 |
-
messages = value.get("messages")
|
| 38 |
-
|
| 39 |
-
if messages:
|
| 40 |
-
msg = messages[0]
|
| 41 |
-
phone_number = msg["from"]
|
| 42 |
-
text = msg["text"]["body"]
|
| 43 |
-
|
| 44 |
-
print(f"📲 From: {phone_number} | 💬 Text: {text}")
|
| 45 |
-
|
| 46 |
-
# Auto-reply
|
| 47 |
-
async with httpx.AsyncClient() as client:
|
| 48 |
-
await client.post(
|
| 49 |
-
"https://graph.facebook.com/v18.0/me/messages",
|
| 50 |
-
headers={
|
| 51 |
-
"Authorization": f"Bearer {WHATSAPP_TOKEN}",
|
| 52 |
-
"Content-Type": "application/json"
|
| 53 |
-
},
|
| 54 |
-
json={
|
| 55 |
-
"messaging_product": "whatsapp",
|
| 56 |
-
"to": phone_number,
|
| 57 |
-
"type": "text",
|
| 58 |
-
"text": {"body": f"You said: {text}"}
|
| 59 |
-
}
|
| 60 |
-
)
|
| 61 |
-
except Exception as e:
|
| 62 |
-
print("⚠️ Error handling message:", e)
|
| 63 |
-
|
| 64 |
-
return {"status": "received"}
|
| 65 |
|
| 66 |
@app.post("/send")
|
| 67 |
-
def check_token():
|
| 68 |
-
|
|
|
|
| 69 |
return {"token_loaded": True}
|
| 70 |
-
|
| 71 |
-
return {"error": "Token not found in environment"}
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
@app.get("/")
|
| 8 |
def root():
|
| 9 |
return {"message": "Hello from WhatsApp ChatBot"}
|
| 10 |
|
| 11 |
@app.get("/webhook")
|
| 12 |
+
async def verify_webhook(request: Request):
|
| 13 |
params = request.query_params
|
| 14 |
+
verify_token = os.getenv("WHATSAPP_TOKEN", "")
|
| 15 |
+
if (
|
| 16 |
+
params.get("hub.mode") == "subscribe"
|
| 17 |
+
and params.get("hub.verify_token") == verify_token
|
| 18 |
+
):
|
| 19 |
+
return int(params.get("hub.challenge"))
|
| 20 |
+
return JSONResponse(content={"error": "Invalid token"}, status_code=403)
|
| 21 |
|
| 22 |
@app.post("/webhook")
|
| 23 |
+
async def receive_webhook(request: Request):
|
| 24 |
body = await request.json()
|
| 25 |
+
# (Optional) Log or process body here
|
| 26 |
+
return {"status": "Webhook received"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
@app.post("/send")
|
| 29 |
+
async def check_token():
|
| 30 |
+
token = os.getenv("WHATSAPP_TOKEN", "")
|
| 31 |
+
if token:
|
| 32 |
return {"token_loaded": True}
|
| 33 |
+
return JSONResponse(content={"error": "Token not found in environment"}, status_code=400)
|
|
|
requirements.txt
CHANGED
|
@@ -1,3 +1,2 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn
|
| 3 |
-
httpx
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn
|
|
|