{
  "name": "Guida - Alert Telegram quando un endpoint cade",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            { "field": "minutes", "minutesInterval": 5 }
          ]
        }
      },
      "id": "trigger1",
      "name": "Ogni 5 minuti",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "url": "https://example.com",
        "options": { "timeout": 10000 }
      },
      "onError": "continueErrorOutput",
      "id": "http1",
      "name": "Controlla endpoint",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "a1", "name": "isUp", "type": "boolean", "value": true }
          ]
        }
      },
      "id": "set1",
      "name": "Marca ONLINE",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [440, -80]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "a2", "name": "isUp", "type": "boolean", "value": false }
          ]
        }
      },
      "id": "set2",
      "name": "Marca OFFLINE",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [440, 80]
    },
    {
      "parameters": {
        "jsCode": "const isUp = $input.first().json.isUp;\nconst key = 'endpointUp';\nconst wasUp = $getWorkflowStaticData('node')[key];\n$getWorkflowStaticData('node')[key] = isUp;\nconst changed = wasUp !== undefined && wasUp !== isUp;\nreturn [{ json: { isUp, changed } }];"
      },
      "id": "code1",
      "name": "Confronta con stato precedente",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "loose" },
          "conditions": [
            {
              "id": "cond1",
              "leftValue": "={{$json.changed}}",
              "rightValue": true,
              "operator": { "type": "boolean", "operation": "true" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if1",
      "name": "Stato cambiato?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [880, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.telegram.org/botYOUR_BOT_TOKEN/sendMessage",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            { "name": "chat_id", "value": "YOUR_CHAT_ID" },
            { "name": "text", "value": "={{$json.isUp ? '✅ Endpoint tornato ONLINE' : '🔴 Endpoint OFFLINE'}}" }
          ]
        },
        "options": {}
      },
      "id": "http2",
      "name": "Invia alert Telegram",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1100, 0]
    }
  ],
  "connections": {
    "Ogni 5 minuti": { "main": [[{ "node": "Controlla endpoint", "type": "main", "index": 0 }]] },
    "Controlla endpoint": {
      "main": [
        [{ "node": "Marca ONLINE", "type": "main", "index": 0 }],
        [{ "node": "Marca OFFLINE", "type": "main", "index": 0 }]
      ]
    },
    "Marca ONLINE": { "main": [[{ "node": "Confronta con stato precedente", "type": "main", "index": 0 }]] },
    "Marca OFFLINE": { "main": [[{ "node": "Confronta con stato precedente", "type": "main", "index": 0 }]] },
    "Confronta con stato precedente": { "main": [[{ "node": "Stato cambiato?", "type": "main", "index": 0 }]] },
    "Stato cambiato?": { "main": [[{ "node": "Invia alert Telegram", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" }
}
