Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
import hmac
|
| 3 |
-
import hashlib
|
| 4 |
-
|
| 5 |
-
from fastapi import FastAPI, Header, HTTPException, Request, Query
|
| 6 |
-
from typing import Optional
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
async def handle_webhook(
|
| 14 |
-
hub_mode: str = Query(..., alias="hub.mode"),
|
| 15 |
-
hub_challenge: int = Query(..., alias="hub.challenge"),
|
| 16 |
-
hub_verify_token: str = Query(..., alias="hub.verify_token")
|
| 17 |
-
):
|
| 18 |
-
if hub_mode == "subscribe" and hub_verify_token == VERIFY_TOKEN:
|
| 19 |
-
return int(hub_challenge)
|
| 20 |
-
else:
|
| 21 |
-
raise HTTPException(status_code=403, detail="Verification failed")
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
@app.post("/webhooks")
|
| 26 |
-
async def handle_event_notifications(
|
| 27 |
-
request: Request,
|
| 28 |
-
x_hub_signature_256: Optional[str] = Header(None) # Header for signature verification
|
| 29 |
-
):
|
| 30 |
-
# Read and verify the request body
|
| 31 |
-
body = await request.body()
|
| 32 |
-
|
| 33 |
-
# Verify the X-Hub-Signature-256 header
|
| 34 |
-
if not x_hub_signature_256:
|
| 35 |
-
raise HTTPException(status_code=400, detail="Missing X-Hub-Signature-256 header")
|
| 36 |
-
|
| 37 |
-
# Compute the expected signature
|
| 38 |
-
expected_signature = (
|
| 39 |
-
"sha256="
|
| 40 |
-
+ hmac.new(VERIFY_TOKEN.encode(), body, hashlib.sha256).hexdigest()
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
if not hmac.compare_digest(expected_signature, x_hub_signature_256):
|
| 44 |
-
raise HTTPException(status_code=403, detail="Signature verification failed")
|
| 45 |
-
|
| 46 |
-
# Parse the JSON payload
|
| 47 |
-
payload = await request.json()
|
| 48 |
-
object_type = payload.get("object")
|
| 49 |
-
entries = payload.get("entry", [])
|
| 50 |
-
|
| 51 |
-
# Log the received event (or process as needed)
|
| 52 |
-
for entry in entries:
|
| 53 |
-
changes = entry.get("changes", [])
|
| 54 |
-
for change in changes:
|
| 55 |
-
field = change.get("field")
|
| 56 |
-
value = change.get("value")
|
| 57 |
-
# Handle specific fields or values as required
|
| 58 |
-
print(f"Received change for field: {field}, value: {value}")
|
| 59 |
-
|
| 60 |
-
# Return a 200 OK response to acknowledge receipt
|
| 61 |
-
return {"status": "ok"}
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
+
@app.get("/")
|
| 6 |
+
def greet_json():
|
| 7 |
+
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|