- 前端 React+TypeScript (源码+构建产物) - 后端 Python FastAPI - 数据库迁移 001-015 - Docker部署配置 - 微信ClawBot API频道绑定 - 项目文档 README.md
55 lines
2.0 KiB
Python
55 lines
2.0 KiB
Python
"""
|
|
统一会话标识 MessageSession - 参照AstrBot MessageSession设计
|
|
格式: platform:type:session_id
|
|
"""
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
from typing import Optional
|
|
|
|
class SessionType(str, Enum):
|
|
FRIEND = "friend" # 私聊
|
|
GROUP = "group" # 群聊
|
|
CHANNEL = "channel" # 频道/广播
|
|
|
|
@dataclass
|
|
class MessageSession:
|
|
"""统一消息会话标识 - 用于跨平台会话追踪"""
|
|
platform_name: str # wecom/feishu/dingtalk/telegram/slack/whatsapp
|
|
session_type: SessionType
|
|
session_id: str # 会话唯一ID(平台侧的用户ID或群ID)
|
|
|
|
@property
|
|
def unified_msg_origin(self) -> str:
|
|
"""统一消息来源字符串: platform:type:session_id"""
|
|
return f"{self.platform_name}:{self.session_type.value}:{self.session_id}"
|
|
|
|
@classmethod
|
|
def from_str(cls, origin: str) -> "MessageSession":
|
|
"""从格式化字符串解析: platform:type:session_id"""
|
|
parts = origin.split(":", 2)
|
|
if len(parts) != 3:
|
|
raise ValueError(f"无效的消息来源格式: {origin},应为 platform:type:session_id")
|
|
return cls(
|
|
platform_name=parts[0],
|
|
session_type=SessionType(parts[1]),
|
|
session_id=parts[2],
|
|
)
|
|
|
|
@classmethod
|
|
def from_channel_event(cls, platform: str, channel_id: str, user_id: str, group_id: Optional[str] = None) -> "MessageSession":
|
|
"""从频道事件创建"""
|
|
if group_id:
|
|
return cls(platform_name=platform, session_type=SessionType.GROUP, session_id=group_id)
|
|
return cls(platform_name=platform, session_type=SessionType.FRIEND, session_id=user_id)
|
|
|
|
def __str__(self) -> str:
|
|
return self.unified_msg_origin
|
|
|
|
def __hash__(self) -> int:
|
|
return hash(self.unified_msg_origin)
|
|
|
|
def __eq__(self, other) -> bool:
|
|
if isinstance(other, MessageSession):
|
|
return self.unified_msg_origin == other.unified_msg_origin
|
|
return False
|