{
  "name": "Monitor de Comentarios y Menciones en Redes Sociales",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 15
            }
          ]
        }
      },
      "id": "a3b4c5d6-0013-4a01-b001-ef1234567001",
      "name": "Schedule Trigger - Cada 15 Min",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://graph.facebook.com/v17.0/PAGE_ID/feed",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "id,message,comments{id,message,from,created_time}"
            },
            {
              "name": "since",
              "value": "={{ Math.floor(Date.now() / 1000) - 900 }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a3b4c5d6-0013-4a02-b002-ef1234567002",
      "name": "HTTP Request - Comentarios Facebook",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [500, 200]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://graph.facebook.com/v17.0/IG_USER_ID/media",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "id,caption,comments{id,text,username,timestamp}"
            }
          ]
        },
        "options": {}
      },
      "id": "a3b4c5d6-0013-4a03-b003-ef1234567003",
      "name": "HTTP Request - Comentarios Instagram",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [500, 400]
    },
    {
      "parameters": {
        "functionCode": "const negativeWords = ['reclamo', 'malo', 'pésimo', 'estafa', 'nunca más', 'horrible', 'terrible', 'fraude', 'robo', 'engaño', 'basura', 'peor', 'asco', 'denuncia'];\nconst results = [];\n\nfor (const item of items) {\n  const comments = item.json.comments || item.json.data || [];\n  \n  for (const comment of (Array.isArray(comments) ? comments : (comments.data || []))) {\n    const text = (comment.message || comment.text || '').toLowerCase();\n    const isNegative = negativeWords.some(word => text.includes(word));\n    \n    results.push({\n      json: {\n        commentId: comment.id,\n        texto: comment.message || comment.text,\n        autor: comment.from ? comment.from.name : comment.username,\n        fecha: comment.created_time || comment.timestamp,\n        plataforma: comment.message ? 'Facebook' : 'Instagram',\n        sentimiento: isNegative ? 'NEGATIVO' : 'POSITIVO',\n        postId: item.json.id\n      }\n    });\n  }\n}\n\nreturn results.length > 0 ? results : [{json: {noComments: true}}];"
      },
      "id": "a3b4c5d6-0013-4a04-b004-ef1234567004",
      "name": "Function - Analizar Sentimiento",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [750, 300]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.sentimiento }}",
              "operation": "equal",
              "value2": "NEGATIVO"
            }
          ]
        }
      },
      "id": "a3b4c5d6-0013-4a05-b005-ef1234567005",
      "name": "IF - Comentario Negativo",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [1000, 300]
    },
    {
      "parameters": {
        "chatId": "",
        "text": "=🚨 *ALERTA - Comentario Negativo Detectado*\n\n📱 Plataforma: {{ $json.plataforma }}\n👤 Autor: {{ $json.autor }}\n💬 Comentario: \"{{ $json.texto }}\"\n📅 Fecha: {{ $json.fecha }}\n🔗 Post ID: {{ $json.postId }}\n\n⚡ _Responder lo antes posible para proteger la reputación de la marca._",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "a3b4c5d6-0013-4a06-b006-ef1234567006",
      "name": "Telegram - Alerta Urgente",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [1250, 200],
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "Registro Comentarios"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "a3b4c5d6-0013-4a07-b007-ef1234567007",
      "name": "Google Sheets - Registrar Comentario",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1250, 400],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "Schedule Trigger - Cada 15 Min": {
      "main": [
        [
          {
            "node": "HTTP Request - Comentarios Facebook",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request - Comentarios Instagram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Comentarios Facebook": {
      "main": [
        [
          {
            "node": "Function - Analizar Sentimiento",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Comentarios Instagram": {
      "main": [
        [
          {
            "node": "Function - Analizar Sentimiento",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function - Analizar Sentimiento": {
      "main": [
        [
          {
            "node": "IF - Comentario Negativo",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF - Comentario Negativo": {
      "main": [
        [
          {
            "node": "Telegram - Alerta Urgente",
            "type": "main",
            "index": 0
          },
          {
            "node": "Google Sheets - Registrar Comentario",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Google Sheets - Registrar Comentario",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "social-media"
    },
    {
      "name": "monitoreo"
    },
    {
      "name": "reputacion"
    }
  ]
}
