Slack API Operators¶
Introduction¶
Slack API operators can post text messages or send files to specified Slack channel(s).
SlackAPIPostOperator¶
Use the SlackAPIPostOperator
to post messages to a Slack channel.
Using the Operator¶
You could send simple text message
slack_operator_post_text = SlackAPIPostOperator(
task_id="slack_post_text",
channel=SLACK_CHANNEL,
text=(
"Apache Airflow® is an open-source platform for developing, "
"scheduling, and monitoring batch-oriented workflows."
),
)
Or you could use Block Kit for create app layouts
slack_operator_post_blocks = SlackAPIPostOperator(
task_id="slack_post_blocks",
channel=SLACK_CHANNEL,
blocks=[
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": (
"*<https://github.com/apache/airflow|Apache Airflow®>* "
"is an open-source platform for developing, scheduling, "
"and monitoring batch-oriented workflows."
),
},
"accessory": {"type": "image", "image_url": IMAGE_URL, "alt_text": "Pinwheel"},
}
],
text="Fallback message",
)
SlackAPIFileOperator¶
Use the SlackAPIFileOperator
to send files to a Slack channel(s).
Using the Operator¶
Note
Operator supports two methods for upload files, which controlled by method_version
,
by default it use Slack SDK method upload_files_v2
it is possible to use legacy upload_files
method by set v1
to method_version
however this not recommended because it
might impact a performance, cause random API errors and is being sunset on March 11, 2025 in addition
beginning May 8, 2024, newly-created apps will be unable to use this API method.
If you previously use v1
you should check that your application has appropriate scopes:
files:write - for write files.
files:read - for read files (not required if you use Slack SDK >= 3.23.0).
channels:read - get list of public channels, for convert Channel Name to Channel ID.
groups:read - get list of private channels, for convert Channel Name to Channel ID
mpim:read - additional permission for API method conversations.list
im:read - additional permission for API method conversations.list
You could send file attachment by specifying file path
slack_operator_file = SlackAPIFileOperator(
task_id="slack_file_upload_1",
channels=SLACK_CHANNEL,
filename="/files/dags/test.txt",
filetype="txt",
)
Or by directly providing file contents
slack_operator_file_content = SlackAPIFileOperator(
task_id="slack_file_upload_2",
channels=SLACK_CHANNEL,
content="file content in txt",
)