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¶
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",
)