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
|
the Slack channel to send the message to, e.g. "#general".
|
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 | class Message(BaseMessage):
"""
A Slack message object that can be converted to a JSON string for use with
the Slack message API.
Args:
channel: the Slack channel to send the message to, e.g. "#general".
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`](/slackblocks/latest/reference/blocks) to form the contents
of the message instead of the contents of `text`.
attachments: a list of
[`Attachments`](/slackblocks/latest/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`](/slackblocks/latest/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
|
ResponseType
Types of messages that can be sent via WebhookMessage
.
Source code in slackblocks/messages.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 | class ResponseType(Enum):
"""
Types of messages that can be sent via `WebhookMessage`.
"""
EPHEMERAL = "ephemeral"
IN_CHANNEL = "in_channel"
@staticmethod
def get_value(value: Union["ResponseType", str]) -> str:
if isinstance(value, ResponseType):
return value.value
if value not in [response_type.value for response_type in ResponseType]:
raise InvalidUsageError(
"ResponseType must be either `ephemeral` or `in_channel`"
)
return value
|
WebhookMessage
Messages sent via the Slack WebhookClient
takes different arguments than
those sent via the regular WebClient
.
See: https://github.com/slackapi/python-slack-sdk/blob/7e71b73/slack_sdk/webhook/client.py#L28
Parameters:
Name |
Type |
Description |
Default |
text |
Optional[str]
|
markdown text to send in the message. If blocks are provided
then this is a fallback to display in notifications.
|
None
|
attachments |
Optional[List[Attachment]]
|
a list of
Attachments
that form the secondary contents of the message (deprecated).
|
None
|
blocks |
Optional[Union[List[Block], Block]]
|
a list of Blocks to form the contents
of the message instead of the contents of text .
|
None
|
response_type |
Union[ResponseType, str]
|
one of ResponseType.EPHEMERAL or ResponseType.IN_CHANNEL .
Ephemeral messages are shown only to the requesting user whereas
"in-channel" messages are shown to all channel participants.
|
None
|
replace_orginal |
|
when True , the message triggering this response will be
replaced by this messaage. Mutually exclusive with delete_original .
|
required
|
delete_original |
Optional[bool]
|
when True , the original message triggering this response
will be deleted, and any content of this message will be posted as a
new message. Mutually exclusive with replace_orginal .
|
None
|
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
|
metadata |
Optional[Dict[str, Any]]
|
additional metadata to attach to the message.
|
None
|
headres |
|
HTTP request headers to include with the message.
|
required
|
Throws
InvalidUsageError: when any of the passed fields fail validation.
Source code in slackblocks/messages.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 | class WebhookMessage:
"""
Messages sent via the Slack `WebhookClient` takes different arguments than
those sent via the regular `WebClient`.
See: <https://github.com/slackapi/python-slack-sdk/blob/7e71b73/slack_sdk/webhook/client.py#L28>
Args:
text: markdown text to send in the message. If `blocks` are provided
then this is a fallback to display in notifications.
attachments: a list of
[`Attachments`](/slackblocks/latest/reference/attachments/#attachments.Attachment)
that form the secondary contents of the message (deprecated).
blocks: a list of [`Blocks`](/slackblocks/latest/reference/blocks) to form the contents
of the message instead of the contents of `text`.
response_type: one of `ResponseType.EPHEMERAL` or `ResponseType.IN_CHANNEL`.
Ephemeral messages are shown only to the requesting user whereas
"in-channel" messages are shown to all channel participants.
replace_orginal: when `True`, the message triggering this response will be
replaced by this messaage. Mutually exclusive with `delete_original`.
delete_original: when `True`, the original message triggering this response
will be deleted, and any content of this message will be posted as a
new message. Mutually exclusive with `replace_orginal`.
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.
metadata: additional metadata to attach to the message.
headres: HTTP request headers to include with the message.
Throws:
InvalidUsageError: when any of the passed fields fail validation.
"""
def __init__(
self,
text: Optional[str] = None,
attachments: Optional[List[Attachment]] = None,
blocks: Optional[Union[List[Block], Block]] = None,
response_type: Union[ResponseType, str] = None,
replace_original: Optional[bool] = None,
delete_original: Optional[bool] = None,
unfurl_links: Optional[bool] = None,
unfurl_media: Optional[bool] = None,
metadata: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, str]] = None,
) -> "WebhookMessage":
self.text = text
self.attachments = coerce_to_list(attachments, Attachment, allow_none=True)
self.blocks = coerce_to_list(blocks, Block, allow_none=True)
self.response_type = ResponseType.get_value(response_type)
self.replace_original = replace_original
self.delete_original = delete_original
self.unfurl_links = unfurl_links
self.unfurl_media = unfurl_media
self.metadata = metadata
self.headers = headers
def _resolve(self) -> None:
webhook_message = {}
if self.text is not None:
webhook_message["text"] = self.text
if self.attachments is not None:
webhook_message["attachments"] = [
attachment._resolve() for attachment in self.attachments
]
if self.blocks is not None:
webhook_message["blocks"] = [block._resolve() for block in self.blocks]
if self.response_type is not None:
webhook_message["response_type"] = self.response_type
if self.replace_original is not None:
webhook_message["replace_original"] = self.replace_original
if self.delete_original is not None:
webhook_message["delete_original"] = self.delete_original
if self.unfurl_links is not None:
webhook_message["unfurl_links"] = self.unfurl_links
if self.unfurl_media is not None:
webhook_message["unfurl_media"] = self.unfurl_media
if self.metadata is not None:
webhook_message["metadata"] = self.metadata
if self.headers is not None:
webhook_message["headers"] = self.headers
return webhook_message
def to_dict(self) -> Dict[str, Any]:
return self._resolve()
def json(self) -> str:
return dumps(self._resolve(), indent=4)
def __repr__(self) -> str:
return self.json()
def __getitem__(self, item):
return self._resolve()[item]
def keys(self) -> Dict[str, Any]:
return self._resolve().keys()
|