新增 Facebook Messenger 渠道扩展,支持在 Yuxi 平台中集成 Messenger 即时通讯渠道。 包含以下功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - actions: 动作处理 - template: 消息模板 - quick_reply: 快捷回复 - private_reply: 私密回复 - handover: 转人工切换 - persona: 人设管理 - profile: 主页配置 - user: 用户信息 - insights: 数据洞察 - notification: 通知推送 - media: 媒体资源处理 - types: 类型定义
124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
import logging
|
|
|
|
import httpx
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
MESSENGER_API_BASE = "https://graph.facebook.com/v22.0"
|
|
|
|
|
|
class MessengerHandoverAPI:
|
|
def __init__(self, page_id: str, access_token: str):
|
|
self._page_id = page_id
|
|
self._access_token = access_token
|
|
|
|
async def pass_thread_control(
|
|
self,
|
|
psid: str,
|
|
target_app_id: str,
|
|
metadata: str = "",
|
|
) -> dict | None:
|
|
url = f"{MESSENGER_API_BASE}/me/pass_thread_control"
|
|
body = {
|
|
"recipient": {"id": psid},
|
|
"target_app_id": target_app_id,
|
|
}
|
|
if metadata:
|
|
body["metadata"] = metadata
|
|
try:
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
|
|
resp = await client.post(
|
|
url,
|
|
params={"access_token": self._access_token},
|
|
json=body,
|
|
)
|
|
if resp.status_code == 200:
|
|
return resp.json()
|
|
logger.error(f"messenger pass_thread_control failed: {resp.status_code} {resp.text}")
|
|
return None
|
|
except Exception:
|
|
logger.exception("messenger pass_thread_control error")
|
|
return None
|
|
|
|
async def take_thread_control(
|
|
self,
|
|
psid: str,
|
|
metadata: str = "",
|
|
) -> dict | None:
|
|
url = f"{MESSENGER_API_BASE}/me/take_thread_control"
|
|
body = {"recipient": {"id": psid}}
|
|
if metadata:
|
|
body["metadata"] = metadata
|
|
try:
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
|
|
resp = await client.post(
|
|
url,
|
|
params={"access_token": self._access_token},
|
|
json=body,
|
|
)
|
|
if resp.status_code == 200:
|
|
return resp.json()
|
|
logger.error(f"messenger take_thread_control failed: {resp.status_code} {resp.text}")
|
|
return None
|
|
except Exception:
|
|
logger.exception("messenger take_thread_control error")
|
|
return None
|
|
|
|
async def request_thread_control(
|
|
self,
|
|
psid: str,
|
|
metadata: str = "",
|
|
) -> dict | None:
|
|
url = f"{MESSENGER_API_BASE}/me/request_thread_control"
|
|
body = {"recipient": {"id": psid}}
|
|
if metadata:
|
|
body["metadata"] = metadata
|
|
try:
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
|
|
resp = await client.post(
|
|
url,
|
|
params={"access_token": self._access_token},
|
|
json=body,
|
|
)
|
|
if resp.status_code == 200:
|
|
return resp.json()
|
|
logger.error(f"messenger request_thread_control failed: {resp.status_code} {resp.text}")
|
|
return None
|
|
except Exception:
|
|
logger.exception("messenger request_thread_control error")
|
|
return None
|
|
|
|
async def get_secondary_receivers(self) -> list[dict] | None:
|
|
url = f"{MESSENGER_API_BASE}/me/secondary_receivers"
|
|
try:
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
|
|
resp = await client.get(
|
|
url,
|
|
params={"access_token": self._access_token},
|
|
)
|
|
if resp.status_code == 200:
|
|
data = resp.json()
|
|
return data.get("data", [])
|
|
logger.error(f"messenger get_secondary_receivers failed: {resp.status_code} {resp.text}")
|
|
return None
|
|
except Exception:
|
|
logger.exception("messenger get_secondary_receivers error")
|
|
return None
|
|
|
|
async def get_thread_owner(self, psid: str) -> str | None:
|
|
url = f"{MESSENGER_API_BASE}/me/thread_owner"
|
|
try:
|
|
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
|
|
resp = await client.get(
|
|
url,
|
|
params={"recipient": psid, "access_token": self._access_token},
|
|
)
|
|
if resp.status_code == 200:
|
|
data = resp.json()
|
|
return data.get("data", [{}])[0].get("owner_app_id")
|
|
logger.error(f"messenger get_thread_owner failed: {resp.status_code} {resp.text}")
|
|
return None
|
|
except Exception:
|
|
logger.exception("messenger get_thread_owner error")
|
|
return None
|