ForcePilot/backend/package/yuxi/channel/extensions/workplace/subscriptions.py
Kris 7215418610 feat(channel): 添加企业微信、微博、WhatsApp 和 Workplace 渠道扩展
新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。

企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status

微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status

WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status

Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
2026-05-21 12:01:56 +08:00

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)}