8
n8n 中文网amn8n.com

将n8n执行失败警报发送到Slack频道

中级

这是一个DevOps领域的自动化工作流,包含 10 个节点。主要使用 N8n, Set, Code, Slack, Aggregate 等节点。 每小时n8n错误监控与Slack通知

前置要求
  • Slack Bot Token 或 Webhook URL
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "7gbO6txgnBCmDw7c",
  "meta": {
    "instanceId": "3a51cf8d97711c544e4f2cb09ef2b6ab8c441b10f29e0911311ba2722a967418",
    "templateCredsSetupCompleted": true
  },
  "name": "将 n8n 执行失败警报发送到 Slack 频道",
  "tags": [],
  "nodes": [
    {
      "id": "45a1993e-1a89-4077-b258-75c8d4e74bbb",
      "name": "计划触发器",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        900,
        460
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "3491cecc-6ffc-4798-a5f4-a6230408010b",
      "name": "获取工作流",
      "type": "n8n-nodes-base.n8n",
      "position": [
        1360,
        460
      ],
      "parameters": {
        "filters": {
          "activeWorkflows": true
        },
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "id": "w9AYm4viCKUSl2da",
          "name": "N8N"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "4f10d86d-1410-45c6-892a-e113fd4cb2b3",
      "name": "循环",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1580,
        460
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "b06a9bcb-4fc6-488c-bb3e-7cf1f81547ca",
      "name": "n8n",
      "type": "n8n-nodes-base.n8n",
      "position": [
        1780,
        520
      ],
      "parameters": {
        "limit": 1,
        "filters": {
          "status": "error",
          "workflowId": {
            "__rl": true,
            "mode": "id",
            "value": "={{ $json.id }}"
          }
        },
        "options": {
          "activeWorkflows": false
        },
        "resource": "execution",
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "id": "w9AYm4viCKUSl2da",
          "name": "N8N"
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "9012633d-3dcd-4bf5-9645-b90e51129a35",
      "name": "聚合",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        2000,
        520
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "fd91c759-7ee6-45d8-96dd-91eabeb38796",
      "name": "生成消息",
      "type": "n8n-nodes-base.code",
      "position": [
        1780,
        360
      ],
      "parameters": {
        "jsCode": "const list = $input.all().map(item => item.json);\nconsole.log(list)\nlet blocks = [];\n\nfor (const item of list) {\n  if(item.qtdErrors > 0){\n    console.log(item)\n    blocks.push({\n\t\t\t\"type\": \"section\",\n\t\t\t\"text\": {\n\t\t\t\t\"type\": \"mrkdwn\",\n\t\t\t\t\"text\": `:aviso: *${item.workflowName}*\\n *${item.qtdErrors}* errors found in the last run`\n\t\t\t},\n\t\t\t\"accessory\": {\n\t\t\t\t\"type\": \"button\",\n\t\t\t\t\"text\": {\n\t\t\t\t\t\"type\": \"plain_text\",\n\t\t\t\t\t\"text\": \"Vizualize\",\n\t\t\t\t\t\"emoji\": true\n\t\t\t\t},\n\t\t\t\t\"value\": \"click_me_123\",\n\t\t\t\t\"url\": `${item.url}`,\n\t\t\t\t\"action_id\": \"button-action\"\n\t\t\t}\n\t\t})\n  }\n}\n\nif(blocks.length > 0){\n  return {\n    blocks: blocks\n  };\n}else{\n  return null\n}\n"
      },
      "typeVersion": 2
    },
    {
      "id": "51db21fc-6bf1-4652-944f-a00f15f492b5",
      "name": "过滤最近一小时",
      "type": "n8n-nodes-base.code",
      "position": [
        2220,
        520
      ],
      "parameters": {
        "jsCode": "const list = $(\"Aggregate\").last().json.data;\n\nconst lastHour = new Date();\nlastHour.setHours(lastHour.getHours() - 1);\n\n\nconst executionsLastHour = list.filter(execution => {\n  const dataStoppedAt = new Date(execution.stoppedAt);\n\n  return dataStoppedAt > lastHour;\n});\n\nif (executionsLastHour.length > 0) {\n  console.log(`Foram encontrados ${executionsLastHour.length} itens da última hora.`);\n} else {\n  console.log(\"Nenhum item da última hora foi encontrado.\");\n}\n\nreturn {\n  workflowId: $(\"Loop\").last().json.id,\n  workflowName: $(\"Loop\").last().json.name,\n  qtdErrors: executionsLastHour.length,\n  url: $(\"Config\").last().json.urlBase + $(\"Loop\").last().json.id\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "93684cff-609d-4b10-b375-d01724f03a42",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        900,
        240
      ],
      "parameters": {
        "width": 360,
        "height": 200,
        "content": "## Slack - 最近60分钟的错误"
      },
      "typeVersion": 1
    },
    {
      "id": "c11de8c2-1478-49ff-93a8-9cfeacb58507",
      "name": "Slack",
      "type": "n8n-nodes-base.slack",
      "position": [
        2000,
        360
      ],
      "parameters": {
        "text": "=",
        "user": {
          "__rl": true,
          "mode": "list",
          "value": "U05FMSA2610",
          "cachedResultName": "matheus.custodio"
        },
        "select": "user",
        "blocksUi": "={{ $json }}",
        "messageType": "block",
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "id": "dtoLOhzSRbL2Pte5",
          "name": "Slack Automacao"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "28c56721-55d9-448a-8311-57413b0deb84",
      "name": "配置",
      "type": "n8n-nodes-base.set",
      "position": [
        1140,
        460
      ],
      "parameters": {
        "mode": "raw",
        "options": {},
        "jsonOutput": "{\n  \"urlBase\": \"https://n8n.com/workflow/\"\n}\n"
      },
      "typeVersion": 3.4
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "timezone": "America/Sao_Paulo",
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v1",
    "executionTimeout": -1,
    "saveManualExecutions": true
  },
  "versionId": "b71a7834-6b2b-46df-8c1e-911d15b7a3c3",
  "connections": {
    "n8n": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop": {
      "main": [
        [
          {
            "node": "MakeMessage",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "n8n",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Config": {
      "main": [
        [
          {
            "node": "GetWorkflows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "FilterLastHour",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MakeMessage": {
      "main": [
        [
          {
            "node": "Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GetWorkflows": {
      "main": [
        [
          {
            "node": "Loop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FilterLastHour": {
      "main": [
        [
          {
            "node": "Loop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 开发运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量10
分类1
节点类型8
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Matheus Pedrosa

Matheus Pedrosa

@julinho

I am a software engineer specializing in automations, with extensive experience on the N8N platform. With solid skills in JavaScript, Go, .NET, and C#, I am equipped to develop efficient and scalable solutions.

外部链接
在 n8n.io 查看

分享此工作流