|
1 | 1 | import copy |
2 | 2 | import logging |
3 | 3 | import warnings |
4 | | -from typing import Dict, Sequence, Optional, Set, Union, Any, List |
| 4 | +from typing import Any, Dict, List, Optional, Sequence, Set, Union |
5 | 5 |
|
6 | 6 | from slack_sdk.models import show_unknown_key_warning |
7 | 7 | from slack_sdk.models.basic_objects import ( |
@@ -87,6 +87,8 @@ def parse(cls, block: Union[dict, "Block"]) -> Optional["Block"]: |
87 | 87 | return CallBlock(**block) |
88 | 88 | elif type == HeaderBlock.type: |
89 | 89 | return HeaderBlock(**block) |
| 90 | + elif type == MarkdownBlock.type: |
| 91 | + return MarkdownBlock(**block) |
90 | 92 | elif type == VideoBlock.type: |
91 | 93 | return VideoBlock(**block) |
92 | 94 | elif type == RichTextBlock.type: |
@@ -523,6 +525,45 @@ def _validate_alt_text_length(self): |
523 | 525 | return self.text is None or len(self.text.text) <= self.text_max_length |
524 | 526 |
|
525 | 527 |
|
| 528 | +class MarkdownBlock(Block): |
| 529 | + type = "markdown" |
| 530 | + text_max_length = 12000 |
| 531 | + |
| 532 | + @property |
| 533 | + def attributes(self) -> Set[str]: # type: ignore[override] |
| 534 | + return super().attributes.union({"text"}) |
| 535 | + |
| 536 | + def __init__( |
| 537 | + self, |
| 538 | + *, |
| 539 | + text: str, |
| 540 | + block_id: Optional[str] = None, |
| 541 | + **others: dict, |
| 542 | + ): |
| 543 | + """Displays formatted markdown. |
| 544 | + https://docs.slack.dev/reference/block-kit/blocks/markdown-block/ |
| 545 | +
|
| 546 | + Args: |
| 547 | + block_id: A string acting as a unique identifier for a block. If not specified, one will be generated. |
| 548 | + Maximum length for this field is 255 characters. |
| 549 | + block_id should be unique for each message and each iteration of a message. |
| 550 | + If a message is updated, use a new block_id. |
| 551 | + text (required): The standard markdown-formatted text. Limit 12,000 characters max. |
| 552 | + """ |
| 553 | + super().__init__(type=self.type, block_id=block_id) |
| 554 | + show_unknown_key_warning(self, others) |
| 555 | + |
| 556 | + self.text = text |
| 557 | + |
| 558 | + @JsonValidator("text attribute must be specified") |
| 559 | + def _validate_text(self): |
| 560 | + return self.text != "" |
| 561 | + |
| 562 | + @JsonValidator(f"text attribute cannot exceed {text_max_length} characters") |
| 563 | + def _validate_alt_text_length(self): |
| 564 | + return len(self.text) <= self.text_max_length |
| 565 | + |
| 566 | + |
526 | 567 | class VideoBlock(Block): |
527 | 568 | type = "video" |
528 | 569 | title_max_length = 200 |
|
0 commit comments