ForcePilot/backend/package/yuxi/channels/adapters/whatsapp/media.py
Kris e9b57546ea feat(whatsapp): 新增WhatsApp适配器完整功能模块
新增大量WhatsApp适配器相关代码,包括账号管理、会话处理、消息收发、验证授权、媒体处理、互动命令、审批流程、健康检测等完整功能模块,搭建基础的Baileys协议WhatsApp接入能力
2026-05-12 00:51:58 +08:00

43 lines
1.0 KiB
Python

from __future__ import annotations
import os
import tempfile
from typing import Any
from yuxi.utils.logging_config import logger
_MEDIA_SUFFIX_MAP: dict[str, str] = {
"image": ".jpg",
"video": ".mp4",
"audio": ".ogg",
"document": "",
"sticker": ".webp",
}
async def download_media(bridge, remote_jid: str, message_id: str, message: dict[str, Any]) -> bytes:
try:
return await bridge.download_media(remote_jid, message_id, message)
except Exception as e:
logger.error(f"Media download failed: {e}")
raise
def make_temp_file(media_type: str, media_data: bytes) -> str:
suffix = _MEDIA_SUFFIX_MAP.get(media_type, "")
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as f:
f.write(media_data)
return f.name
def cleanup_temp_file(filepath: str) -> None:
try:
if os.path.exists(filepath):
os.unlink(filepath)
except OSError:
pass
def supported_media_types() -> list[str]:
return ["image", "video", "audio", "document", "sticker"]