fix(slack): keep full text of button messages beyond 3000 chars
When buttons are attached, Slack renders the blocks instead of the text field, and the only section block carried text[:3000]. Anything past 3000 characters of the final chunk (up to 39000) was silently dropped. Split the chunk across multiple section blocks that each respect the Block Kit limit.
This commit is contained in:
1 parent
2d52db2478
commit
a3ef95f725
2 files changed
+34
-1
No files matched your search
@@ -87,6 +87,7 @@ class SlackConfig(Base):
|
||||
|
||||
|
||||
SLACK_MAX_MESSAGE_LEN = 39_000 # Slack API allows ~40k; leave margin
|
||||
SLACK_SECTION_TEXT_MAX_LEN = 3_000 # Block Kit section text limit
|
||||
SLACK_DOWNLOAD_TIMEOUT = 30.0
|
||||
# Abort Socket Mode WSS handshake after this many seconds. REST auth_test can still
|
||||
# succeed while WSS blocks (firewall / region). slack-sdk does not apply HTTP(S)_PROXY
|
||||
@@ -739,8 +740,12 @@ class SlackChannel(BaseChannel):
|
||||
@staticmethod
|
||||
def _build_button_blocks(text: str, buttons: list[list[str]]) -> list[dict[str, Any]]:
|
||||
"""Build Slack Block Kit blocks with action buttons."""
|
||||
# Slack renders ``blocks`` instead of ``text`` and caps each section's
|
||||
# text at 3000 chars, so split the chunk across sections rather than
|
||||
# dropping everything past the first 3000 chars.
|
||||
blocks: list[dict[str, Any]] = [
|
||||
{"type": "section", "text": {"type": "mrkdwn", "text": text[:3000]}},
|
||||
{"type": "section", "text": {"type": "mrkdwn", "text": part}}
|
||||
for part in split_message(text, SLACK_SECTION_TEXT_MAX_LEN)
|
||||
]
|
||||
elements: list[dict[str, Any]] = []
|
||||
for row in buttons:
|
||||
|
||||
@@ -248,6 +248,34 @@ async def test_send_renders_buttons_on_last_message_chunk() -> None:
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_with_buttons_keeps_text_beyond_section_limit() -> None:
|
||||
"""Button messages render via blocks, so every char must land in a section."""
|
||||
channel = SlackChannel(SlackConfig(enabled=True), MessageBus())
|
||||
fake_web = _FakeAsyncWebClient()
|
||||
channel._web_client = fake_web
|
||||
lines = [f"line {i:04d} " + "x" * 40 for i in range(200)]
|
||||
|
||||
await channel.send(
|
||||
OutboundMessage(
|
||||
channel="slack",
|
||||
chat_id="C123",
|
||||
content="\n".join(lines),
|
||||
buttons=[["Yes", "No"]],
|
||||
)
|
||||
)
|
||||
|
||||
assert len(fake_web.chat_post_calls) == 1
|
||||
blocks = fake_web.chat_post_calls[0]["blocks"]
|
||||
assert isinstance(blocks, list)
|
||||
sections = [block for block in blocks if block["type"] == "section"]
|
||||
assert len(sections) > 1
|
||||
assert all(len(section["text"]["text"]) <= 3000 for section in sections)
|
||||
rendered = "\n".join(section["text"]["text"] for section in sections)
|
||||
assert rendered == "\n".join(lines)
|
||||
assert blocks[-1]["type"] == "actions"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_updates_reaction_when_final_response_sent() -> None:
|
||||
channel = SlackChannel(SlackConfig(enabled=True, react_emoji="eyes"), MessageBus())
|
||||
|
||||
Reference in new issue
Block a user