# Apache Airflow 接入 Server酱：DAG 任务失败时微信收到告警（2026 教程）

> Apache Airflow 通过官方 apprise provider 接入 Server酱，DAG 或任务运行失败/成功时自动推送微信通知。schan:// 协议直连，AppriseNotifier 回调配置。

- 网页版：https://sct.ftqq.com/docs/integrations/airflow/
- 更新日期：2026-07-17

# Apache Airflow + Server酱：DAG 任务失败时微信收到通知

> 效果：Airflow 的 DAG 或任务运行失败/成功时，微信自动收到推送通知。

## 前置条件

- 已有 Server酱 SendKey（[30 秒获取教程](https://sct.ftqq.com/docs/getting-started/sendkey/)）。schan:// 协议对接 Server酱Turbo，SendKey 需以 `SCT` 开头。
- Airflow 环境可安装 pip 包。如需推送到企业微信/钉钉/飞书群等其他通道，参阅[通道说明](https://sct.ftqq.com/docs/getting-started/channels/)。

## 配置步骤

### 1. 安装 apprise provider

```bash
pip install apache-airflow-providers-apprise
```

### 2. 创建 Apprise 连接

在 Airflow Web UI 中进入 Admin → Connections，新建一个 Apprise 类型连接：

- 连接类型：Apprise
- 连接名称：`serverchan`（自定义，后续 DAG 中引用此值）
- config：`schan://你的SENDKEY`

schan:// 是 Apprise 中 Server酱的专用协议，底层调用 `https://sctapi.ftqq.com/你的SENDKEY.send`。通用 Apprise 接法详见[站内 Apprise 集成指南](https://sct.ftqq.com/docs/integrations/apprise/)。

### 3. 命令行自测（可选）

安装 apprise CLI 后可直接验证连通性：

```bash
pip install apprise && apprise -t "测试" -b "正文" "schan://你的SENDKEY"
```

收到微信推送说明 SendKey 和通道均正常。

### 4. 在 DAG 中配置 AppriseNotifier 回调

将 AppriseNotifier 实例作为 `on_failure_callback` / `on_success_callback` 传入 DAG 或 Task 的 default_args：

```python
from airflow.providers.apprise.notifications.apprise import AppriseNotifier

serverchan_notifier = AppriseNotifier(conn_id="serverchan")

default_args = {
    "on_failure_callback": serverchan_notifier,
    "on_success_callback": serverchan_notifier,
}
```

conn_id 值需与第 2 步中创建的连接名称一致。

## 完整示例

以下 DAG 包含一个会失败的任务，部署后任务失败时微信收到告警：

```python
from datetime import datetime
from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.providers.apprise.notifications.apprise import AppriseNotifier

notifier = AppriseNotifier(conn_id="serverchan")

with DAG(
    dag_id="serverchan_failure_demo",
    start_date=datetime(2026, 1, 1),
    schedule="@daily",
    default_args={"on_failure_callback": notifier},
    catchup=False,
) as dag:
    BashOperator(
        task_id="will_fail",
        bash_command="exit 1",
    )
```

任务因 `exit 1` 失败后，Airflow 调用 AppriseNotifier，经 schan:// 协议向 Server酱 发送请求，微信收到失败告警。

## 常见问题

**免费额度是多少？**

Server酱Turbo 免费额度为每天 5 条，每分钟上限 50 条。订阅会员目前特价最低 3 元/月，一天最多可发 1000 条（[价格详情](https://sct.ftqq.com/subscribe)）。

**任务失败了但没收到微信通知？**

先检查 Airflow 任务日志中 AppriseNotifier 是否被调用、conn_id 是否与连接名称一致。再确认 schan:// 后的 SendKey 是否正确。仍无法解决可参考[常见问题与排查](https://sct.ftqq.com/docs/getting-started/faq/)。

**sctp 开头的 SendKey 能用吗？**

schan:// 协议当前对接 Server酱Turbo 端点 `https://sctapi.ftqq.com`，仅支持 `SCT` 开头的 SendKey。`sctp` 开头的 SendKey 不适用于 schan:// 协议。

**大量任务同时失败会触发太多通知吗？**

Server酱 每分钟上限 50 条。如果 DAG 中有大量并发任务同时失败，建议在回调中做聚合或限制，避免短时间内发送过多请求。

## 相关集成

- [Python 脚本推送](https://sct.ftqq.com/docs/integrations/python/) — 在 Airflow DAG 中直接用 Python 调用 Server酱 API
- [青龙面板任务通知](https://sct.ftqq.com/docs/integrations/qinglong/) — 定时任务平台的 Server酱 接入
- [Uptime Kuma 宕机告警](https://sct.ftqq.com/docs/integrations/uptime-kuma/) — 服务监控场景的微信推送


## 懒人方案：把这段话复制给你的 AI 助手

把下面这段文字原样粘贴给 Claude Code、Cursor 或任何 AI 编程助手，它就会替你完成 Server酱 的接入：

```text
请帮我把 Server酱 微信通知接入到我当前的项目/工具中：
1. API：POST https://sctapi.ftqq.com/{SendKey}.send，参数 title（必填，不能含换行）、desp（正文，支持 Markdown，可选）；成功时返回 JSON 的 code 为 0。
2. 如果我的 SendKey 以 sctp 开头，则改用 https://{uid}.push.ft07.com/send/{SendKey}.send，uid 是 SendKey 中 sctp 与 t 之间的数字。
3. SendKey 从环境变量 SERVERCHAN_SENDKEY 读取；如果没有，提醒我到 https://sct.ftqq.com 免费获取（每天 5 条额度）。
4. 安全要求：不要把 SendKey 硬编码进代码、不要打印到日志、不要提交到 git。
5. 频率约定：任务结束发 1 条结果摘要即可，批量任务合并成一条，不要每步都推。
请根据我当前的项目和场景直接写好代码/配置并验证。
```

