Skip to content

Commit beecde2

Browse files
authored
feat(models): add BlockChunk type to chat.{start,append,stop}Stream methods (#1872)
1 parent 73d255a commit beecde2

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

slack_sdk/models/messages/chunk.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from slack_sdk.models import show_unknown_key_warning
55
from slack_sdk.models.basic_objects import JsonObject
6+
from slack_sdk.models.blocks import Block
67
from slack_sdk.models.blocks.block_elements import UrlSourceElement
78

89

@@ -32,7 +33,9 @@ def parse(cls, chunk: Union[Dict, "Chunk"]) -> Optional["Chunk"]:
3233
else:
3334
if "type" in chunk:
3435
type = chunk["type"]
35-
if type == MarkdownTextChunk.type:
36+
if type == BlocksChunk.type:
37+
return BlocksChunk(**chunk)
38+
elif type == MarkdownTextChunk.type:
3639
return MarkdownTextChunk(**chunk)
3740
elif type == PlanUpdateChunk.type:
3841
return PlanUpdateChunk(**chunk)
@@ -132,3 +135,26 @@ def __init__(
132135
self.details = details
133136
self.output = output
134137
self.sources = sources
138+
139+
140+
class BlocksChunk(Chunk):
141+
type = "blocks"
142+
143+
@property
144+
def attributes(self) -> Set[str]: # type: ignore[override]
145+
return super().attributes.union({"blocks"})
146+
147+
def __init__(
148+
self,
149+
*,
150+
blocks: Sequence[Union[Dict, Block]],
151+
**others: Dict,
152+
):
153+
"""Used for passing an array of blocks within a streaming message.
154+
155+
https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming
156+
"""
157+
super().__init__(type=self.type)
158+
show_unknown_key_warning(self, others)
159+
160+
self.blocks = blocks

tests/slack_sdk/models/test_chunks.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import unittest
22

3+
from slack_sdk.models.blocks import PlainTextObject, SectionBlock
34
from slack_sdk.models.blocks.block_elements import UrlSourceElement
4-
from slack_sdk.models.messages.chunk import MarkdownTextChunk, PlanUpdateChunk, TaskUpdateChunk
5+
from slack_sdk.models.messages.chunk import BlocksChunk, Chunk, MarkdownTextChunk, PlanUpdateChunk, TaskUpdateChunk
56

67

78
class MarkdownTextChunkTests(unittest.TestCase):
@@ -89,3 +90,46 @@ def test_json(self):
8990
],
9091
},
9192
)
93+
94+
95+
class BlocksChunkTests(unittest.TestCase):
96+
def test_json_with_dicts(self):
97+
self.assertDictEqual(
98+
BlocksChunk(
99+
blocks=[
100+
{"type": "section", "text": {"type": "plain_text", "text": "Hello"}},
101+
]
102+
).to_dict(),
103+
{
104+
"type": "blocks",
105+
"blocks": [
106+
{"type": "section", "text": {"type": "plain_text", "text": "Hello"}},
107+
],
108+
},
109+
)
110+
111+
def test_json_with_block_objects(self):
112+
self.assertDictEqual(
113+
BlocksChunk(
114+
blocks=[
115+
SectionBlock(text=PlainTextObject(text="Hello")),
116+
]
117+
).to_dict(),
118+
{
119+
"type": "blocks",
120+
"blocks": [
121+
{"type": "section", "text": {"type": "plain_text", "text": "Hello"}},
122+
],
123+
},
124+
)
125+
126+
def test_parse(self):
127+
chunk = Chunk.parse(
128+
{
129+
"type": "blocks",
130+
"blocks": [{"type": "section", "text": {"type": "plain_text", "text": "Hello"}}],
131+
}
132+
)
133+
self.assertIsInstance(chunk, BlocksChunk)
134+
self.assertEqual(chunk.type, "blocks")
135+
self.assertEqual(len(chunk.blocks), 1)

0 commit comments

Comments
 (0)