Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions slack_sdk/models/messages/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from slack_sdk.models import show_unknown_key_warning
from slack_sdk.models.basic_objects import JsonObject
from slack_sdk.models.blocks import Block
from slack_sdk.models.blocks.block_elements import UrlSourceElement


Expand Down Expand Up @@ -38,6 +39,8 @@ def parse(cls, chunk: Union[Dict, "Chunk"]) -> Optional["Chunk"]:
return PlanUpdateChunk(**chunk)
elif type == TaskUpdateChunk.type:
return TaskUpdateChunk(**chunk)
elif type == BlocksChunk.type:
return BlocksChunk(**chunk)
Comment on lines +42 to +43
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🧮 quibble: Might this be alright to move up for alphabetical ordering to match other entries?

else:
cls.logger.warning(f"Unknown chunk detected and skipped ({chunk})")
return None
Expand Down Expand Up @@ -132,3 +135,26 @@ def __init__(
self.details = details
self.output = output
self.sources = sources


class BlocksChunk(Chunk):
type = "blocks"

@property
def attributes(self) -> Set[str]: # type: ignore[override]
return super().attributes.union({"blocks"})

def __init__(
self,
*,
blocks: Sequence[Union[Dict, Block]],
**others: Dict,
):
"""Used for passing an array of blocks within a streaming message.

https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming
"""
super().__init__(type=self.type)
show_unknown_key_warning(self, others)

self.blocks = blocks
46 changes: 45 additions & 1 deletion tests/slack_sdk/models/test_chunks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import unittest

from slack_sdk.models.blocks import PlainTextObject, SectionBlock
from slack_sdk.models.blocks.block_elements import UrlSourceElement
from slack_sdk.models.messages.chunk import MarkdownTextChunk, PlanUpdateChunk, TaskUpdateChunk
from slack_sdk.models.messages.chunk import BlocksChunk, Chunk, MarkdownTextChunk, PlanUpdateChunk, TaskUpdateChunk


class MarkdownTextChunkTests(unittest.TestCase):
Expand Down Expand Up @@ -89,3 +90,46 @@ def test_json(self):
],
},
)


class BlocksChunkTests(unittest.TestCase):
def test_json_with_dicts(self):
self.assertDictEqual(
BlocksChunk(
blocks=[
{"type": "section", "text": {"type": "plain_text", "text": "Hello"}},
]
).to_dict(),
{
"type": "blocks",
"blocks": [
{"type": "section", "text": {"type": "plain_text", "text": "Hello"}},
],
},
)

def test_json_with_block_objects(self):
self.assertDictEqual(
BlocksChunk(
blocks=[
SectionBlock(text=PlainTextObject(text="Hello")),
]
).to_dict(),
{
"type": "blocks",
"blocks": [
{"type": "section", "text": {"type": "plain_text", "text": "Hello"}},
],
},
)

def test_parse(self):
chunk = Chunk.parse(
{
"type": "blocks",
"blocks": [{"type": "section", "text": {"type": "plain_text", "text": "Hello"}}],
}
)
self.assertIsInstance(chunk, BlocksChunk)
self.assertEqual(chunk.type, "blocks")
self.assertEqual(len(chunk.blocks), 1)