Skip to content
Merged
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
28 changes: 27 additions & 1 deletion 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 @@ -32,7 +33,9 @@ def parse(cls, chunk: Union[Dict, "Chunk"]) -> Optional["Chunk"]:
else:
if "type" in chunk:
type = chunk["type"]
if type == MarkdownTextChunk.type:
if type == BlocksChunk.type:
return BlocksChunk(**chunk)
elif type == MarkdownTextChunk.type:
return MarkdownTextChunk(**chunk)
elif type == PlanUpdateChunk.type:
return PlanUpdateChunk(**chunk)
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)