新增快手(Kuaishou)渠道扩展,支持在 Yuxi 平台中集成快手客服渠道。 包含以下功能模块: - api: 快手 API 客户端封装 - accounts: 账户管理 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - media: 媒体资源处理 - types: 类型定义
35 lines
1.1 KiB
Python
35 lines
1.1 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
from .gateway import KuaishouGateway
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# TODO(P1-1): 当快手开放 IM API 后,替换为官方实际上传端点
|
|
KUAISHOU_IM_UPLOAD_ENDPOINT = "/openapi/im/material/upload"
|
|
|
|
|
|
class KuaishouMedia:
|
|
def __init__(self, gateway: KuaishouGateway):
|
|
self._gateway = gateway
|
|
|
|
async def upload(self, file_path: str | Path, media_type: str = "image") -> str | None:
|
|
try:
|
|
token = await self._gateway.client.ensure_token()
|
|
file_path = Path(file_path)
|
|
|
|
resp = await self._gateway.client._http.post(
|
|
KUAISHOU_IM_UPLOAD_ENDPOINT,
|
|
data={"access_token": token, "media_type": media_type},
|
|
files={"media": (file_path.name, file_path.open("rb"))},
|
|
)
|
|
data = resp.json()
|
|
if data.get("result") == 1:
|
|
return data.get("media_id", "")
|
|
return None
|
|
except Exception:
|
|
logger.exception(f"上传快手素材失败: {file_path}")
|
|
return None
|