85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
|
|
import hashlib
|
||
|
|
import hmac
|
||
|
|
import logging
|
||
|
|
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
def _compute_appsecret_proof(access_token: str, app_secret: str) -> str:
|
||
|
|
return hmac.new(
|
||
|
|
app_secret.encode("utf-8"),
|
||
|
|
access_token.encode("utf-8"),
|
||
|
|
hashlib.sha256,
|
||
|
|
).hexdigest()
|
||
|
|
|
||
|
|
|
||
|
|
async def list_subscriptions(
|
||
|
|
app_id: str,
|
||
|
|
app_token: str,
|
||
|
|
api_version: str = "v24.0",
|
||
|
|
) -> list[dict]:
|
||
|
|
url = f"https://graph.facebook.com/{api_version}/{app_id}/subscriptions"
|
||
|
|
params = {"access_token": app_token}
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=15.0) as client:
|
||
|
|
try:
|
||
|
|
resp = await client.get(url, params=params)
|
||
|
|
if resp.status_code == 200:
|
||
|
|
data = resp.json()
|
||
|
|
return data.get("data", [])
|
||
|
|
logger.error("Failed to list subscriptions: %d %s", resp.status_code, resp.text[:200])
|
||
|
|
return []
|
||
|
|
except httpx.RequestError as exc:
|
||
|
|
logger.error("Failed to list subscriptions: %s", exc)
|
||
|
|
return []
|
||
|
|
|
||
|
|
|
||
|
|
async def subscribe_page(
|
||
|
|
page_id: str,
|
||
|
|
access_token: str,
|
||
|
|
api_version: str = "v24.0",
|
||
|
|
app_secret: str = "",
|
||
|
|
) -> dict:
|
||
|
|
url = f"https://graph.facebook.com/{api_version}/{page_id}/subscribed_apps"
|
||
|
|
params = {"access_token": access_token}
|
||
|
|
if app_secret:
|
||
|
|
params["appsecret_proof"] = _compute_appsecret_proof(access_token, app_secret)
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=15.0) as client:
|
||
|
|
try:
|
||
|
|
resp = await client.post(url, params=params)
|
||
|
|
if resp.status_code == 200:
|
||
|
|
logger.info("Page %s subscribed successfully", page_id)
|
||
|
|
return {"success": True, "data": resp.json()}
|
||
|
|
logger.error("Failed to subscribe page %s: %d %s", page_id, resp.status_code, resp.text[:200])
|
||
|
|
return {"success": False, "error": resp.text[:200]}
|
||
|
|
except httpx.RequestError as exc:
|
||
|
|
logger.error("Failed to subscribe page %s: %s", page_id, exc)
|
||
|
|
return {"success": False, "error": str(exc)}
|
||
|
|
|
||
|
|
|
||
|
|
async def unsubscribe_page(
|
||
|
|
page_id: str,
|
||
|
|
access_token: str,
|
||
|
|
api_version: str = "v24.0",
|
||
|
|
app_secret: str = "",
|
||
|
|
) -> dict:
|
||
|
|
url = f"https://graph.facebook.com/{api_version}/{page_id}/subscribed_apps"
|
||
|
|
params = {"access_token": access_token}
|
||
|
|
if app_secret:
|
||
|
|
params["appsecret_proof"] = _compute_appsecret_proof(access_token, app_secret)
|
||
|
|
|
||
|
|
async with httpx.AsyncClient(timeout=15.0) as client:
|
||
|
|
try:
|
||
|
|
resp = await client.delete(url, params=params)
|
||
|
|
if resp.status_code == 200:
|
||
|
|
logger.info("Page %s unsubscribed successfully", page_id)
|
||
|
|
return {"success": True, "data": resp.json()}
|
||
|
|
logger.error("Failed to unsubscribe page %s: %d %s", page_id, resp.status_code, resp.text[:200])
|
||
|
|
return {"success": False, "error": resp.text[:200]}
|
||
|
|
except httpx.RequestError as exc:
|
||
|
|
logger.error("Failed to unsubscribe page %s: %s", page_id, exc)
|
||
|
|
return {"success": False, "error": str(exc)}
|