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

此条目是否有帮助?