{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "monitor-alert",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ],
      "id": "0a2acd5e-a030-4590-9b4b-a9ddcfab0335",
      "name": "Webhook"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "models/gemini-2.0-flash",
          "mode": "list",
          "cachedResultName": "models/gemini-2.0-flash"
        },
        "messages": {
          "values": [
            {
              "content": "=Eres un motor de notificaciones backend.\nTu \u00daNICA funci\u00f3n es convertir datos JSON en un mensaje de texto plano para WhatsApp.\nREGLAS SAGRADAS:\n1. NO saludes, NO expliques, NO uses introducciones como \"Aqu\u00ed est\u00e1 el mensaje\".\n2. Tu salida debe empezar directamente con el primer emoji del mensaje.\n3. Si hablas m\u00e1s de la cuenta, el sistema fallar\u00e1.",
              "role": "model"
            },
            {
              "content": "=Genera la alerta con estos datos:\nServidor: {{ $json.body.site }}\nError: {{ $json.body.error_code }}\nHora: {{ $now.format('HH:mm - dd/MM/yyyy') }}"
            }
          ]
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1.1,
      "position": [
        0,
        224
      ],
      "id": "80a5bdfd-664b-403e-b85e-0676742737d7",
      "name": "Message a model",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// 1. OBTENER ESTADO NUEVO\nconst webhookData = $('Webhook').first().json.body;\nconst newStatus = String(webhookData.error_code).trim();\nconst site = webhookData.site;\nconst timestamp = webhookData.check_time;\n\n// 2. OBTENER ESTADO ANTERIOR\nlet oldStatus = 'Primer chequeo';\n\n// Accedemos al input binario\nconst binaryData = $input.first().binary;\n\nif (binaryData && binaryData.data) {\n  try {\n    // Intentamos obtener el contenido directamente. \n    // En n8n, el m\u00e9todo m\u00e1s seguro es usar el helper del objeto binario\n    // o el Buffer si ya habilitaste la variable de entorno.\n    \n    const buffer = await this.helpers.getBinaryDataBuffer(0, 'data');\n    oldStatus = buffer.toString('utf8').trim();\n    \n  } catch (error) {\n    // Si falla el helper, probamos el m\u00e9todo directo del objeto\n    try {\n        oldStatus = Buffer.from(binaryData.data.data, 'base64').toString('utf8').trim();\n    } catch (e) {\n        oldStatus = 'Primer chequeo';\n    }\n  }\n}\n\n// 3. L\u00d3GICA DE COMPARACI\u00d3N\nconst hasChanged = (newStatus !== oldStatus);\nconst isUp = (newStatus === '200' || newStatus === '302');\n\n// Formatear mensaje visual\nlet displayStatus = newStatus;\nif (newStatus === '000') displayStatus = '000 (Sin Conexi\u00f3n)';\n\nreturn {\n  json: {\n    alert: hasChanged,\n    site: site,\n    new_status: displayStatus,\n    old_status: oldStatus,\n    type: isUp ? 'recovered' : 'down',\n    check_time: timestamp,\n    file_content: newStatus \n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        432,
        0
      ],
      "id": "2887f53d-255f-4a64-b80f-6bbbb98ba7e9",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "chatId": "794033826",
        "text": "={{ $json.type == 'recovered' ? '\ud83d\udfe2 \u00a1SERVICIO RECUPERADO!' : '\ud83d\udea8 \u00a1ALERTA: SITIO CA\u00cdDO!' }}  \ud83c\udf10 **Sitio:** {{ $json.site }} \ud83d\udcca **Estado:** {{ $json.new_status }} history **Anterior:** {{ $json.old_status }}  \ud83d\udcc5 **Hora:** {{ $json.timestamp }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        896,
        -32
      ],
      "id": "c3a46b7a-5590-4132-9adf-1523b42b2eea",
      "name": "Send a text message",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "9350a1ce-3382-4a58-b138-ef25d26a51a3",
              "leftValue": "={{ $json.alert }}",
              "rightValue": "true",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        656,
        0
      ],
      "id": "9644a91e-7caf-4ec4-94f0-22e2ecf7acb8",
      "name": "If"
    },
    {
      "parameters": {
        "fileSelector": "/home/node/.n8n-files/monitor_status.txt",
        "options": {}
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        208,
        0
      ],
      "id": "107fac7f-20df-4234-a2d8-1b22d4361806",
      "name": "Read/Write Files from Disk",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "operation": "write",
        "fileName": "/home/node/.n8n-files/monitor_status.txt",
        "options": {
          "append": false
        }
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        800,
        208
      ],
      "id": "19f04bc7-3410-4d04-a782-121ecb3d2b91",
      "name": "Read/Write Files from Disk1",
      "executeOnce": false,
      "retryOnFail": false,
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "operation": "toText",
        "sourceProperty": "new_status",
        "options": {}
      },
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        608,
        208
      ],
      "id": "8b11b833-0a36-4023-a049-0a81371f50a7",
      "name": "Convert to File"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Read/Write Files from Disk",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        []
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          },
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read/Write Files from Disk": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message": {
      "main": [
        []
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "Read/Write Files from Disk1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false,
    "timeSavedMode": "fixed",
    "callerPolicy": "workflowsFromSameOwner"
  },
  "versionId": "f7040b27-544d-4d20-a59a-c8c99389d3c5",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "AjBDxecKidHHOm2c6NYg9",
  "tags": []
}