Skip to content

Commit 5994167

Browse files
authored
fix: preserve links and mentions in Slack lists (#10447)
1 parent d8ef9dd commit 5994167

2 files changed

Lines changed: 82 additions & 7 deletions

File tree

‎astrbot/core/platform/sources/slack/slack_adapter.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,25 @@ def _parse_blocks(self, blocks: list) -> list:
297297
list_text = ""
298298
for item in list_items:
299299
if item.get("type") == "rich_text_section":
300-
item_elements = item.get("elements", [])
301-
item_text = ""
302-
for item_element in item_elements:
303-
if item_element.get("type") == "text":
304-
item_text += item_element.get("text", "")
305-
list_text += f"• {item_text}\n"
300+
# Reuse section parsing to preserve inline components.
301+
item_components = self._parse_blocks(
302+
[{"type": "rich_text", "elements": [item]}]
303+
)
304+
list_text += "• "
305+
for component in item_components:
306+
if isinstance(component, Plain):
307+
list_text += component.text
308+
else:
309+
if list_text:
310+
message_components.append(
311+
Plain(text=list_text)
312+
)
313+
list_text = ""
314+
message_components.append(component)
315+
list_text += "\n"
306316

307317
if list_text.strip():
308-
message_components.append(Plain(text=list_text.strip()))
318+
message_components.append(Plain(text=list_text.rstrip()))
309319

310320
elif block_type == "section":
311321
# 处理段落块

‎tests/test_slack_rich_text.py‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,68 @@ def test_slack_quote_keeps_boundaries_around_mentions(before_mention, after_ment
115115
f"Question:\n{before_mention}{after_mention}\nNext paragraph"
116116
)
117117
assert [c.qq for c in components if isinstance(c, At)] == ["UOTHER"]
118+
119+
120+
@pytest.mark.asyncio
121+
@pytest.mark.parametrize("style", ["bullet", "ordered"])
122+
async def test_slack_list_preserves_links_and_mentions(style):
123+
adapter = SlackAdapter(
124+
make_platform_config("slack", bot_token="xoxb-test", app_token="xapp-test"),
125+
{},
126+
asyncio.Queue(),
127+
)
128+
adapter.bot_self_id = "UBOT"
129+
adapter.web_client.users_info = AsyncMock(
130+
return_value={"user": {"real_name": "Tester"}},
131+
)
132+
adapter.web_client.conversations_info = AsyncMock(
133+
return_value={"channel": {"is_im": False, "name": "test"}},
134+
)
135+
url = "https://example.com/slack-list-8642"
136+
received = await adapter.convert_message(
137+
{
138+
"user": "UTEST",
139+
"channel": "CTEST",
140+
"text": f"<@UBOT> Link: {url}",
141+
"blocks": [
142+
{
143+
"type": "rich_text",
144+
"elements": [
145+
{
146+
"type": "rich_text_list",
147+
"style": style,
148+
"elements": [
149+
{
150+
"type": "rich_text_section",
151+
"elements": [
152+
{"type": "user", "user_id": "UBOT"},
153+
{"type": "text", "text": " Link: "},
154+
{"type": "link", "url": url, "text": "Probe"},
155+
{"type": "text", "text": " after"},
156+
],
157+
},
158+
{
159+
"type": "rich_text_section",
160+
"elements": [{"type": "link", "url": url}],
161+
},
162+
{
163+
"type": "rich_text_section",
164+
"elements": [{"type": "user", "user_id": "UOTHER"}],
165+
},
166+
{
167+
"type": "rich_text_section",
168+
"elements": [{"type": "text", "text": "End"}],
169+
},
170+
],
171+
},
172+
],
173+
},
174+
],
175+
},
176+
)
177+
178+
assert [c.qq for c in received.message if isinstance(c, At)] == ["UBOT", "UOTHER"]
179+
assert received.message_str == (
180+
f"• Link: [Probe]({url}) after\n• [{url}]({url})\n• \n• End"
181+
)
182+
assert [type(c) for c in received.message] == [Plain, At, Plain, At, Plain]

0 commit comments

Comments
 (0)