{
  "name": "Channel Failover Router",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "channel-status",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "cf-001",
      "name": "Health Check Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Check WhatsApp Cloud API status\nconst https = require('https');\n\n// WhatsApp Cloud API check (Meta's endpoint)\nconst options = {\n  hostname: 'graph.facebook.com',\n  port: 443,\n  path: '/v18.0/me',\n  method: 'GET',\n  headers: {\n    'Authorization': 'Bearer ' + $env.WHATSAPP_TOKEN\n  },\n  timeout: 5000\n};\n\nreturn new Promise((resolve) => {\n  const req = https.request(options, (res) => {\n    resolve({ status: res.statusCode, channel: 'whatsapp', available: res.statusCode === 200 });\n  });\n  req.on('error', () => resolve({ status: 0, channel: 'whatsapp', available: false }));\n  req.on('timeout', () => { req.destroy(); resolve({ status: 0, channel: 'whatsapp', available: false }); });\n  req.end();\n});"
      },
      "id": "cf-002",
      "name": "Check WhatsApp Status",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        200,
        -100
      ]
    },
    {
      "parameters": {
        "jsCode": "// Check Telegram Bot API status\nconst https = require('https');\n\nconst options = {\n  hostname: 'api.telegram.org',\n  port: 443,\n  path: '/bot' + $env.TELEGRAM_BOT_TOKEN + '/getMe',\n  method: 'GET',\n  timeout: 5000\n};\n\nreturn new Promise((resolve) => {\n  const req = https.request(options, (res) => {\n    let data = '';\n    res.on('data', chunk => data += chunk);\n    res.on('end', () => {\n      try {\n        const json = JSON.parse(data);\n        resolve({ status: res.statusCode, channel: 'telegram', available: json.ok === true });\n      } catch {\n        resolve({ status: res.statusCode, channel: 'telegram', available: false });\n      }\n    });\n  });\n  req.on('error', () => resolve({ status: 0, channel: 'telegram', available: false }));\n  req.on('timeout', () => { req.destroy(); resolve({ status: 0, channel: 'telegram', available: false }); });\n  req.end();\n});"
      },
      "id": "cf-003",
      "name": "Check Telegram Status",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        200,
        100
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true
          },
          "conditions": [
            {
              "id": "cf-wa-up",
              "leftValue": "={{$json.available}}",
              "rightValue": true,
              "operation": "equals"
            },
            {
              "id": "cf-tg-up",
              "leftValue": "={{$json.available}}",
              "rightValue": true,
              "operation": "equals"
            }
          ]
        },
        "options": {}
      },
      "id": "cf-004",
      "name": "Switch: Both Up",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 1,
      "position": [
        400,
        0
      ]
    },
    {
      "parameters": {
        "respondWith": {
          "respondWith": "json",
          "responseBody": "={{ $json }}"
        },
        "options": {}
      },
      "id": "cf-005",
      "name": "Both Available",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        600,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Merge status from both channels\nconst wa = $input.all()[0].json;\nconst tg = $input.all()[1].json;\n\nconst result = {\n  timestamp: new Date().toISOString(),\n  whatsapp: { available: wa.available, status: wa.status },\n  telegram: { available: tg.available, status: tg.status },\n  primary: wa.available ? 'whatsapp' : (tg.available ? 'telegram' : 'none'),\n  failover_cta: wa.available ? 'https://t.me/buncheng' : 'https://wa.me/2347032981049'\n};\n\nreturn [{ json: result }];"
      },
      "id": "cf-006",
      "name": "Merge Status",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        400,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": {
          "respondWith": "json",
          "responseBody": "={{ $json }}"
        },
        "options": {}
      },
      "id": "cf-007",
      "name": "Return Status",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        600,
        200
      ]
    }
  ],
  "connections": {
    "Health Check Webhook": {
      "main": [
        [
          {
            "node": "Check WhatsApp Status",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check Telegram Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check WhatsApp Status": {
      "main": [
        [
          {
            "node": "Merge Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Telegram Status": {
      "main": [
        [
          {
            "node": "Merge Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Status": {
      "main": [
        [
          {
            "node": "Return Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveManualExecutions": false,
    "timezone": "Africa/Lagos"
  },
  "staticData": null,
  "tags": [
    "infrastructure",
    "failover",
    "health-check"
  ]
}