新增企业微信、微博、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
42 lines
1.2 KiB
Python
42 lines
1.2 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 get_user_profile(
|
|
user_id: str,
|
|
access_token: str,
|
|
api_version: str = "v24.0",
|
|
app_secret: str = "",
|
|
) -> dict | None:
|
|
url = f"https://graph.facebook.com/{api_version}/{user_id}"
|
|
params = {
|
|
"access_token": access_token,
|
|
"fields": "id,name,work_info,department,location,title,division,organization",
|
|
}
|
|
if app_secret:
|
|
params["appsecret_proof"] = _compute_appsecret_proof(access_token, app_secret)
|
|
|
|
async with httpx.AsyncClient(timeout=30.0) as client:
|
|
try:
|
|
resp = await client.get(url, params=params)
|
|
if resp.status_code == 200:
|
|
return resp.json()
|
|
logger.error("Failed to get user profile %s: %d", user_id, resp.status_code)
|
|
return None
|
|
except httpx.RequestError as exc:
|
|
logger.error("Failed to get user profile %s: %s", user_id, exc)
|
|
return None
|