Apache Airflow Slack Provider
The Apache Airflow Slack provider package enables interaction with Slack, offering operators and hooks to send messages, upload files, and integrate with Slack workflows. It includes `SlackOperator` for the Slack API and `SlackWebhookOperator` for incoming webhooks. The current version is 9.9.0, and provider packages typically release independently of core Airflow, often aligning with Airflow's major releases or critical bug fixes.
Warnings
- breaking Prior to Airflow 2.0, Slack operators and hooks were part of `airflow.contrib`. With the introduction of provider packages, all Slack-related components were moved to `airflow.providers.slack`. Importing from `airflow.contrib.operators.slack_operator` or `airflow.contrib.hooks.slack_hook` will lead to `ModuleNotFoundError` in Airflow 2.0+.
- gotcha Operators like `SlackWebhookOperator` and `SlackOperator` rely on correctly configured Airflow Connections. A common mistake is using the wrong connection type (e.g., `Generic` instead of `HTTP` for webhooks, or `Generic` instead of `Slack` for the Slack API) or placing the token/URL in the wrong field (e.g., `Host` for a webhook URL, `Password` for a Slack token).
- gotcha When using `SlackOperator` (which interacts with the Slack API directly), insufficient bot token scopes (permissions) are a frequent cause of `slack_sdk.errors.SlackApiError`.
- gotcha `SlackWebhookOperator` uses Slack's incoming webhooks, which are simpler but have limitations compared to the full Slack API. It does not support features like uploading files, advanced user/channel lookups, or directly interacting with Slack APIs requiring OAuth. For these advanced use cases, `SlackOperator` (or `SlackHook` directly) should be used.
Install
-
pip install apache-airflow-providers-slack
Imports
- SlackOperator
from airflow.providers.slack.operators.slack_operator import SlackOperator
- SlackWebhookOperator
from airflow.providers.slack.operators.slack_webhook import SlackWebhookOperator
- SlackHook
from airflow.providers.slack.hooks.slack import SlackHook
- SlackWebhookHook
from airflow.providers.slack.hooks.slack_webhook import SlackWebhookHook
Quickstart
from airflow.providers.slack.operators.slack_webhook import SlackWebhookOperator
from airflow.models.dag import DAG
from datetime import datetime
# IMPORTANT: For this DAG to send messages, an Airflow Connection named 'slack_webhook_default'
# must be configured. It should be an 'HTTP' connection type, with your Slack Incoming Webhook URL
# placed in the 'Host' field (e.g., https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX).
# For local testing without sending actual messages, you can create a dummy HTTP connection.
with DAG(
dag_id='slack_webhook_quickstart',
start_date=datetime(2023, 1, 1),
schedule=None, # This DAG runs manually
catchup=False,
tags=['slack', 'example'],
) as dag:
send_message_to_slack = SlackWebhookOperator(
task_id='send_message_to_slack',
slack_webhook_conn_id='slack_webhook_default',
message="Hello from Airflow! This is a test message from the quickstart DAG. :airflow:",
channel="#general" # Specify the Slack channel (optional, overrides default webhook channel)
)