Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -6,11 +6,14 @@ import requests
|
|
| 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 |
@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
|
| 23 |
if code:
|
| 24 |
print(f"🔑 Received code: {code}")
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
"client_id": APP_ID,
|
| 28 |
"redirect_uri": REDIRECT_URI,
|
| 29 |
"client_secret": APP_SECRET,
|
| 30 |
"code": code
|
| 31 |
}
|
| 32 |
-
|
|
|
|
| 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 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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", []))
|