35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
|
|
import logging
|
||
|
|
|
||
|
|
import httpx
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
MESSENGER_API_BASE = "https://graph.facebook.com/v22.0"
|
||
|
|
DEFAULT_PROFILE_FIELDS = "first_name,last_name,profile_pic,locale,timezone,gender"
|
||
|
|
|
||
|
|
|
||
|
|
class MessengerUserAPI:
|
||
|
|
def __init__(self, access_token: str):
|
||
|
|
self._access_token = access_token
|
||
|
|
|
||
|
|
async def get_user_profile(
|
||
|
|
self,
|
||
|
|
psid: str,
|
||
|
|
fields: str = DEFAULT_PROFILE_FIELDS,
|
||
|
|
) -> dict | None:
|
||
|
|
url = f"{MESSENGER_API_BASE}/{psid}"
|
||
|
|
params = {
|
||
|
|
"fields": fields,
|
||
|
|
"access_token": self._access_token,
|
||
|
|
}
|
||
|
|
try:
|
||
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client:
|
||
|
|
resp = await client.get(url, params=params)
|
||
|
|
if resp.status_code == 200:
|
||
|
|
return resp.json()
|
||
|
|
logger.warning(f"messenger user profile failed: {resp.status_code} {resp.text}")
|
||
|
|
return None
|
||
|
|
except Exception:
|
||
|
|
logger.exception("messenger user profile error")
|
||
|
|
return None
|