ForcePilot/backend/package/yuxi/channel/extensions/viber/media.py
Kris 1c590097be feat(channel): 添加 Twitter 和 Viber 渠道扩展
新增 Twitter 和 Viber 两个渠道扩展。

Twitter 渠道扩展功能模块:
- auth: OAuth 认证管理
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- tweets: 推文管理
- social: 社交互动
- reactions: 表情反应
- media: 媒体资源处理

Viber 渠道扩展功能模块:
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- rate_limiter: 速率限制
- media: 媒体资源处理
2026-05-21 11:57:22 +08:00

46 lines
1.5 KiB
Python

from __future__ import annotations
import logging
import os
import time
import httpx
logger = logging.getLogger(__name__)
class ViberMediaAdapter:
TTL_SECONDS = 3600
def __init__(self, storage_path: str = "/tmp/forcepilot/viber_media"):
self._storage_path = storage_path
os.makedirs(storage_path, exist_ok=True)
async def download(self, media_url: str, message_token: str | None = None) -> str | None:
if not media_url:
return None
try:
prefix = f"{message_token}_" if message_token else ""
filename = f"{prefix}{media_url.rsplit('/', 1)[-1]}"
filepath = f"{self._storage_path}/{filename}"
if os.path.exists(filepath):
mtime = os.path.getmtime(filepath)
age = time.time() - mtime
if age < self.TTL_SECONDS:
logger.debug("Viber media cache hit: %s (age=%.0fs)", filepath, age)
return filepath
logger.info("Viber media cache expired: %s (age=%.0fs)", filepath, age)
async with httpx.AsyncClient(timeout=httpx.Timeout(60.0)) as client:
resp = await client.get(media_url)
resp.raise_for_status()
with open(filepath, "wb") as f:
f.write(resp.content)
logger.info("Viber media downloaded: %s%s", media_url, filepath)
return filepath
except Exception:
logger.exception("Viber media download failed: %s", media_url)
return None