{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "5354e780-a6cb-4ca9-8e87-b8ef4aafc2a9",
      "name": "Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "position": [
        0,
        0
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "6cc196e2-ad87-4442-b378-35cfa7fcd3d5",
      "name": "Set Context",
      "type": "n8n-nodes-base.set",
      "position": [
        240,
        0
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "cfg-001",
              "name": "n8n_instance_url",
              "type": "string",
              "value": "https://n8n.YOUR-DOMAIN.com"
            },
            {
              "id": "cfg-002",
              "name": "n8n_api_key",
              "type": "string",
              "value": "YOUR_N8N_API_KEY_HERE"
            },
            {
              "id": "cfg-003",
              "name": "telegram_chat_id",
              "type": "string",
              "value": "YOUR_TELEGRAM_CHAT_ID"
            },
            {
              "id": "auto-001",
              "name": "error_workflow_id",
              "type": "string",
              "value": "={{ $('Error Trigger').item.json.workflow.id }}"
            },
            {
              "id": "auto-002",
              "name": "error_workflow_name",
              "type": "string",
              "value": "={{ $('Error Trigger').item.json.workflow.name }}"
            },
            {
              "id": "auto-003",
              "name": "error_node",
              "type": "string",
              "value": "={{ $('Error Trigger').item.json.execution.lastNodeExecuted }}"
            },
            {
              "id": "auto-004",
              "name": "error_message",
              "type": "string",
              "value": "={{ $('Error Trigger').item.json.execution.error.message }}"
            },
            {
              "id": "auto-005",
              "name": "error_stack",
              "type": "string",
              "value": "={{ $('Error Trigger').item.json.execution.error.stack }}"
            },
            {
              "id": "auto-006",
              "name": "execution_id",
              "type": "string",
              "value": "={{ $('Error Trigger').item.json.execution.id }}"
            },
            {
              "id": "auto-007",
              "name": "retry_of",
              "type": "string",
              "value": "={{ $('Error Trigger').item.json.execution.retryOf || '' }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "5c2dc61b-77ca-44a8-9779-18643d20444e",
      "name": "Get Workflow Content",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueRegularOutput",
      "position": [
        544,
        0
      ],
      "parameters": {
        "url": "={{ $('Set Context').item.json.n8n_instance_url + '/api/v1/workflows/' + $('Set Context').item.json.error_workflow_id }}",
        "options": {},
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "accept",
              "value": "application/json"
            },
            {
              "name": "X-N8N-API-KEY",
              "value": "={{ $('Set Context').item.json.n8n_api_key }}"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "fe97c17b-ded9-4817-bae3-6a4da55ac3c2",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        784,
        0
      ],
      "parameters": {
        "text": "=Workflow Name: {{ $('Set Context').item.json.error_workflow_name }}\nFailed Node: {{ $('Set Context').item.json.error_node }}\nError Message: {{ $('Set Context').item.json.error_message }}\nStack Trace: {{ $('Set Context').item.json.error_stack }}\nRetry Of Execution: {{ $('Set Context').item.json.retry_of }}\nExecution Link: {{ $('Set Context').item.json.n8n_instance_url + '/workflow/' + $('Set Context').item.json.error_workflow_id + '/executions/' + $('Set Context').item.json.execution_id }}\nWorkflow Definition: {{ JSON.stringify($json) }}",
        "options": {
          "systemMessage": "=You are an AI assistant specialized in analyzing n8n workflow errors. Your job is to generate a clear, actionable error report formatted for Telegram.\n\n## Error Classification\nClassify the error based on its message and stack trace:\n- HTTP 401/403 \u2192 \"\ud83d\udd10 Authentication Error\"\n- HTTP 404 \u2192 \"\u274c Not Found Error\"\n- HTTP 429 \u2192 \"\u23f1\ufe0f Rate Limit Error\"\n- HTTP 500/502/503/504 \u2192 \"\u26a0\ufe0f Server Error\"\n- \"does not exist\" / \"credential\" \u2192 \"\ud83d\udd11 Credential Error\"\n- \"Cannot read\" / \"undefined\" / \"null\" \u2192 \"\ud83d\udc1b Undefined Property Error\"\n- \"Invalid JSON\" / \"parse\" \u2192 \"\ud83d\udcc4 JSON Parse Error\"\n- \"ECONNREFUSED\" / \"Connection\" / \"ETIMEDOUT\" \u2192 \"\ud83d\udd0c Connection Error\"\n- \"Timeout\" \u2192 \"\u23f0 Timeout Error\"\n- Other \u2192 \"\ud83d\udd34 Runtime Error\"\n\n## Output Format\nReturn ONLY a valid JSON object with a single field:\n{\n  \"explanation\": \"Telegram HTML formatted string here\"\n}\n\n## Telegram HTML Rules\n- Allowed tags ONLY: <b>, <i>, <code>, <pre>, <a href=\"URL\">\n- Use literal \\n for line breaks\n- No markdown, no other HTML tags\n- Do not use & < > characters outside of allowed HTML tags\n\n## Report Template (in English)\n\ud83d\udea8 <b>N8N WORKFLOW ERROR</b>\\n\\n\ud83d\udccc <b>Workflow:</b> <code>{workflow_name}</code>\\n\u2699\ufe0f <b>Failed Node:</b> <code>{failed_node}</code>\\n\ud83c\udff7\ufe0f <b>Error Type:</b> {error_type}\\n\\n<b>\ud83d\udccb Error Message:</b>\\n<code>{error_message_short}</code>\\n\\n<b>\ud83d\udd0d Root Cause:</b>\\n{root_cause_1_2_sentences}\\n\\n<b>\u2705 Fix Steps:</b>\\n\u2022 {fix_step_1}\\n\u2022 {fix_step_2}\\n\u2022 {fix_step_3}\\n\\n{retry_line_only_if_retry_of_not_empty}\\n<a href=\"{execution_link}\">\ud83d\udd17 View execution details</a>\n\n## Rules\n- MUST return ONLY the JSON object, no extra text before or after\n- Keep the explanation under 15 lines total\n- error_message_short: truncate to 200 characters if longer\n- Provide exactly 2-3 specific and actionable fix steps based on error type\n- If retry_of is empty or null, OMIT the retry line entirely\n- retry_line format: \u26a0\ufe0f <i>This is a retry of execution #{retry_of}</i>\\n\\n\n- Use the workflow definition to give context-aware suggestions when possible"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "retryOnFail": true,
      "typeVersion": 3.1
    },
    {
      "id": "9bc878d7-29c1-4683-9499-d85e17955278",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        704,
        240
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "b2e47b17-01fb-496c-8a97-68bb51650d70",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        944,
        240
      ],
      "parameters": {
        "autoFix": true,
        "jsonSchemaExample": "{\n  \"explanation\": \"\ud83d\udea8 <b>L\u1ed6I WORKFLOW N8N</b>\\n\\n\ud83d\udccc <b>Workflow:</b> <code>My Workflow</code>\\n\u2699\ufe0f <b>Node l\u1ed7i:</b> <code>HTTP Request</code>\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "4e20e15b-d178-4de7-b6dc-6ed91a8d0286",
      "name": "Send Telegram Notification",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1168,
        0
      ],
      "parameters": {
        "text": "={{ $json.output.explanation }}",
        "chatId": "={{ $('Set Context').item.json.telegram_chat_id }}",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "3e789f7e-509d-4774-a976-4c1f6bd55176",
      "name": "Main Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -608,
        -448
      ],
      "parameters": {
        "width": 560,
        "height": 912,
        "content": "## AI-Powered n8n Error Monitor\n\nThis workflow **automatically catches errors** from any other n8n workflow, analyzes them with **Google Gemini AI**, and sends a structured diagnostic report to your **Telegram** \u2014 including root cause analysis and actionable fix steps.\n\nAll configuration lives in **one node** (`Set Context`). No other node needs editing.\n\n### How it works\n1. **Error Trigger** fires whenever any workflow on this n8n instance encounters a failure.\n2. **Set Context** captures error data and holds your 3 config values (instance URL, API key, Telegram chat ID).\n3. **Get Workflow Content** fetches the full workflow definition via n8n API to give AI context.\n4. **AI Agent** (Gemini) classifies the error type, identifies root cause, and writes a Telegram-formatted report in Vietnamese.\n5. **Send Telegram Notification** delivers the formatted HTML report to your chat.\n\n### Setup\n* [ ] Connect **Google Gemini** (`googlePalmApi`) credential to the Gemini Chat Model node.\n* [ ] Connect **Telegram Bot** credential to the Send Telegram Notification node.\n* [ ] Open **Set Context** \u2014 update `n8n_instance_url`, `n8n_api_key`, and `telegram_chat_id`.\n* [ ] Activate the workflow.\n* [ ] In other workflows, set this workflow as their **Error Workflow** under Settings.\n\n### Customization tips\n* **Different AI model:** Swap the Gemini node for OpenAI or any other LLM sub-node.\n* **Different notification channel:** Replace Telegram with Slack, Discord, or email.\n* **Report language:** Change the language instruction in the AI Agent system prompt.\n\n### LICENCE\nThis template is shared free of charge. Copyright belongs to Nguyen Thieu Toan (Jay Nguyen). Any copying or modification must credit the author."
      },
      "typeVersion": 1
    },
    {
      "id": "b8fd3456-51d8-41f2-80f4-1ae54c6f0e55",
      "name": "Warning Set Context",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        128,
        -256
      ],
      "parameters": {
        "color": 3,
        "width": 340,
        "height": 416,
        "content": "## \u26a0\ufe0f Edit this node!\n\nUpdate these **3 fields** only:\n- `n8n_instance_url` \u2014 your n8n public URL\n- `n8n_api_key` \u2014 from n8n \u2192 Settings \u2192 API\n- `telegram_chat_id` \u2014 your Telegram chat/user ID\n\nAll other fields are auto-populated from the error."
      },
      "typeVersion": 1
    },
    {
      "id": "fc67cd95-f98c-4f15-9bd7-c0ec66858d30",
      "name": "Author Message",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1328,
        -192
      ],
      "parameters": {
        "color": 4,
        "width": 560,
        "height": 360,
        "content": "## Author Message\n\nHi! I am **Nguyen Thieu Toan (Jay Nguyen)** \u2014 a Verified n8n Creator. Thank you for using this template!\n\nThis workflow is shared with you for free. If it brings value to your work, optimizes your operations, or saves you time, you can buy me a coffee here: **[My Donate Website](https://nguyenthieutoan.com/payment/)** *(PayPal, Momo, Bank Transfer)*\n\n* Website: [nguyenthieutoan.com](https://nguyenthieutoan.com)\n* Email: me@nguyenthieutoan.com\n* Company: GenStaff ([genstaff.net](https://genstaff.net))\n* Socials (Facebook / X / LinkedIn): @nguyenthieutoan\n\n*Discover more of my automation solutions:* **[Click here](https://n8n.io/creators/nguyenthieutoan/)**"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send Telegram Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Context": {
      "main": [
        [
          {
            "node": "Get Workflow Content",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Error Trigger": {
      "main": [
        [
          {
            "node": "Set Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Workflow Content": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Structured Output Parser",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  }
}