{
  "name": "Manin Studio — мониторинг сайтов и сервисов",
  "nodes": [
    {
      "id": "4c42b6bb-70f7-4b2d-9f8d-c1b2f66b0e94",
      "name": "Каждые 5 минут",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        240
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      }
    },
    {
      "id": "ad69f727-be45-445e-a1f5-c30c68eef1aa",
      "name": "Адреса для проверки",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        240
      ],
      "parameters": {
        "jsCode": "// Укажите только разрешённые для проверки URL.\nreturn [\n  {json: {url: 'https://example.com/'}},\n  {json: {url: 'https://example.org/'}}\n];"
      }
    },
    {
      "id": "5ffe060c-f7a7-4f16-814b-5916d3174357",
      "name": "Первая HTTP-проверка",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        460,
        240
      ],
      "parameters": {
        "url": "={{ $json.url }}",
        "options": {
          "timeout": 8000,
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "text"
            }
          }
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "id": "b230c11c-fb52-4036-be6c-9b18a39e5149",
      "name": "Определить результат",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        700,
        240
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const original = $('Адреса для проверки').item.json;\nconst response = $input.item.json;\nconst status = Number(response.statusCode || String(response.error?.message || '').match(/^\\d{3}/)?.[0] || 0);\nreturn {json: {url: original.url, ok: !response.error && status >= 200 && status < 400, statusCode: status}};"
      }
    },
    {
      "id": "6dba8c6d-ce04-4363-b70e-df64a4b27326",
      "name": "Сохранить состояние и журнал",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        940,
        240
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT * FROM ms_monitor_record($1::text, $2::boolean, $3::integer);",
        "options": {
          "queryReplacement": "={{ [$json.url, $json.ok, $json.statusCode] }}"
        }
      }
    },
    {
      "id": "29badb34-6301-40bb-bafa-ab6590d722cc",
      "name": "Состояние изменилось?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1180,
        240
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "7b24f33e-2c34-481e-b4ca-e5fb1d0d2a65",
              "leftValue": "={{ $json.should_notify }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "203894d5-dba0-4e5a-bed6-2246140a62fd",
      "name": "Уведомить в Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1420,
        180
      ],
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "REPLACE_WITH_YOUR_CHAT_ID",
        "text": "={{ $json.current_ok ? 'Восстановлен: ' + $json.url : 'Недоступен: ' + $json.url }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "fbffa7d9-3b9c-4da8-a319-db80410c56ef",
      "name": "Первая проверка успешна?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        700,
        180
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "49e561d3-89df-401e-b2cb-0b425ad7b8ed",
              "leftValue": "={{ !$json.error && $json.statusCode >= 200 && $json.statusCode < 400 }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "11b44508-439f-4782-a11b-efcdd9f8a3d5",
      "name": "Повторная HTTP-проверка",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        940,
        340
      ],
      "parameters": {
        "url": "={{ $('Адреса для проверки').item.json.url }}",
        "options": {
          "timeout": 8000,
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "text"
            }
          }
        }
      },
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {
    "Каждые 5 минут": {
      "main": [
        [
          {
            "node": "Адреса для проверки",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Адреса для проверки": {
      "main": [
        [
          {
            "node": "Первая HTTP-проверка",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Определить результат": {
      "main": [
        [
          {
            "node": "Сохранить состояние и журнал",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Сохранить состояние и журнал": {
      "main": [
        [
          {
            "node": "Состояние изменилось?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Состояние изменилось?": {
      "main": [
        [
          {
            "node": "Уведомить в Telegram",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Первая HTTP-проверка": {
      "main": [
        [
          {
            "node": "Первая проверка успешна?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Первая проверка успешна?": {
      "main": [
        [
          {
            "node": "Определить результат",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Повторная HTTP-проверка",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Повторная HTTP-проверка": {
      "main": [
        [
          {
            "node": "Определить результат",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "pinData": {},
  "id": "6abf88feeeeb4b94b084a2f039fb3d71"
}
