diff --git a/nanobot/channels/slack/runtime.py b/nanobot/channels/slack/runtime.py index df73e923e..81f3e17c8 100644 --- a/nanobot/channels/slack/runtime.py +++ b/nanobot/channels/slack/runtime.py @@ -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: diff --git a/nanobot/channels/slack/tests/test_slack_channel.py b/nanobot/channels/slack/tests/test_slack_channel.py index 69e858e89..1969b40ae 100644 --- a/nanobot/channels/slack/tests/test_slack_channel.py +++ b/nanobot/channels/slack/tests/test_slack_channel.py @@ -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())