Mr-Help commited on
Commit
284af3b
·
verified ·
1 Parent(s): 6e7aa15

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -44
main.py CHANGED
@@ -1,49 +1,17 @@
1
  from fastapi import FastAPI, Request
2
- from datetime import datetime, timezone, timedelta
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"}
 
1
  from fastapi import FastAPI, Request
2
+ from fastapi.responses import PlainTextResponse
3
 
4
  app = FastAPI()
5
 
6
+ VERIFY_TOKEN = "my_secure_token"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ @app.get("/webhook")
9
+ async def verify_webhook(request: Request):
10
+ params = request.query_params
11
+ mode = params.get("hub.mode")
12
+ token = params.get("hub.verify_token")
13
+ challenge = params.get("hub.challenge")
14
+
15
+ if mode == "subscribe" and token == VERIFY_TOKEN:
16
+ return PlainTextResponse(content=challenge) # 👈 this is key
17
+ return PlainTextResponse(content="Verification failed", status_code=403)