Mr-Help commited on
Commit
9c3e8e7
·
verified ·
1 Parent(s): 56a1090

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +19 -11
main.py CHANGED
@@ -6,11 +6,14 @@ import requests
6
  app = FastAPI()
7
 
8
  VERIFY_TOKEN = "my_secure_token"
9
- PAGE_ACCESS_TOKEN = "EAAJGMsgQEhMBO0FxGBA1ZBKVIM6HmYYDK9stTrz4XjgN7PDOnkgH3g5WScFZBIVp6ZBXEN4rEsRuelZChxDnELXfojjkXyw9WUcZBYevkinXRIYsdjGZAGQ6QK2MDmnlX6mW4tD4uKzcpKxaaRDzPFouM51Vn6Yx5tatqYJyPBd39z1391QtEzTWWp8CS1BXuBdAJ078l4QF1uQ1ybQ4k4eksZD" # Replace after exchange
10
  APP_ID = "640133872226835"
11
- APP_SECRET = "01602f51d9aa49953064fda7c507ed7c" # Replace with your actual secret
12
  REDIRECT_URI = "https://mr-help-binrushd-meta-authenticator.hf.space/webhook"
13
 
 
 
 
14
  @app.get("/webhook")
15
  async def verify_webhook(request: Request):
16
  params = request.query_params
@@ -19,17 +22,19 @@ async def verify_webhook(request: Request):
19
  token = params.get("hub.verify_token")
20
  challenge = params.get("hub.challenge")
21
 
22
- # OAuth2 token exchange logic
23
  if code:
24
  print(f"🔑 Received code: {code}")
25
- token_url = "https://graph.facebook.com/v22.0/oauth/access_token"
26
- token_params = {
 
27
  "client_id": APP_ID,
28
  "redirect_uri": REDIRECT_URI,
29
  "client_secret": APP_SECRET,
30
  "code": code
31
  }
32
- response = requests.get(token_url, params=token_params)
 
33
  print("🎟️ Token Exchange Response:")
34
  print(response.json())
35
  return PlainTextResponse("Token exchange completed")
@@ -54,16 +59,19 @@ async def receive_webhook(request: Request):
54
  lead_id = change["value"]["leadgen_id"]
55
  print(f"🔍 Lead ID: {lead_id}")
56
 
57
- # Fetch lead details
58
- url = f"https://graph.facebook.com/v18.0/{lead_id}"
59
- params = {"access_token": PAGE_ACCESS_TOKEN}
60
- lead_resp = requests.get(url, params=params)
 
 
 
 
61
  lead_data = lead_resp.json()
62
 
63
  print("📄 Lead Details:")
64
  print(lead_data)
65
 
66
- # Pretty print each field
67
  for item in lead_data.get("field_data", []):
68
  field_name = item.get("name")
69
  field_value = ", ".join(item.get("values", []))
 
6
  app = FastAPI()
7
 
8
  VERIFY_TOKEN = "my_secure_token"
9
+ PAGE_ACCESS_TOKEN = "EAAJGMsgQEhMBO0FxGBA1ZBKVIM6HmYYDK9stTrz4XjgN7PDOnkgH3g5WScFZBIVp6ZBXEN4rEsRuelZChxDnELXfojjkXyw9WUcZBYevkinXRIYsdjGZAGQ6QK2MDmnlX6mW4tD4uKzcpKxaaRDzPFouM51Vn6Yx5tatqYJyPBd39z1391QtEzTWWp8CS1BXuBdAJ078l4QF1uQ1ybQ4k4eksZD"
10
  APP_ID = "640133872226835"
11
+ APP_SECRET = "01602f51d9aa49953064fda7c507ed7c"
12
  REDIRECT_URI = "https://mr-help-binrushd-meta-authenticator.hf.space/webhook"
13
 
14
+ # ✅ رابط GAS WebApp (اللي بينتهي بـ /exec)
15
+ GAS_WEBAPP_URL = "https://script.google.com/macros/s/AKfycbzmFQvZtpwhO6r3aDbBUhYJAb_1N94DMRT4MnZScIsTi9EMzODkHk7ICEyuH77vFv1E/exec"
16
+
17
  @app.get("/webhook")
18
  async def verify_webhook(request: Request):
19
  params = request.query_params
 
22
  token = params.get("hub.verify_token")
23
  challenge = params.get("hub.challenge")
24
 
25
+ # OAuth2 token exchange عبر GAS بدل graph.facebook.com
26
  if code:
27
  print(f"🔑 Received code: {code}")
28
+
29
+ gas_payload = {
30
+ "action": "exchange_code",
31
  "client_id": APP_ID,
32
  "redirect_uri": REDIRECT_URI,
33
  "client_secret": APP_SECRET,
34
  "code": code
35
  }
36
+
37
+ response = requests.post(GAS_WEBAPP_URL, json=gas_payload, timeout=20)
38
  print("🎟️ Token Exchange Response:")
39
  print(response.json())
40
  return PlainTextResponse("Token exchange completed")
 
59
  lead_id = change["value"]["leadgen_id"]
60
  print(f"🔍 Lead ID: {lead_id}")
61
 
62
+ # Fetch lead details عبر GAS بدل graph.facebook.com
63
+ gas_payload = {
64
+ "action": "fetch_lead",
65
+ "lead_id": lead_id,
66
+ "access_token": PAGE_ACCESS_TOKEN
67
+ }
68
+
69
+ lead_resp = requests.post(GAS_WEBAPP_URL, json=gas_payload, timeout=20)
70
  lead_data = lead_resp.json()
71
 
72
  print("📄 Lead Details:")
73
  print(lead_data)
74
 
 
75
  for item in lead_data.get("field_data", []):
76
  field_name = item.get("name")
77
  field_value = ", ".join(item.get("values", []))