新增大量WhatsApp适配器相关代码,包括账号管理、会话处理、消息收发、验证授权、媒体处理、互动命令、审批流程、健康检测等完整功能模块,搭建基础的Baileys协议WhatsApp接入能力
43 lines
1.0 KiB
Python
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"]
|