How-to Guide for Slack Incoming Webhook notifications¶
Introduction¶
Slack Incoming Webhook notifier (airflow.providers.slack.notifications.slack_webhook.SlackWebhookNotifier
)
allows users to send messages to a slack channel through Incoming Webhook
using the various on_*_callbacks
at both the DAG level and Task level
Example Code:¶
from datetime import datetime, timezone
from airflow import DAG
from airflow.providers.standard.operators.bash import BashOperator
from airflow.providers.slack.notifications.slack_webhook import send_slack_webhook_notification
dag_failure_slack_webhook_notification = send_slack_webhook_notification(
slack_webhook_conn_id="slackwebhook", text="The dag {{ dag.dag_id }} failed"
)
task_failure_slack_webhook_notification = send_slack_webhook_notification(
slack_webhook_conn_id="slackwebhook",
text="The task {{ ti.task_id }} failed",
)
with DAG(
dag_id="mydag",
schedule="@once",
start_date=datetime(2023, 1, 1, tzinfo=timezone.utc),
on_failure_callback=[dag_failure_slack_webhook_notification],
catchup=False,
):
BashOperator(
task_id="mytask", on_failure_callback=[task_failure_slack_webhook_notification], bash_command="fail"
)