Slack Utilities#
magistrate.SlackAssistant
#
Bases: Block
A Prefect block for interacting with Slack.
This block provides methods for posting messages to Slack channels and other Slack
API interactions. It attempts to make working with the Slack API easier than using
the slack_sdk or the default Prefect Slack integration.
Attributes:
| Name | Type | Description |
|---|---|---|
token |
SecretStr | str | None
|
Bot user OAuth token for the Slack app used to
perform actions. Can be set as an environment variable with the name
|
file_upload
async
#
file_upload(*, channel_name_or_id: str | None = None, filename: str | None = None, file: str | Path | bytes | None = None, content: str | bytes | None = None, title: str | None = None, alt_text: str | None = None, initial_comment: str | None = None) -> dict
Upload a file to a Slack channel.
For the most part, this method is a thin wrapper around files_upload_v2. That
is a fancy wrapper around files.getUploadURLExternal
and files.completeUploadExternal.
There is no documentation for files_upload_v2 in the slack_sdk library, but
the 3.19.0 release notes
are the best place to look for more information.
files_upload_v2 has a slight delay in uploading files (see slackapi/python-slack-sdk#1521),
so this method polls the Slack API to confirm that the files have actually been
uploaded. This is done by calling files_info on the uploaded file ID until the
file is found or the maximum number of retries is reached. The dict returned by
files_info is returned by this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_name_or_id
|
str | None
|
The name or ID of the channel to
post the message to. If not set, the file will be uploaded but not
published. The file will need to be shared via a link or a call to
|
None
|
filename
|
str | None
|
Name of the file being uploaded. Should
only be used if |
None
|
file
|
str | Path | bytes | None
|
Path to the file upload, or
bytes of the file. Should only be used if |
None
|
content
|
str | bytes | None
|
Content of the file to upload.
Should only be used if |
None
|
title
|
str | None
|
Title of the file uploaded. Defaults to None. |
None
|
alt_text
|
str | None
|
Description of image for screen-reader. Defaults to None. |
None
|
initial_comment
|
str | None
|
The message text introducing the file in specified channels. If None, no text will be posted. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If the file upload fails. |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The response from |
Source code in magistrate/slack.py
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
flow_status_hook
#
Create a Prefect hook that posts flow status updates to a Slack channel.
The hook will post a message to the specified channel whenever the flow run status changes. The message will contain a summary of the flow run status and a link to the flow run in the Prefect UI.
The message will look something like this:
## Prefect Flow Run Status
*Flow:* *Run:*
[Flow Name](https://link-to-flow-in-prefect-ui) [Run Name](https://link-to-flow-run-in-prefect-ui)
*State:*
`State Name`
To use this hook, add it to the flow decorator according to the Prefect documentation:
@flow(
name="my-flow",
on_completion=[SlackAssistant().flow_status_hook("#my-channel")],
on_failure=[SlackAssistant().flow_status_hook("#my-channel")],
on_crashed=[SlackAssistant().flow_status_hook("#my-channel")],
)
def my_flow():
pass
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_name_or_id
|
str
|
The name or ID of the channel to post
the message to. Defaults to |
STATUS_SLACK_CHANNEL
|
Returns:
| Name | Type | Description |
|---|---|---|
FlowStateHook |
FlowStateHook
|
A callable that can be used as a Prefect hook. |
Source code in magistrate/slack.py
get_channel_id
async
#
Get the ID of a Slack channel by name.
This method is a thin wrapper around the conversations_list method of the
Slack API. It retrieves a list of all channels the bot user is a member of, and
returns the ID of the channel with the specified name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_name
|
str
|
The name of the channel to get the ID of. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the channel with the specified name cannot be found. |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The ID of the channel with the specified name. |
Source code in magistrate/slack.py
get_client
#
Create a new Slack web client instance.
Returns:
| Name | Type | Description |
|---|---|---|
AsyncWebClient |
AsyncWebClient
|
A new Slack web client instance. |
get_token
#
Get the bot user OAuth token for the Slack app.
Raises:
| Type | Description |
|---|---|
TypeError
|
If the token is not a |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The bot user OAuth token for the Slack app. |
Source code in magistrate/slack.py
join_channel_if_necessary
async
#
Join a Slack channel if the bot user is not already a member.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_name_or_id
|
str
|
The name or ID of the channel to join. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the bot user could not join the channel. |
Source code in magistrate/slack.py
post_message
async
#
post_message(channel_name_or_id: str, *, text: str | None = None, attachments: str | list[dict | Attachment] | None = None, blocks: str | list[dict | Block] | None = None) -> dict
Post a message to a Slack channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_name_or_id
|
str
|
The name or ID of the channel to post the message to. |
required |
text
|
str | None
|
Standard text to post to the channel. Defaults to None. |
None
|
attachments
|
str | list[dict | Attachment] | None
|
Attachment(s) to post alongside the text or blocks. Defaults to None. |
None
|
blocks
|
str | list[dict | Block] | None
|
Slack Block(s) to post to the channel. Blocks are a more advanced way to format messages in Slack, and can be used to create more complex layouts and interactions than plain text messages or attachments. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The response from the Slack API. |
Source code in magistrate/slack.py
resolve_channel_to_id
async
#
Converts either a channel name or ID to a channel ID.
Slack expects channel IDs for most API calls, so this method is used to make it easier to work with channel names, which are more human-readable.
Channel names should start with a # character, while channel IDs should start
with C07.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel_name_or_id
|
str
|
The channel name or ID to resolve. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is neither a channel name nor ID. |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The resolved channel ID. |