Skip to content

Commit ae87985

Browse files
authored
fix(models): type CardBlock icon/hero_image as image element (not str) (#1937)
1 parent c2eacc2 commit ae87985

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎slack_sdk/models/blocks/blocks.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,8 @@ def __init__(
949949
self,
950950
*,
951951
block_id: Optional[str] = None,
952-
hero_image: Optional[str] = None,
953-
icon: Optional[str] = None,
952+
hero_image: Optional[Union[dict, ImageElement]] = None,
953+
icon: Optional[Union[dict, ImageElement]] = None,
954954
title: Optional[Union[str, dict, TextObject]] = None,
955955
subtitle: Optional[Union[str, dict, TextObject]] = None,
956956
body: Optional[Union[str, dict, TextObject]] = None,
@@ -962,8 +962,10 @@ def __init__(
962962
963963
Args:
964964
block_id: A unique identifier for a block. If not specified, a block_id will be generated.
965-
hero_image: Link to the top image used on the card.
966-
icon: Link to the small image used next to the card's title and subtitle.
965+
hero_image: Link to the top image used on the card. Max length 3000 characters.
966+
The alt_text property has a max length of 2000 characters.
967+
icon: Link to the small image used next to the card's title and subtitle. Max length
968+
3000 characters. The alt_text property has a max length of 2000 characters.
967969
title: Title of the card. 150 characters max.
968970
subtitle: Subtitle of the card. 150 characters max.
969971
body: Content of the card. 200 characters max.

‎tests/slack_sdk/models/test_blocks.py‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
VideoBlock,
4040
)
4141
from slack_sdk.models.blocks.basic_components import FeedbackButtonObject, SlackFile
42-
from slack_sdk.models.blocks.block_elements import FeedbackButtonsElement, IconButtonElement
42+
from slack_sdk.models.blocks.block_elements import FeedbackButtonsElement, IconButtonElement, ImageElement
4343

4444
from . import STRING_3001_CHARS
4545

@@ -1560,10 +1560,14 @@ class CardBlockTests(unittest.TestCase):
15601560
def test_document(self):
15611561
input = {
15621562
"type": "card",
1563-
"icon": "https://picsum.photos/36/36",
1563+
"icon": {"type": "image", "image_url": "https://picsum.photos/36/36", "alt_text": "Icon"},
15641564
"title": {"type": "mrkdwn", "text": "Lumon Industries", "verbatim": False},
15651565
"subtitle": {"type": "mrkdwn", "text": "Committed to work-life balance", "verbatim": False},
1566-
"hero_image": "https://picsum.photos/400/300",
1566+
"hero_image": {
1567+
"type": "image",
1568+
"image_url": "https://picsum.photos/400/300",
1569+
"alt_text": "Sample hero image",
1570+
},
15671571
"body": {"type": "mrkdwn", "text": "Please enjoy each card equally.", "verbatim": False},
15681572
"actions": [
15691573
{
@@ -1575,6 +1579,24 @@ def test_document(self):
15751579
}
15761580
self.assertDictEqual(input, CardBlock(**input).to_dict())
15771581

1582+
def test_image_element_icon_and_hero_image(self):
1583+
block = CardBlock(
1584+
icon=ImageElement(image_url="https://picsum.photos/36/36", alt_text="Icon"),
1585+
hero_image=ImageElement(image_url="https://picsum.photos/400/300", alt_text="Sample hero image"),
1586+
title=MarkdownTextObject(text="Lumon Industries"),
1587+
)
1588+
expected = {
1589+
"type": "card",
1590+
"icon": {"type": "image", "image_url": "https://picsum.photos/36/36", "alt_text": "Icon"},
1591+
"hero_image": {
1592+
"type": "image",
1593+
"image_url": "https://picsum.photos/400/300",
1594+
"alt_text": "Sample hero image",
1595+
},
1596+
"title": {"type": "mrkdwn", "text": "Lumon Industries"},
1597+
}
1598+
self.assertDictEqual(expected, block.to_dict())
1599+
15781600
def test_parse(self):
15791601
input = {
15801602
"type": "card",

0 commit comments

Comments
 (0)