Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,54 +3,47 @@ from datetime import datetime, timezone, timedelta
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@app.post("/webhook")
|
| 7 |
async def receive_webhook(request: Request):
|
| 8 |
payload = await request.json()
|
| 9 |
print("📥 Webhook received:")
|
| 10 |
|
| 11 |
-
# Define GMT+3 offset
|
| 12 |
gmt_plus_3 = timezone(timedelta(hours=3))
|
| 13 |
|
| 14 |
-
# Top-level fields
|
| 15 |
-
print(f"Request ID: {payload.get('request_id')}")
|
| 16 |
print(f"Object: {payload.get('object')}")
|
| 17 |
-
print(f"Time (Unix): {payload.get('time')}")
|
| 18 |
-
try:
|
| 19 |
-
formatted_time = datetime.fromtimestamp(payload.get('time'), gmt_plus_3).strftime('%Y-%m-%d %H:%M:%S GMT+3')
|
| 20 |
-
print(f"Time (Formatted): {formatted_time}")
|
| 21 |
-
except Exception:
|
| 22 |
-
print("Time (Formatted): Invalid timestamp")
|
| 23 |
print("----")
|
| 24 |
|
| 25 |
-
# Loop through entries
|
| 26 |
for entry in payload.get('entry', []):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
print(f"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
print(f"{change.get('field').capitalize()}: {change.get('value')}")
|
| 54 |
-
print("----")
|
| 55 |
-
|
| 56 |
-
return {"status": "ok"}
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
+
@app.get("/webhook")
|
| 7 |
+
async def verify_webhook(hub_mode: str = "", hub_verify_token: str = "", hub_challenge: str = ""):
|
| 8 |
+
VERIFY_TOKEN = "my_secure_token" # Match this to your subscription
|
| 9 |
+
if hub_mode == "subscribe" and hub_verify_token == VERIFY_TOKEN:
|
| 10 |
+
return int(hub_challenge)
|
| 11 |
+
return {"status": "verification failed"}
|
| 12 |
+
|
| 13 |
@app.post("/webhook")
|
| 14 |
async def receive_webhook(request: Request):
|
| 15 |
payload = await request.json()
|
| 16 |
print("📥 Webhook received:")
|
| 17 |
|
|
|
|
| 18 |
gmt_plus_3 = timezone(timedelta(hours=3))
|
| 19 |
|
|
|
|
|
|
|
| 20 |
print(f"Object: {payload.get('object')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
print("----")
|
| 22 |
|
|
|
|
| 23 |
for entry in payload.get('entry', []):
|
| 24 |
+
entry_id = entry.get("id")
|
| 25 |
+
timestamp = entry.get("time")
|
| 26 |
+
|
| 27 |
+
print(f"📄 Entry ID (Page ID): {entry_id}")
|
| 28 |
+
if timestamp:
|
| 29 |
+
try:
|
| 30 |
+
formatted = datetime.fromtimestamp(timestamp, gmt_plus_3).strftime('%Y-%m-%d %H:%M:%S GMT+3')
|
| 31 |
+
print(f"🕒 Time: {formatted}")
|
| 32 |
+
except:
|
| 33 |
+
print(f"🕒 Time: {timestamp} (Invalid format)")
|
| 34 |
+
|
| 35 |
+
for change in entry.get("changes", []):
|
| 36 |
+
field = change.get("field")
|
| 37 |
+
value = change.get("value", {})
|
| 38 |
+
|
| 39 |
+
print(f"🔔 Change Field: {field}")
|
| 40 |
+
if field == "leadgen":
|
| 41 |
+
print("📌 Lead Details:")
|
| 42 |
+
print(f"• Lead ID: {value.get('leadgen_id')}")
|
| 43 |
+
print(f"• Form ID: {value.get('form_id')}")
|
| 44 |
+
print(f"• Ad ID: {value.get('ad_id')}")
|
| 45 |
+
print(f"• Page ID: {value.get('page_id')}")
|
| 46 |
+
print(f"• Created Time: {value.get('created_time')}")
|
| 47 |
+
|
| 48 |
+
print("----")
|
| 49 |
+
return {"status": "ok"}
|
|
|
|
|
|
|
|
|
|
|
|