Mr-Help commited on
Commit
953d0c7
·
verified ·
1 Parent(s): 4415b9c

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +26 -6
main.py CHANGED
@@ -1,12 +1,13 @@
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import PlainTextResponse
3
  from datetime import datetime, timezone, timedelta
 
4
 
5
  app = FastAPI()
6
 
7
  VERIFY_TOKEN = "my_secure_token"
 
8
 
9
- # Verification (GET)
10
  @app.get("/webhook")
11
  async def verify_webhook(request: Request):
12
  params = request.query_params
@@ -18,16 +19,35 @@ async def verify_webhook(request: Request):
18
  return PlainTextResponse(content=challenge)
19
  return PlainTextResponse(content="Verification failed", status_code=403)
20
 
21
- # Lead delivery (POST)
22
  @app.post("/webhook")
23
- async def receive_lead(request: Request):
24
  payload = await request.json()
25
  print("📥 Webhook payload received:")
26
-
27
- # Print with timestamps
28
  gmt_plus_3 = timezone(timedelta(hours=3))
29
- print("Timestamp:", datetime.now(gmt_plus_3).strftime("%Y-%m-%d %H:%M:%S"))
30
 
 
31
  print(payload)
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  return {"status": "ok"}
 
1
  from fastapi import FastAPI, Request
2
  from fastapi.responses import PlainTextResponse
3
  from datetime import datetime, timezone, timedelta
4
+ import requests
5
 
6
  app = FastAPI()
7
 
8
  VERIFY_TOKEN = "my_secure_token"
9
+ PAGE_ACCESS_TOKEN = "EAAJGMsgQEhMBO3RPgdfKCwDGrmiQCZBZADpRZCqqYyxRkKjr0lZCQk3NfNY6qARDSWpjAVgnwJnHEgZAnp5QNKnoNgZCIFi6g149ZBU02TfKXil2P690Q1ZC0fn8jGQYew8MZB62zUIOed9jG9Li4tmpnZBddZBvyLyRlJH6EqvyYA9Bg03HGyMU0UzKZAA6Aexc5H8tWPrNZBE0wkxF2qT0CWxsZD" # Replace with your actual Page Token
10
 
 
11
  @app.get("/webhook")
12
  async def verify_webhook(request: Request):
13
  params = request.query_params
 
19
  return PlainTextResponse(content=challenge)
20
  return PlainTextResponse(content="Verification failed", status_code=403)
21
 
22
+
23
  @app.post("/webhook")
24
+ async def receive_webhook(request: Request):
25
  payload = await request.json()
26
  print("📥 Webhook payload received:")
27
+
 
28
  gmt_plus_3 = timezone(timedelta(hours=3))
29
+ print("Timestamp:", datetime.now(gmt_plus_3).strftime('%Y-%m-%d %H:%M:%S'))
30
 
31
+ # Extract and print the payload
32
  print(payload)
33
 
34
+ try:
35
+ for entry in payload.get("entry", []):
36
+ for change in entry.get("changes", []):
37
+ if change.get("field") == "leadgen":
38
+ lead_id = change["value"]["leadgen_id"]
39
+ print(f"🔍 Lead ID: {lead_id}")
40
+
41
+ # Fetch lead details from Graph API
42
+ url = f"https://graph.facebook.com/v18.0/{lead_id}"
43
+ params = {"access_token": PAGE_ACCESS_TOKEN}
44
+ lead_resp = requests.get(url, params=params)
45
+ lead_data = lead_resp.json()
46
+
47
+ print("📄 Lead Details:")
48
+ print(lead_data)
49
+
50
+ except Exception as e:
51
+ print(f"⚠️ Error processing lead: {e}")
52
+
53
  return {"status": "ok"}