{
  "name": "Publicador Automático Multi-Red Social",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 30
            }
          ]
        }
      },
      "id": "f2a3b4c5-0012-4a01-b001-ef1234567001",
      "name": "Cron Trigger - Cada 30 Min",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "Publicaciones"
        },
        "options": {
          "range": "A:J"
        }
      },
      "id": "f2a3b4c5-0012-4a02-b002-ef1234567002",
      "name": "Google Sheets - Leer Publicaciones",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [500, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const now = new Date();\nconst currentHour = now.getHours();\nconst currentDate = now.toISOString().split('T')[0];\nconst results = [];\n\nfor (const item of items) {\n  if (item.json.fecha === currentDate && \n      parseInt(item.json.hora) === currentHour && \n      item.json.estado === 'Pendiente') {\n    \n    // Adaptar texto por plataforma\n    let textoFacebook = item.json.texto;\n    let textoInstagram = item.json.texto;\n    let textoTelegram = item.json.texto;\n    \n    // Instagram: agregar hashtags\n    if (item.json.hashtags) {\n      textoInstagram += '\\n\\n' + item.json.hashtags;\n    }\n    \n    // Telegram: formato enriquecido\n    textoTelegram = '*' + (item.json.titulo || '') + '*\\n\\n' + item.json.texto;\n    \n    results.push({\n      json: {\n        ...item.json,\n        textoFacebook,\n        textoInstagram,\n        textoTelegram\n      }\n    });\n  }\n}\n\nreturn results.length > 0 ? results : [{json: {skip: true}}];"
      },
      "id": "f2a3b4c5-0012-4a03-b003-ef1234567003",
      "name": "Function - Filtrar y Adaptar Texto",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [750, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v17.0/PAGE_ID/feed",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "message",
              "value": "={{ $json.textoFacebook }}"
            }
          ]
        },
        "options": {}
      },
      "id": "f2a3b4c5-0012-4a04-b004-ef1234567004",
      "name": "HTTP Request - Facebook",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1000, 150]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v17.0/IG_USER_ID/media",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "caption",
              "value": "={{ $json.textoInstagram }}"
            },
            {
              "name": "image_url",
              "value": "={{ $json.imagen_url }}"
            }
          ]
        },
        "options": {}
      },
      "id": "f2a3b4c5-0012-4a05-b005-ef1234567005",
      "name": "HTTP Request - Instagram",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1000, 300]
    },
    {
      "parameters": {
        "chatId": "",
        "text": "={{ $json.textoTelegram }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "f2a3b4c5-0012-4a06-b006-ef1234567006",
      "name": "Telegram - Publicar Canal",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [1000, 450],
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "Publicaciones"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "estado": "Publicado",
            "fecha_publicado": "={{ $now.toISO() }}"
          }
        },
        "lookupColumn": "id_publicacion",
        "lookupValue": "={{ $json.id_publicacion }}",
        "options": {}
      },
      "id": "f2a3b4c5-0012-4a07-b007-ef1234567007",
      "name": "Google Sheets - Actualizar Estado",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1250, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "Cron Trigger - Cada 30 Min": {
      "main": [
        [
          {
            "node": "Google Sheets - Leer Publicaciones",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Leer Publicaciones": {
      "main": [
        [
          {
            "node": "Function - Filtrar y Adaptar Texto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function - Filtrar y Adaptar Texto": {
      "main": [
        [
          {
            "node": "HTTP Request - Facebook",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request - Instagram",
            "type": "main",
            "index": 0
          },
          {
            "node": "Telegram - Publicar Canal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Facebook": {
      "main": [
        [
          {
            "node": "Google Sheets - Actualizar Estado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Instagram": {
      "main": [
        [
          {
            "node": "Google Sheets - Actualizar Estado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram - Publicar Canal": {
      "main": [
        [
          {
            "node": "Google Sheets - Actualizar Estado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "social-media"
    },
    {
      "name": "multi-plataforma"
    },
    {
      "name": "publicacion"
    }
  ]
}
