新增快手(Kuaishou)渠道扩展,支持在 Yuxi 平台中集成快手客服渠道。 包含以下功能模块: - api: 快手 API 客户端封装 - accounts: 账户管理 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - signature: 请求签名验证 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - media: 媒体资源处理 - types: 类型定义
94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from .api import KuaishouAPIError
|
|
from .gateway import KuaishouGateway
|
|
from .media import KuaishouMedia
|
|
from .types import OutboundResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
KUAISHOU_IM_SEND_ENDPOINT = "/openapi/im/message/send"
|
|
|
|
|
|
class KuaishouOutbound:
|
|
def __init__(self, gateway: KuaishouGateway, media: KuaishouMedia | None = None):
|
|
self._gateway = gateway
|
|
self._media = media
|
|
|
|
async def send_text(
|
|
self,
|
|
to_user_id: str,
|
|
content: str,
|
|
*,
|
|
reply_to_id: str | None = None,
|
|
thread_id: str | None = None,
|
|
chat_id: str | None = None,
|
|
) -> OutboundResult:
|
|
try:
|
|
token = await self._gateway.client.ensure_token()
|
|
payload = {
|
|
"access_token": token,
|
|
"open_id": to_user_id,
|
|
"msg_type": "text",
|
|
"content": content[:2000],
|
|
}
|
|
if reply_to_id:
|
|
payload["reply_to_msg_id"] = reply_to_id
|
|
if chat_id:
|
|
payload["chat_id"] = chat_id
|
|
|
|
resp = await self._gateway.client.post(
|
|
KUAISHOU_IM_SEND_ENDPOINT,
|
|
json=payload,
|
|
)
|
|
success = resp.get("result") == 1
|
|
return OutboundResult(
|
|
success=success,
|
|
message_id=resp.get("msg_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except KuaishouAPIError as e:
|
|
logger.exception(f"发送快手文本消息失败: {e}")
|
|
return OutboundResult(success=False, error=str(e))
|
|
|
|
async def send_image(
|
|
self,
|
|
to_user_id: str,
|
|
media_id: str,
|
|
*,
|
|
reply_to_id: str | None = None,
|
|
chat_id: str | None = None,
|
|
) -> OutboundResult:
|
|
try:
|
|
token = await self._gateway.client.ensure_token()
|
|
payload = {
|
|
"access_token": token,
|
|
"open_id": to_user_id,
|
|
"msg_type": "image",
|
|
"media_id": media_id,
|
|
}
|
|
if reply_to_id:
|
|
payload["reply_to_msg_id"] = reply_to_id
|
|
if chat_id:
|
|
payload["chat_id"] = chat_id
|
|
|
|
resp = await self._gateway.client.post(
|
|
KUAISHOU_IM_SEND_ENDPOINT,
|
|
json=payload,
|
|
)
|
|
return OutboundResult(
|
|
success=resp.get("result") == 1,
|
|
message_id=resp.get("msg_id", ""),
|
|
raw_response=resp,
|
|
)
|
|
except KuaishouAPIError as e:
|
|
logger.exception(f"发送快手图片消息失败: {e}")
|
|
return OutboundResult(success=False, error=str(e))
|
|
|
|
async def upload_media(self, file_path: str, media_type: str = "image") -> str | None:
|
|
if self._media:
|
|
return await self._media.upload(file_path, media_type)
|
|
return None
|