diff --git a/block-kit/README.md b/block-kit/README.md index a35a6c0..f53fe13 100644 --- a/block-kit/README.md +++ b/block-kit/README.md @@ -20,7 +20,6 @@ Read the [docs](https://docs.slack.dev/block-kit/) to learn concepts behind thes - **[Input](https://docs.slack.dev/reference/block-kit/blocks/input-block)**: Collects information from users via elements. [Implementation](./src/blocks/input.py). - **[Markdown](https://docs.slack.dev/reference/block-kit/blocks/markdown-block)**: Displays formatted markdown. [Implementation](./src/blocks/markdown.py). - **[Plan](https://docs.slack.dev/reference/block-kit/blocks/plan-block)**: Displays a collection of related tasks. [Implementation](./src/blocks/plan.py). -- **[Rich text](https://docs.slack.dev/reference/block-kit/blocks/rich-text-block)**: Displays formatted, structured representation of text. [Implementation](./src/blocks/rich_text.py). - **[Section](https://docs.slack.dev/reference/block-kit/blocks/section-block)**: Displays text, possibly alongside elements. [Implementation](./src/blocks/section.py). - **[Table](https://docs.slack.dev/reference/block-kit/blocks/table-block)**: Displays structured information in a table. [Implementation](./src/blocks/table.py). - **[Task card](https://docs.slack.dev/reference/block-kit/blocks/task-card-block)**: Displays a single task, representing a single action. [Implementation](./src/blocks/task_card.py). diff --git a/block-kit/src/blocks/rich_text.py b/block-kit/src/blocks/rich_text.py deleted file mode 100644 index d6e56db..0000000 --- a/block-kit/src/blocks/rich_text.py +++ /dev/null @@ -1,321 +0,0 @@ -from slack_sdk.models.blocks import RichTextBlock -from slack_sdk.models.blocks.block_elements import ( - RichTextElementParts, - RichTextListElement, - RichTextPreformattedElement, - RichTextQuoteElement, - RichTextSectionElement, -) - - -def example01() -> list[RichTextBlock]: - """ - Displays formatted, structured representation of text. - https://docs.slack.dev/reference/block-kit/blocks/rich-text-block/ - - Four basic rich text section examples (basic, bold, italic, strikethrough). - """ - blocks = [ - RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Text( - text="Hello there, I am a basic rich text block!" - ) - ] - ) - ] - ), - RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Text(text="Hello there, "), - RichTextElementParts.Text( - text="I am a bold rich text block!", style={"bold": True} - ), - ] - ) - ] - ), - RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Text(text="Hello there, "), - RichTextElementParts.Text( - text="I am an italic rich text block!", - style={"italic": True}, - ), - ] - ) - ] - ), - RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Text(text="Hello there, "), - RichTextElementParts.Text( - text="I am a strikethrough rich text block!", - style={"strike": True}, - ), - ] - ) - ] - ), - ] - return blocks - - -def example02() -> RichTextBlock: - """ - A rich text block with a bullet list. - """ - block = RichTextBlock( - block_id="block1", - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Text( - text="My favorite Slack features (in no particular order):" - ) - ] - ), - RichTextListElement( - style="bullet", - indent=0, - border=1, - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Text(text="Huddles")] - ), - RichTextSectionElement( - elements=[RichTextElementParts.Text(text="Canvas")] - ), - RichTextSectionElement( - elements=[ - RichTextElementParts.Text(text="Developing with Block Kit") - ] - ), - ], - ), - ], - ) - return block - - -def example03() -> RichTextBlock: - """ - A rich text block with a nested bullet list. - """ - block = RichTextBlock( - block_id="block1", - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Text(text="Breakfast foods I enjoy:")] - ), - RichTextListElement( - style="bullet", - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Text(text="Hashbrowns")] - ), - RichTextSectionElement( - elements=[RichTextElementParts.Text(text="Eggs")] - ), - ], - ), - RichTextListElement( - style="bullet", - indent=1, - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Text(text="Scrambled")] - ), - RichTextSectionElement( - elements=[RichTextElementParts.Text(text="Over easy")] - ), - ], - ), - RichTextListElement( - style="bullet", - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Text(text="Pancakes, extra syrup") - ] - ) - ], - ), - ], - ) - return block - - -def example04() -> RichTextBlock: - """ - A rich text block with preformatted code. - """ - block = RichTextBlock( - elements=[ - RichTextPreformattedElement( - border=0, - elements=[ - RichTextElementParts.Text( - text='{\n "object": {\n "description": "this is an example of a json object"\n }\n}' - ) - ], - ) - ] - ) - return block - - -def example05() -> RichTextBlock: - """ - A rich text block with a quote. - """ - block = RichTextBlock( - block_id="Vrzsu", - elements=[ - RichTextQuoteElement( - elements=[ - RichTextElementParts.Text( - text="What we need is good examples in our documentation." - ) - ] - ), - RichTextSectionElement( - elements=[ - RichTextElementParts.Text(text="Yes - I completely agree, Luke!") - ] - ), - ], - ) - return block - - -def example06() -> RichTextBlock: - """ - A rich text block with a broadcast element. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Broadcast(range="everyone")] - ) - ] - ) - return block - - -def example07() -> RichTextBlock: - """ - A rich text block with a color element. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Color(value="#F405B3")] - ) - ] - ) - return block - - -def example08() -> RichTextBlock: - """ - A rich text block with a channel element. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Channel(channel_id="C123ABC456")] - ) - ] - ) - return block - - -def example09() -> RichTextBlock: - """ - A rich text block with a date element. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Date( - timestamp=1720710212, - format="{date_num} at {time}", - fallback="timey", - ) - ] - ) - ] - ) - return block - - -def example10() -> RichTextBlock: - """ - A rich text block with emoji elements. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[ - RichTextElementParts.Emoji(name="basketball"), - RichTextElementParts.Text(text=" "), - RichTextElementParts.Emoji(name="snowboarder"), - RichTextElementParts.Text(text=" "), - RichTextElementParts.Emoji(name="checkered_flag"), - ] - ) - ] - ) - return block - - -def example11() -> RichTextBlock: - """ - A rich text block with a link element. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.Link(url="https://api.slack.com")] - ) - ] - ) - return block - - -def example12() -> RichTextBlock: - """ - A rich text block with a user mention element. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.User(user_id="U123ABC456")] - ) - ] - ) - return block - - -def example13() -> RichTextBlock: - """ - A rich text block with a usergroup mention element. - """ - block = RichTextBlock( - elements=[ - RichTextSectionElement( - elements=[RichTextElementParts.UserGroup(usergroup_id="G123ABC456")] - ) - ] - ) - return block diff --git a/block-kit/tests/blocks/test_rich_text.py b/block-kit/tests/blocks/test_rich_text.py deleted file mode 100644 index 7c4eb20..0000000 --- a/block-kit/tests/blocks/test_rich_text.py +++ /dev/null @@ -1,458 +0,0 @@ -import json - -from src.blocks import rich_text - - -def test_example01(): - blocks = rich_text.example01() - actual = [block.to_dict() for block in blocks] - expected = [ - { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Hello there, I am a basic rich text block!", - }, - ], - }, - ], - }, - { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Hello there, ", - }, - { - "type": "text", - "text": "I am a bold rich text block!", - "style": { - "bold": True, - }, - }, - ], - }, - ], - }, - { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Hello there, ", - }, - { - "type": "text", - "text": "I am an italic rich text block!", - "style": { - "italic": True, - }, - }, - ], - }, - ], - }, - { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Hello there, ", - }, - { - "type": "text", - "text": "I am a strikethrough rich text block!", - "style": { - "strike": True, - }, - }, - ], - }, - ], - }, - ] - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example02(): - block = rich_text.example02() - actual = block.to_dict() - expected = { - "type": "rich_text", - "block_id": "block1", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "My favorite Slack features (in no particular order):", - }, - ], - }, - { - "type": "rich_text_list", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Huddles", - }, - ], - }, - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Canvas", - }, - ], - }, - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Developing with Block Kit", - }, - ], - }, - ], - "style": "bullet", - "indent": 0, - "border": 1, - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example03(): - block = rich_text.example03() - actual = block.to_dict() - expected = { - "type": "rich_text", - "block_id": "block1", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Breakfast foods I enjoy:", - }, - ], - }, - { - "type": "rich_text_list", - "style": "bullet", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Hashbrowns", - }, - ], - }, - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Eggs", - }, - ], - }, - ], - }, - { - "type": "rich_text_list", - "style": "bullet", - "indent": 1, - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Scrambled", - }, - ], - }, - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Over easy", - }, - ], - }, - ], - }, - { - "type": "rich_text_list", - "style": "bullet", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Pancakes, extra syrup", - }, - ], - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example04(): - block = rich_text.example04() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_preformatted", - "elements": [ - { - "type": "text", - "text": '{\n "object": {\n "description": "this is an example of a json object"\n }\n}', - }, - ], - "border": 0, - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example05(): - block = rich_text.example05() - actual = block.to_dict() - expected = { - "type": "rich_text", - "block_id": "Vrzsu", - "elements": [ - { - "type": "rich_text_quote", - "elements": [ - { - "type": "text", - "text": "What we need is good examples in our documentation.", - }, - ], - }, - { - "type": "rich_text_section", - "elements": [ - { - "type": "text", - "text": "Yes - I completely agree, Luke!", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example06(): - block = rich_text.example06() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "broadcast", - "range": "everyone", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example07(): - block = rich_text.example07() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "color", - "value": "#F405B3", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example08(): - block = rich_text.example08() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "channel", - "channel_id": "C123ABC456", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example09(): - block = rich_text.example09() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "date", - "timestamp": 1720710212, - "format": "{date_num} at {time}", - "fallback": "timey", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example10(): - block = rich_text.example10() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "emoji", - "name": "basketball", - }, - { - "type": "text", - "text": " ", - }, - { - "type": "emoji", - "name": "snowboarder", - }, - { - "type": "text", - "text": " ", - }, - { - "type": "emoji", - "name": "checkered_flag", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example11(): - block = rich_text.example11() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "link", - "url": "https://api.slack.com", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example12(): - block = rich_text.example12() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "user", - "user_id": "U123ABC456", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True) - - -def test_example13(): - block = rich_text.example13() - actual = block.to_dict() - expected = { - "type": "rich_text", - "elements": [ - { - "type": "rich_text_section", - "elements": [ - { - "type": "usergroup", - "usergroup_id": "G123ABC456", - }, - ], - }, - ], - } - assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True)