Skip to content

Attachments

Warning: Deprecated Feature

Attachments, while still accepted by the Slack API, have long (for years now) been considered a deprecated feature.

That said, there is currently no other way to achieve the vertical, colored bars next to content.

Secondary (less important) content can be attached using the deprecated attachments API.

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

Attachment

Lower priority content can be attached to messages using Attachments. This is content that doesn't necessarily need to be seen to appreciate the intent of the message, but perhaps adds further context or additional information.

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

N.B: fields is a deprecated field, included only for legacy purposes. Other legacy fields, e.g. author_name are deliberately omitted as they were never implemented in slackblocks.

Parameters:

Name Type Description Default
blocks Optional[Union[Block, List[Block]]]

an array of Blocks that define the content of the attachment.

None
color Optional[Union[str, Color]]

the color (in hex format, e.g. #ffffff) of the vertical bar to the left of the attachment content. Consider using the Color enum from this module.

None
fields Optional[Union[Field, List[Field]]]

a list of Field objects to be included in what's rendered in the attachment.

None
Throws

InvalidUsageError: if the color code provided is invalid.

Source code in slackblocks/attachments.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
class Attachment:
    """
    Lower priority content can be attached to messages using Attachments.
    This is content that doesn't necessarily need to be seen to appreciate
    the intent of the message, but perhaps adds further context or additional information.

    See <https://api.slack.com/reference/messaging/attachments>.

    N.B: `fields` is a deprecated field, included only for legacy purposes. Other legacy
    fields, e.g. `author_name` are deliberately omitted as they were never implemented in
    `slackblocks`.

    Args:
        blocks: an array of Blocks that define the content of the attachment.
        color: the color (in hex format, e.g. #ffffff) of the vertical bar to the left of the
            attachment content. Consider using the `Color` enum from this module.
        fields: a list of `Field` objects to be included in what's rendered in the attachment.

    Throws:
        InvalidUsageError: if the `color` code provided is invalid.
    """

    def __init__(
        self,
        blocks: Optional[Union[Block, List[Block]]] = None,
        color: Optional[Union[str, Color]] = None,
        fields: Optional[Union[Field, List[Field]]] = None,
    ):
        self.blocks = coerce_to_list(blocks, Block, allow_none=True)
        self.fields = coerce_to_list(fields, Field, allow_none=True)
        if type(color) is Color:
            self.color = color.value
        elif type(color) is str:
            if len(color) == 7 and color.startswith("#") and is_hex(color[1:]):
                self.color = color
            elif len(color) == 6 and is_hex(color):
                self.color = f"#{color}"
            else:
                raise InvalidUsageError(
                    "Color must be a valid hex code (e.g. `#ffffff`)"
                )
        else:
            self.color = None

    def _resolve(self) -> Dict[str, Any]:
        attachment = dict()
        if self.blocks:
            attachment["blocks"] = [block._resolve() for block in self.blocks]
        if self.color:
            attachment["color"] = self.color
        return attachment

    def __repr__(self) -> str:
        return dumps(self._resolve(), indent=4)

Color

Color is a utility class for use with the Slack secondary attachments API.

Pass these to the color argument of Attachment.

Color.GOOD good
Color.WARNING warning
Color.DANGER danger
Color.RED red
Color.BLUE blue
Color.YELLOW yellow
Color.GREEN green
Color.ORANGE orange
Color.PURPLE purple
Color.BLACK black
Source code in slackblocks/attachments.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 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
class Color(Enum):
    """
    Color is a utility class for use with the Slack secondary attachments API.

    Pass these to the `color` argument of
        [`Attachment`](/reference/attachments/#attachments.Attachment).

    <table style="width:50%">
    <tr>
    <td><code>Color.GOOD</code></td>
    <td>
    <img valign='middle' alt='good' src='https://readme-swatches.vercel.app/4CAF50?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.WARNING</code></td>
    <td>
    <img valign='middle' alt='warning' src='https://readme-swatches.vercel.app/FFEB3B?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.DANGER</code></td>
    <td>
    <img valign='middle' alt='danger' src='https://readme-swatches.vercel.app/F44336?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.RED</code></td>
    <td>
    <img valign='middle' alt='red' src='https://readme-swatches.vercel.app/ff0000?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.BLUE</code></td>
    <td>
    <img valign='middle' alt='blue' src='https://readme-swatches.vercel.app/0000ff?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.YELLOW</code></td>
    <td>
    <img valign='middle' alt='yellow' src='https://readme-swatches.vercel.app/ffff00?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.GREEN</code></td>
    <td>
    <img valign='middle' alt='green' src='https://readme-swatches.vercel.app/00ff00?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.ORANGE</code></td>
    <td>
    <img valign='middle' alt='orange' src='https://readme-swatches.vercel.app/ff8800?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.PURPLE</code></td>
    <td>
    <img valign='middle' alt='purple' src='https://readme-swatches.vercel.app/8800ff?style=round'/>
    </td>
    </tr>
    <tr>
    <td><code>Color.BLACK</code></td>
    <td>
    <img valign='middle' alt='black' src='https://readme-swatches.vercel.app/000000?style=round'/>
    </td>
    </tr>
    </table>
    """

    GOOD = "good"
    WARNING = "warning"
    DANGER = "danger"
    RED = "#ff0000"
    BLUE = "#0000ff"
    YELLOW = "#ffff00"
    GREEN = "#00ff00"
    ORANGE = "#ff8800"
    PURPLE = "#8800ff"
    BLACK = "#000000"

    def __repr__(self) -> str:
        return f"<slackblocks Color {self.name}: {self.value}>"

Field

Field text objects for use with Slack's secondary attachment API.

See https://api.slack.com/reference/messaging/attachments#fields.

Parameters:

Name Type Description Default
title Optional[str]

text shown as a bold heading on the field.

None
value Optional[str]

text (mrkdwn or plaintext) representing the value of the field.

None
short Optional[bool]

whether the contents of the field is short enough to be presented in multipe columns.

False
Source code in slackblocks/attachments.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Field:
    """
    Field text objects for use with Slack's secondary attachment API.

    See <https://api.slack.com/reference/messaging/attachments#fields>.

    Args:
        title: text shown as a bold heading on the field.
        value: text (`mrkdwn` or `plaintext`) representing the value of the field.
        short: whether the contents of the field is short enough to be presented in
            multipe columns.
    """

    def __init__(
        self,
        title: Optional[str] = None,
        value: Optional[str] = None,
        short: Optional[bool] = False,
    ):
        self.title = title
        self.value = value
        self.short = short

    def _resolve(self):
        field = dict()
        field["short"] = self.short
        if self.title:
            field["title"] = self.title
        if self.value:
            field["value"] = self.value
        return dumps(field)