How-to Guide for Slack Operators

Introduction

Slack operators can send text messages (SlackAPIFileOperator) or files (SlackAPIPostOperator) to specified Slack channels. Provide either slack_conn_id or token for the connection, and specify channel (name or ID).

Example Code for Sending Files

The example below demonstrates how to send files to a Slack channel by both specifying file names as well as directly providing file contents. Note that the slack_conn_id, channel, and initial_comment values for the operators are specified as default_args of the DAG.

airflow/providers/slack/example_dags/example_slack.py[source]

with DAG(
    dag_id='slack_example_dag',
    schedule_interval=None,
    start_date=datetime(2021, 1, 1),
    default_args={'slack_conn_id': 'slack', 'channel': '#general', 'initial_comment': 'Hello World!'},
    max_active_runs=1,
    tags=['example'],
) as dag:

    # Send file with filename and filetype
    slack_operator_file = SlackAPIFileOperator(
        task_id="slack_file_upload_1",
        filename="/files/dags/test.txt",
        filetype="txt",
    )

    # Send file content
    slack_operator_file_content = SlackAPIFileOperator(
        task_id="slack_file_upload_2",
        content="file content in txt",
    )

Was this entry helpful?