ForcePilot/backend/package/yuxi/channel/extensions/wechat-kf/media.py
Kris 87a8931db3 feat(channel): 添加微信客服、微信公众号和微信支付通知渠道扩展
新增微信客服、微信公众号、微信支付通知三个渠道扩展。

微信客服渠道扩展功能模块:
- account: 账户管理
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- crypto: 加解密处理
- dedupe: 消息去重
- customer: 客户管理
- servicer: 客服管理
- session: 会话管理
- status: 会话状态管理
- media: 媒体资源处理
- statistics: 统计功能
- sync: 数据同步
- upgrade: 升级处理

微信公众号渠道扩展功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- crypto: 加解密处理
- dedupe: 消息去重
- passive_reply: 被动回复
- message: 消息处理
- broadcast: 群发消息
- template: 模板消息
- menu: 菜单管理
- qrcode: 二维码管理
- user: 用户管理
- media: 媒体资源处理
- status: 会话状态管理

微信支付通知渠道扩展功能模块:
- config: 渠道配置管理
- webhook: Webhook 事件处理
- crypto: 加解密与签名校验
- cert_manager: 证书管理
- event_router: 事件路由
- dedupe: 消息去重
- pay_repo: 支付数据仓库
- query_client: 查询客户端
- arq_tasks: 异步任务
- callback_compensator: 回调补偿
2026-05-21 12:00:30 +08:00

58 lines
1.8 KiB
Python

import os
import tempfile
import httpx
UPLOAD_URL = "/cgi-bin/media/upload"
DOWNLOAD_URL = "/cgi-bin/media/get"
ALLOWED_TYPES = {"image", "voice", "video", "file"}
TYPE_MAX_SIZES = {
"image": 2 * 1024 * 1024,
"voice": 2 * 1024 * 1024,
"video": 10 * 1024 * 1024,
"file": 20 * 1024 * 1024,
}
class WeChatKFMedia:
def __init__(self, gateway):
self._gateway = gateway
async def upload(self, file_path: str, media_type: str = "image") -> dict:
if media_type not in ALLOWED_TYPES:
return {"errcode": -1, "errmsg": f"不支持的类型: {media_type}"}
token = await self._gateway.get_access_token()
with open(file_path, "rb") as f:
resp = await self._gateway._http.post(
UPLOAD_URL,
params={"access_token": token, "type": media_type},
files={"media": (file_path.split("/")[-1], f)},
)
return resp.json()
async def download(self, media_id: str) -> bytes | None:
token = await self._gateway.get_access_token()
resp = await self._gateway._http.get(
DOWNLOAD_URL,
params={"access_token": token, "media_id": media_id},
)
if resp.status_code == 200:
return resp.content
return None
async def upload_from_url(self, image_url: str, media_type: str = "image") -> dict:
async with httpx.AsyncClient() as client:
resp = await client.get(image_url)
if resp.status_code != 200:
return {"errcode": -1, "errmsg": "下载图片失败"}
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
f.write(resp.content)
tmp_path = f.name
try:
return await self.upload(tmp_path, media_type)
finally:
os.unlink(tmp_path)