-
Notifications
You must be signed in to change notification settings - Fork 204
Ele 4067 slack messaging integration #1822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d718193
f3754c7
eb8f93f
5ac6acd
3dafa93
bfa84ca
4e6b70f
f17006f
aef974e
a2db2ab
82245a7
a7a38ca
50bc881
46eef40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| from enum import Enum | ||
| from typing import Any, Dict, List, Optional, Union | ||
| from typing import Any, Dict, List, Optional, Sequence, Union | ||
|
|
||
| from pydantic import BaseModel | ||
| from typing_extensions import Literal | ||
|
|
@@ -51,7 +51,32 @@ class IconBlock(BaseInlineTextBlock): | |
| icon: Icon | ||
|
|
||
|
|
||
| InlineBlock = Union[TextBlock, LinkBlock, IconBlock] | ||
| class InlineCodeBlock(BaseInlineTextBlock): | ||
| type: Literal["inline_code"] = "inline_code" | ||
| code: str | ||
|
|
||
|
|
||
| class MentionBlock(BaseInlineTextBlock): | ||
| type: Literal["mention"] = "mention" | ||
| user: str | ||
|
|
||
|
|
||
| class LineBlock(BaseBlock): | ||
| type: Literal["line"] = "line" | ||
| inlines: Sequence["InlineBlock"] | ||
| sep: str = " " | ||
|
|
||
|
|
||
| InlineBlock = Union[ | ||
| TextBlock, | ||
| LinkBlock, | ||
| IconBlock, | ||
| InlineCodeBlock, | ||
| MentionBlock, | ||
| "LineBlock", | ||
| ] | ||
|
|
||
| LineBlock.update_forward_refs() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needed to join a subsection of the line with a different sep (the owners with a ',') |
||
|
|
||
|
|
||
| class HeaderBlock(BaseBlock): | ||
|
|
@@ -68,12 +93,6 @@ class DividerBlock(BaseBlock): | |
| type: Literal["divider"] = "divider" | ||
|
|
||
|
|
||
| class LineBlock(BaseBlock): | ||
| type: Literal["line"] = "line" | ||
| inlines: List[InlineBlock] | ||
| sep: str = " " | ||
|
|
||
|
|
||
| class BaseLinesBlock(BaseBlock): | ||
| lines: List[LineBlock] | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import json | ||
| from typing import Any, Dict, List, Optional, Tuple | ||
| from typing import Any, Callable, List, Optional, Tuple | ||
|
|
||
| from pydantic import BaseModel | ||
| from slack_sdk.models import blocks as slack_blocks | ||
| from tabulate import tabulate | ||
|
|
||
|
|
@@ -14,9 +15,11 @@ | |
| Icon, | ||
| IconBlock, | ||
| InlineBlock, | ||
| InlineCodeBlock, | ||
| LineBlock, | ||
| LinesBlock, | ||
| LinkBlock, | ||
| MentionBlock, | ||
| TableBlock, | ||
| TextBlock, | ||
| TextStyle, | ||
|
|
@@ -31,15 +34,26 @@ | |
| } | ||
|
|
||
|
|
||
| class FormattedBlockKitMessage(BaseModel): | ||
| blocks: List[dict] | ||
| attachments: List[dict] | ||
|
|
||
|
|
||
| ResolveMentionCallback = Callable[[str], Optional[str]] | ||
|
|
||
|
|
||
| class BlockKitBuilder: | ||
| _SECONDARY_FACT_CHUNK_SIZE = 2 | ||
| _LONGEST_MARKDOWN_SUFFIX_LEN = 3 # length of markdown's code suffix (```) | ||
| _MAX_CELL_LENGTH_BY_COLUMN_COUNT = {4: 11, 3: 14, 2: 22, 1: 40, 0: 40} | ||
|
|
||
| def __init__(self) -> None: | ||
| def __init__( | ||
| self, resolve_mention: Optional[ResolveMentionCallback] = None | ||
| ) -> None: | ||
| self._blocks: List[dict] = [] | ||
| self._attachment_blocks: List[dict] = [] | ||
| self._is_divided = False | ||
| self._resolve_mention = resolve_mention or (lambda x: None) | ||
|
|
||
| def _format_icon(self, icon: Icon) -> str: | ||
| return ICON_TO_HTML[icon] | ||
|
|
@@ -59,6 +73,16 @@ def _format_inline_block(self, block: InlineBlock) -> str: | |
| return self._format_text_block(block) | ||
| elif isinstance(block, LinkBlock): | ||
| return f"<{block.url}|{block.text}>" | ||
| elif isinstance(block, InlineCodeBlock): | ||
| return f"`{block.code}`" | ||
| elif isinstance(block, MentionBlock): | ||
| resolved_user = self._resolve_mention(block.user) | ||
| if resolved_user: | ||
| return f"<@{resolved_user}>" | ||
| else: | ||
| return block.user | ||
| elif isinstance(block, LineBlock): | ||
| return self._format_line_block_text(block) | ||
| else: | ||
| raise ValueError(f"Unsupported inline block type: {type(block)}") | ||
|
|
||
|
|
@@ -192,12 +216,6 @@ def _add_expandable_block(self, block: ExpandableBlock) -> None: | |
| Expandable blocks are not supported in Slack Block Kit. | ||
| However, slack automatically collapses a large section block into an expandable block. | ||
| """ | ||
| self._add_block( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding the button label as a title in slack looked really bad, looks better without it |
||
| { | ||
| "type": "section", | ||
| "text": self._format_markdown_section_text(f"*{block.title}*"), | ||
| } | ||
| ) | ||
| self._add_message_blocks(block.body) | ||
|
|
||
| def _add_message_block(self, block: MessageBlock) -> None: | ||
|
|
@@ -239,25 +257,28 @@ def _get_final_blocks( | |
| else: | ||
| return [], self._blocks | ||
|
|
||
| def build(self, message: MessageBody) -> Dict[str, Any]: | ||
| def build(self, message: MessageBody) -> FormattedBlockKitMessage: | ||
| self._blocks = [] | ||
| self._attachment_blocks = [] | ||
| self._add_message_blocks(message.blocks) | ||
| color_code = COLOR_MAP.get(message.color) if message.color else None | ||
| blocks, attachment_blocks = self._get_final_blocks(message.color) | ||
| built_message = { | ||
| "blocks": blocks, | ||
| "attachments": [ | ||
| built_message = FormattedBlockKitMessage( | ||
| blocks=blocks, | ||
| attachments=[ | ||
| { | ||
| "blocks": attachment_blocks, | ||
| } | ||
| ], | ||
| } | ||
| ) | ||
| if color_code: | ||
| built_message["attachments"][0]["color"] = color_code | ||
| for attachment in built_message.attachments: | ||
| attachment["color"] = color_code | ||
| return built_message | ||
|
|
||
|
|
||
| def format_block_kit(message: MessageBody) -> Dict[str, Any]: | ||
| builder = BlockKitBuilder() | ||
| def format_block_kit( | ||
| message: MessageBody, resolve_mention: Optional[ResolveMentionCallback] = None | ||
| ) -> FormattedBlockKitMessage: | ||
| builder = BlockKitBuilder(resolve_mention) | ||
| return builder.build(message) | ||
Uh oh!
There was an error while loading. Please reload this page.