Skip to content

Fix code blocks in Slack messages#1182

Closed
bdalpe wants to merge 4 commits into
mainfrom
bd/slack-message-class
Closed

Fix code blocks in Slack messages#1182
bdalpe wants to merge 4 commits into
mainfrom
bd/slack-message-class

Conversation

@bdalpe

@bdalpe bdalpe commented Jul 31, 2025

Copy link
Copy Markdown
Contributor
  • Add transformation to remove the language marker from code blocks
  • Refactor transformations into new SlackMessage class
  • Add unit tests

bdalpe added 4 commits July 31, 2025 16:39
Remove the language marker on code blocks. Slack does not support this causing extraneous text to be displayed.
Copilot AI review requested due to automatic review settings July 31, 2025 21:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes code blocks in Slack messages by removing language markers that Slack doesn't support, and refactors the message handling logic into a new SlackMessage class for better organization and reusability.

  • Introduces a new transform_code_block function to strip language specifiers from markdown code blocks
  • Refactors message transformations into a centralized SlackMessage class with JSON serialization
  • Updates existing functions to use the new SlackMessage class instead of inline transformations

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
examples/slackbot/src/slackbot/slack.py Adds SlackMessage class and transform_code_block function, refactors existing functions to use the new class
examples/slackbot/tests/test_slack_message_class.py Comprehensive unit tests for the new SlackMessage class functionality
examples/slackbot/.dockerignore Excludes test files from Docker builds

```
"""
# Remove the language specifier from the code block
code_block_pattern = r"```(?:\w+)?(.*?)```"

Copilot AI Jul 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern doesn't properly handle the optional language specifier. The (?:\w+)? makes the language specifier optional but doesn't account for newlines after it. This will fail to match code blocks with language specifiers followed by newlines. Consider using r"```(?:\w+\n)?(.*?)```" to properly capture language specifiers followed by newlines.

Suggested change
code_block_pattern = r"```(?:\w+)?(.*?)```"
code_block_pattern = r"```(?:\w+\n)?(.*?)```"

Copilot uses AI. Check for mistakes.
"https://slack.com/api/chat.update",
headers={"Authorization": f"Bearer {settings.slack_api_token}"},
json={"channel": channel_id, "ts": thread_ts, "text": updated_text},
json=message.json(),

Copilot AI Jul 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The message.json() call will exclude the ts field needed for updating messages. The edit_slack_message function needs to pass the ts parameter to identify which message to update, but the current SlackMessage instance doesn't have the ts set when created with SlackMessage(new_text) in replace mode.

Copilot uses AI. Check for mistakes.
delimiter = "\n\n" if delimiter is None else delimiter
updated_text = f"{current_text}{delimiter}{convert_md_links_to_slack(new_text)}"
message = await SlackMessage.from_message(channel_id, thread_ts)
message.append(new_text, delimiter=delimiter)

Copilot AI Jul 31, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The delimiter parameter is used without being defined in the function signature. The function signature shows delimiter as a parameter, but it's not included in the actual parameter list, which will cause a NameError when delimiter is None and needs to default to "\n\n".

Copilot uses AI. Check for mistakes.
@bdalpe bdalpe requested review from jakekaplan and zzstoatzz July 31, 2025 21:46
@zzstoatzz zzstoatzz mentioned this pull request Aug 8, 2025
@zzstoatzz zzstoatzz closed this Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants