本次提交新增了完整的多渠道消息网关系统,包括: 1. 支持飞书、钉钉、Web、Hook 四种渠道的适配器与配置 2. 领域模型层:消息、会话、绑定、出箱等核心实体 3. 应用服务层:管道、中间件、DTO 与业务逻辑 4. 基础设施层:持久化、过滤器、队列等端口实现 5. 接口层:REST API、SSE、WebSocket 通信端点 6. 前端页面与路由配置,添加渠道管理菜单 7. 新增相关依赖包与 docker-compose 部署配置
96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from yuxi.channel.channels.dingtalk.outbound import DINGTALK_CAPABILITIES, DingTalkOutbound
|
|
from yuxi.channel.channels.dingtalk.translator import DingTalkTranslator
|
|
from yuxi.channel.channels.dingtalk.ws_connection import DingTalkWsConnection
|
|
from yuxi.channel.domain.model.message.dispatch_result import SendResult
|
|
from yuxi.channel.domain.model.message.unified_message import UnifiedMessage
|
|
from yuxi.channel.domain.model.shared.channel_capabilities import ChannelCapabilities
|
|
from yuxi.channel.domain.model.shared.channel_type import ChannelType
|
|
from yuxi.channel.domain.port.ws_connection_port import WsConnectionPort
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class _DingTalkRouteContributor:
|
|
@property
|
|
def router(self) -> object:
|
|
from yuxi.channel.channels.dingtalk.routes import router
|
|
|
|
return router
|
|
|
|
|
|
class DingTalkAdapter:
|
|
def __init__(
|
|
self,
|
|
*,
|
|
client_id: str = "",
|
|
client_secret: str = "",
|
|
ws_enabled: bool = True,
|
|
) -> None:
|
|
self._outbound = DingTalkOutbound(client_id=client_id, client_secret=client_secret)
|
|
self._ws_enabled = ws_enabled
|
|
self._ws: DingTalkWsConnection | None = None
|
|
if ws_enabled and client_id and client_secret:
|
|
self._ws = DingTalkWsConnection(client_id=client_id, client_secret=client_secret)
|
|
self._opened = False
|
|
|
|
@property
|
|
def capabilities(self) -> ChannelCapabilities:
|
|
return DINGTALK_CAPABILITIES
|
|
|
|
@property
|
|
def bot_id(self) -> str:
|
|
return self._outbound.client_id
|
|
|
|
@property
|
|
def channel_type(self) -> str:
|
|
return ChannelType.DINGTALK.value
|
|
|
|
@property
|
|
def ws_connection(self) -> WsConnectionPort | None:
|
|
return self._ws
|
|
|
|
@property
|
|
def route_contributor(self) -> _DingTalkRouteContributor:
|
|
return _DingTalkRouteContributor()
|
|
|
|
@classmethod
|
|
def get_default_config(cls) -> dict:
|
|
return {
|
|
"client_id": "",
|
|
"client_secret": "",
|
|
"ws_enabled": True,
|
|
}
|
|
|
|
async def open(self) -> None:
|
|
await self._outbound.start()
|
|
self._opened = True
|
|
|
|
async def close(self) -> None:
|
|
self._opened = False
|
|
|
|
async def receive_message(self, raw: dict) -> UnifiedMessage:
|
|
return DingTalkTranslator.translate_event(raw)
|
|
|
|
async def send_message(self, session_id: str, content: str, *, channel_type: str, metadata: dict) -> SendResult:
|
|
try:
|
|
ok = await self._outbound.send_text(session_id, content)
|
|
return SendResult(success=ok, error="" if ok else "dingtalk_send_failed")
|
|
except Exception as exc:
|
|
return SendResult(success=False, error=str(exc))
|
|
|
|
async def send_typing(self, session_id: str) -> None:
|
|
pass
|
|
|
|
async def send_media(self, session_id: str, *, url: str, media_type: str, metadata: dict) -> bool:
|
|
logger.warning("send_media not implemented for dingtalk channel")
|
|
return False
|
|
|
|
async def is_healthy(self) -> bool:
|
|
if self._ws is not None:
|
|
return self._ws.is_connected
|
|
return self._opened
|