Skip to content

n8n 工作流集成指南

将本地 n8n 工作流引擎集成到 OpenClaw,实现可视化自动化。

🚀 快速开始

1. 部署 n8n

bash
docker run -d \
  --name n8n \
  --restart unless-stopped \
  -p 5678:5678 \
  -v n8n_data:/home/node/.n8n \
  -e GENERIC_TIMEZONE=Asia/Shanghai \
  n8nio/n8n

访问地址http://localhost:5678

2. 初始设置

  1. 打开浏览器访问 http://localhost:5678
  2. 创建管理员账户
  3. 导入工作流模板

3. 导入工作流

bash
# 一键导入所有工作流
/root/.openclaw/workspace/skills/n8n-workflow/import-workflows.sh

或手动导入:

  1. 在 n8n 界面点击右上角 TemplatesImport from File
  2. 选择工作流 JSON 文件

📦 预置工作流

1. OpenClaw 通知工作流

文件workflow-openclaw-notification.json

用途:接收 Webhook 并通过 OpenClaw 发送 Telegram 通知

触发方式

bash
curl -X POST "http://localhost:5678/webhook/openclaw-notification" \
  -H "Content-Type: application/json" \
  -d '{"message": "Hello from n8n!"}'

工作流结构

Webhook → OpenClaw API → Telegram 消息

2. 每日报告工作流

文件workflow-daily-report.json

用途:每天定时发送统计报告

触发方式:Cron 定时(每 24 小时)

工作流结构

Cron (24h) → Code (生成报告) → OpenClaw API → Telegram 消息

自定义

  • 修改 Cron 间隔
  • 自定义报告内容
  • 添加数据源

3. 监控告警工作流

文件workflow-monitor-alert.json

用途:接收监控数据,超阈值时发送告警

触发方式:Webhook

示例调用

bash
curl -X POST "http://localhost:5678/webhook/monitor-alert" \
  -H "Content-Type: application/json" \
  -d '{
    "server": "prod-01",
    "cpu": 85,
    "memory": 72
  }'

工作流结构

Webhook → IF (CPU >= 80%) → OpenClaw API → 告警消息

🔧 配置说明

OpenClaw API 配置

在 n8n 的 HTTP Request 节点中配置:

参数
MethodPOST
URLhttp://host.docker.internal:18789/message/send
BodyJSON
Content-Typeapplication/json

请求体

json
{
  "target": "telegram:1397306645",
  "message": "={{ $json.message }}"
}

Docker 网络配置

如果 n8n 运行在 Docker 中,需要使用 host.docker.internal 访问宿主机:

bash
# macOS/Windows
http://host.docker.internal:18789

# Linux
# 需要添加额外 hosts 配置
docker run --add-host=host.docker.internal:host-gateway ...

💡 实用场景

场景 1:GitLab CI/CD 通知

GitLab Webhook → 
  n8n 解析事件 → 
  IF 部署成功 → 
  OpenClaw 发送通知

GitLab 配置

yaml
deploy:
  script:
    - echo "Deploying..."
  after_script:
    - curl -X POST "http://n8n:5678/webhook/gitlab" \
      -d "status=$CI_JOB_STATUS&project=$CI_PROJECT_NAME"

场景 2:数据库备份监控

Cron (每天 2:00) → 
  检查备份文件 → 
  IF 文件不存在 → 
  OpenClaw 发送告警

n8n 节点

  1. Cron 节点 - 定时触发
  2. Execute Command - 检查文件
  3. IF 节点 - 判断结果
  4. HTTP Request - 发送通知

场景 3:社交媒体自动发布

RSS Feed (新文章) → 
  n8n 抓取内容 → 
  AI 生成摘要 → 
  发布 Twitter → 
  OpenClaw 记录日志

场景 4:客服工单系统

Email (新工单) → 
  n8n 解析邮件 → 
  创建数据库记录 → 
  OpenClaw 分配客服 → 
  Slack 通知

📊 工作流示例

完整示例:服务器监控

json
{
  "name": "服务器监控",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [{"field": "minutes", "minutesInterval": 5}]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron"
    },
    {
      "parameters": {
        "command": "top -bn1 | grep 'Cpu(s)' | awk '{print $2}'",
        "options": {}
      },
      "name": "Execute Command",
      "type": "n8n-nodes-base.executeCommand"
    },
    {
      "parameters": {
        "conditions": {
          "number": [{
            "value1": "={{ $parseFloat($json.stdout) * 100 }}",
            "operation": "largerEqual",
            "value2": 80
          }]
        }
      },
      "name": "IF",
      "type": "n8n-nodes-base.if"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://host.docker.internal:18789/message/send",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"target\": \"telegram:1397306645\",\n  \"message\": \"🚨 CPU 告警:{{ $parseFloat($json.stdout) * 100 }}%\"\n}"
      },
      "name": "OpenClaw",
      "type": "n8n-nodes-base.httpRequest"
    }
  ],
  "connections": {
    "Cron": {"main": [[{"node": "Execute Command", "type": "main", "index": 0}]]},
    "Execute Command": {"main": [[{"node": "IF", "type": "main", "index": 0}]]},
    "IF": {"main": [[{"node": "OpenClaw", "type": "main", "index": 0}], []]}
  }
}

🔍 故障排查

n8n 无法访问 OpenClaw

问题:HTTP Request 节点连接超时

解决

bash
# 检查 Gateway 状态
openclaw gateway status

# 检查端口
curl http://localhost:18789/healthz

# Docker 网络测试
docker exec n8n curl http://host.docker.internal:18789/healthz

Webhook 不触发

解决

  1. 确认工作流已激活(Activate 开关)
  2. 检查 Webhook URL 是否正确
  3. 查看 n8n 执行日志

Docker 网络问题

Linux 用户需要添加 hosts 配置:

bash
docker run --add-host=host.docker.internal:host-gateway ...

📚 更多资源


🎯 下一步

  1. 导入工作流 - 运行 import-workflows.sh
  2. 配置 API - 设置 OpenClaw 目标用户
  3. 激活工作流 - 在 n8n 界面开启
  4. 测试触发 - 发送测试消息验证

基于 OpenClaw 开源项目