52 行
1.6 KiB
Python
52 行
1.6 KiB
Python
"""Platform-neutral identities and fixed notification targets."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from access_policy import PlatformIdentity
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class PlatformTarget:
|
|
"""A target address that AstrBot can use for proactive messages."""
|
|
|
|
platform_id: str
|
|
message_type: str
|
|
session_id: str
|
|
|
|
@property
|
|
def unified_msg_origin(self) -> str:
|
|
return f"{self.platform_id}:{self.message_type}:{self.session_id}"
|
|
|
|
|
|
def target_from_config(
|
|
platform_id: str,
|
|
*,
|
|
group_id: str = "",
|
|
user_id: str = "",
|
|
) -> PlatformTarget | None:
|
|
"""Build a fixed group or private target from configured identifiers."""
|
|
normalized_platform = str(platform_id or "").strip()
|
|
normalized_group = str(group_id or "").strip()
|
|
normalized_user = str(user_id or "").strip()
|
|
if not normalized_platform:
|
|
return None
|
|
if normalized_group:
|
|
return PlatformTarget(normalized_platform, "GroupMessage", normalized_group)
|
|
if normalized_user:
|
|
return PlatformTarget(normalized_platform, "FriendMessage", normalized_user)
|
|
return None
|
|
|
|
|
|
def parse_unified_msg_origin(value: str) -> PlatformTarget | None:
|
|
"""Parse AstrBot's platform/type/session address."""
|
|
parts = str(value or "").split(":", 2)
|
|
if len(parts) != 3 or not all(parts):
|
|
return None
|
|
return PlatformTarget(parts[0], parts[1], parts[2])
|
|
|
|
|
|
def is_supported_platform(identity: PlatformIdentity) -> bool:
|
|
"""Return whether the normalized identity is a supported target platform."""
|
|
return identity.platform_name in {"aiocqhttp", "telegram"}
|