Skip to content

Messages

Messages are the core unit of Slack messaging functionality. They can be built out using blocks, elements, objects, and rich text features.

See: https://api.slack.com/messaging

Message

A Slack message object that can be converted to a JSON string for use with the Slack message API.

Parameters:

Name Type Description Default
channel str
required
text Optional[str]

markdown text to send in the message. If blocks are provided then this is a fallback to display in notifications.

''
blocks Optional[Union[List[Block], Block]]

a list of Blocks to form the contents of the message instead of the contents of text.

None
attachments Optional[List[Attachment]]

a list of Attachments that form the secondary contents of the message (deprecated).

None
thread_ts Optional[str]

the timestamp ID of another unthreaded message that will become the parent message of this message (now a reply in a thread).

None
mrkdwn bool

if True the contents of text will be rendered as markdown rather than plain text.

True
unfurl_links Optional[bool]

if True, links in the message will be automatically unfurled.

None
unfurl_media Optional[bool]

if True, media from links (e.g. images) will automatically unfurl.

None

Throws: InvalidUsageException: in the event that the items passed to blocks are not valid Blocks.

Source code in slackblocks/messages.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class Message(BaseMessage):
    """
    A Slack message object that can be converted to a JSON string for use with
    the Slack message API.

    Args:
        channel:
        text: markdown text to send in the message. If `blocks` are provided
            then this is a fallback to display in notifications.
        blocks: a list of [`Blocks`](/reference/blocks) to form the contents
            of the message instead of the contents of `text`.
        attachments: a list of
            [`Attachments`](/reference/attachments/#attachments.Attachment)
            that form the secondary contents of the message (deprecated).
        thread_ts: the timestamp ID of another unthreaded message that will
            become the parent message of this message (now a reply in a thread).
        mrkdwn: if `True` the contents of `text` will be rendered as markdown
            rather than plain text.
        unfurl_links: if `True`, links in the message will be automatically
            unfurled.
        unfurl_media: if `True`, media from links (e.g. images) will
            automatically unfurl.
    Throws:
        InvalidUsageException: in the event that the items passed to `blocks`
            are not valid [`Blocks`](/reference/blocks).
    """

    def __init__(
        self,
        channel: str,
        text: Optional[str] = "",
        blocks: Optional[Union[List[Block], Block]] = None,
        attachments: Optional[List[Attachment]] = None,
        thread_ts: Optional[str] = None,
        mrkdwn: bool = True,
        unfurl_links: Optional[bool] = None,
        unfurl_media: Optional[bool] = None,
    ) -> "Message":
        super().__init__(channel, text, blocks, attachments, thread_ts, mrkdwn)
        self.unfurl_links = unfurl_links
        self.unfurl_media = unfurl_media

    def _resolve(self) -> Dict[str, Any]:
        result = {**super()._resolve()}
        if self.unfurl_links is not None:
            result["unfurl_links"] = self.unfurl_links
        if self.unfurl_media is not None:
            result["unfurl_media"] = self.unfurl_media
        return result