{
  "name": "SSL 證書監控 (使用 API)",
  "nodes": [
    {
      "parameters": {
        "rule": { "interval": [{ "field": "cronExpression", "expression": "0 9 * * *" }] }
      },
      "id": "schedule-trigger",
      "name": "每日 9:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [0, 0]
    },
    {
      "parameters": {
        "url": "http://192.168.0.110:5001/api/system/ssl/check",
        "options": { "timeout": 30000 }
      },
      "id": "http-ssl",
      "name": "檢查 SSL 狀態",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [220, 0]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst certs = data.certificates || {};\nlet issues = [];\n\nfor (const [domain, info] of Object.entries(certs)) {\n  if (!info.valid) {\n    issues.push('❌ ' + domain + ': ' + info.error);\n  } else if (info.needs_renewal) {\n    issues.push('⏳ ' + domain + ': ' + info.days_left + ' 天後到期');\n  }\n}\n\nreturn [{ json: { healthy: data.healthy, issues, hasIssue: issues.length > 0, issueText: issues.join('\\n') }}];"
      },
      "id": "code-parse",
      "name": "解析狀態",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [{ "id": "c1", "leftValue": "={{ $json.hasIssue }}", "rightValue": true, "operator": { "type": "boolean", "operation": "equals" } }],
          "combinator": "and"
        }
      },
      "id": "if-issue",
      "name": "有問題?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst text = '🟡 *SSL 證書警告*\\n\\n' + data.issueText + '\\n\\n💡 執行: sudo certbot renew --nginx';\n\nreturn [{ json: { telegramBody: { chat_id: '5619078117', text: text, parse_mode: 'Markdown' } } }];"
      },
      "id": "code-alert",
      "name": "組合告警訊息",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [880, 100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telegram.org/bot8075645931:AAH-EGKMo8ZC4QJs-Nc1_0s92xHrGdQvdpg/sendMessage",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.telegramBody) }}"
      },
      "id": "telegram-alert",
      "name": "發送提醒",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [1100, 100]
    },
    {
      "parameters": {},
      "id": "no-op",
      "name": "證書正常",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [880, -100]
    }
  ],
  "connections": {
    "每日 9:00": { "main": [[{ "node": "檢查 SSL 狀態", "type": "main", "index": 0 }]] },
    "檢查 SSL 狀態": { "main": [[{ "node": "解析狀態", "type": "main", "index": 0 }]] },
    "解析狀態": { "main": [[{ "node": "有問題?", "type": "main", "index": 0 }]] },
    "有問題?": { "main": [[{ "node": "組合告警訊息", "type": "main", "index": 0 }], [{ "node": "證書正常", "type": "main", "index": 0 }]] },
    "組合告警訊息": { "main": [[{ "node": "發送提醒", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
