Skip to content

Commit 48e4f7a

Browse files
committed
feat(message): 添加 normalize_message_type 函数以标准化消息类型
1 parent 6b41d76 commit 48e4f7a

4 files changed

Lines changed: 50 additions & 14 deletions

File tree

src/astrbot_sdk/_message_types.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
_GROUP_MESSAGE_TYPES = {"group", "groupmessage", "group_message"}
6+
_PRIVATE_MESSAGE_TYPES = {
7+
"private",
8+
"privatemessage",
9+
"private_message",
10+
"friend",
11+
"friendmessage",
12+
"friend_message",
13+
}
14+
_OTHER_MESSAGE_TYPES = {"other", "othermessage", "other_message"}
15+
16+
17+
def normalize_message_type(
18+
value: Any,
19+
*,
20+
group_id: str | None = None,
21+
user_id: str | None = None,
22+
empty_default: str = "",
23+
) -> str:
24+
"""Collapse SDK-visible message types to canonical values."""
25+
26+
normalized = str(getattr(value, "value", value) or "").strip().lower()
27+
if normalized in _GROUP_MESSAGE_TYPES:
28+
return "group"
29+
if normalized in _PRIVATE_MESSAGE_TYPES:
30+
return "private"
31+
if normalized in _OTHER_MESSAGE_TYPES:
32+
return "other"
33+
if group_id:
34+
return "group"
35+
if user_id:
36+
return "private"
37+
if not normalized:
38+
return empty_default
39+
return "other"

src/astrbot_sdk/context.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
from ._internal.plugin_logger import PluginLogger
4343
from ._internal.star_runtime import current_star_instance
44+
from ._message_types import normalize_message_type
4445
from .clients import (
4546
DBClient,
4647
HTTPClient,
@@ -441,18 +442,7 @@ async def send_message_by_id(
441442

442443
@staticmethod
443444
def _normalize_compat_message_type(value: str) -> str:
444-
normalized = str(value).strip().lower()
445-
if normalized in {"groupmessage", "group_message", "group"}:
446-
return "group"
447-
if normalized in {
448-
"privatemessage",
449-
"private_message",
450-
"private",
451-
"friendmessage",
452-
"friend_message",
453-
"friend",
454-
}:
455-
return "private"
445+
normalized = normalize_message_type(value)
456446
if not normalized:
457447
raise AstrBotError.invalid_input("send_message_by_id requires type")
458448
return normalized

src/astrbot_sdk/events.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from dataclasses import dataclass
1818
from typing import TYPE_CHECKING, Any
1919

20+
from ._message_types import normalize_message_type
2021
from .message.components import (
2122
At,
2223
BaseMessageComponent,
@@ -177,7 +178,11 @@ def __init__(
177178
)
178179
self.self_id = _coerce_str(self_id)
179180
self.platform_id = _coerce_str(platform_id) or normalized_platform
180-
self.message_type = _coerce_str(message_type).lower()
181+
self.message_type = normalize_message_type(
182+
message_type,
183+
group_id=normalized_group_id,
184+
user_id=normalized_user_id,
185+
)
181186
self.sender_name = _coerce_str(sender_name)
182187
self._is_admin = bool(is_admin)
183188
self.raw = raw or {}

src/astrbot_sdk/message/session.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
from dataclasses import dataclass
1717

18+
from .._message_types import normalize_message_type
19+
1820

1921
@dataclass(slots=True)
2022
class MessageSession:
@@ -30,7 +32,7 @@ class MessageSession:
3032

3133
def __post_init__(self) -> None:
3234
self.platform_id = str(self.platform_id)
33-
self.message_type = str(self.message_type).lower()
35+
self.message_type = normalize_message_type(self.message_type)
3436
self.session_id = str(self.session_id)
3537

3638
def __str__(self) -> str:

0 commit comments

Comments
 (0)