Slack Incoming Webhook 通知使用指南¶
簡介¶
Slack Incoming Webhook 通知器 (airflow.providers.slack.notifications.slack_webhook.SlackWebhookNotifier
) 允許使用者透過 Incoming Webhook,使用 DAG 層級和任務層級的各種 on_*_callbacks
,將訊息發送到 Slack 頻道
範例程式碼:¶
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"
)