{
  "name": "Calendario de Redes Sociales Automatizado",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 30
            }
          ]
        }
      },
      "id": "a7b8c9d0-0007-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": "Calendario Redes"
        },
        "options": {
          "range": "A:H"
        }
      },
      "id": "a7b8c9d0-0007-4a02-b002-ef1234567002",
      "name": "Google Sheets - Leer Calendario",
      "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];\n\nconst scheduled = items.filter(item => {\n  const postDate = item.json.fecha_programada;\n  const postHour = parseInt(item.json.hora_programada);\n  return postDate === currentDate && postHour === currentHour && item.json.estado === 'Programado';\n});\n\nreturn scheduled;"
      },
      "id": "a7b8c9d0-0007-4a03-b003-ef1234567003",
      "name": "Function - Filtrar Programados Ahora",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [750, 300]
    },
    {
      "parameters": {
        "rules": {
          "rules": [
            {
              "value": "facebook",
              "output": 0
            },
            {
              "value": "instagram",
              "output": 1
            },
            {
              "value": "ambas",
              "output": 2
            }
          ]
        },
        "fallbackOutput": 0,
        "dataType": "string",
        "value1": "={{ $json.plataforma }}"
      },
      "id": "a7b8c9d0-0007-4a04-b004-ef1234567004",
      "name": "Switch - Plataforma Destino",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 2,
      "position": [1000, 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.texto }}"
            },
            {
              "name": "link",
              "value": "={{ $json.enlace || '' }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a7b8c9d0-0007-4a05-b005-ef1234567005",
      "name": "HTTP Request - Publicar Facebook",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1250, 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.texto + '\\n\\n' + $json.hashtags }}"
            },
            {
              "name": "image_url",
              "value": "={{ $json.imagen_url }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a7b8c9d0-0007-4a06-b006-ef1234567006",
      "name": "HTTP Request - Publicar Instagram",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1250, 350]
    },
    {
      "parameters": {
        "operation": "update",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "Calendario Redes"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "estado": "Publicado",
            "fecha_publicacion_real": "={{ $now.toISO() }}"
          }
        },
        "lookupColumn": "id_post",
        "lookupValue": "={{ $json.id_post }}",
        "options": {}
      },
      "id": "a7b8c9d0-0007-4a07-b007-ef1234567007",
      "name": "Google Sheets - Marcar Publicado",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1500, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "Cron Trigger - Cada 30 Min": {
      "main": [
        [
          {
            "node": "Google Sheets - Leer Calendario",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Leer Calendario": {
      "main": [
        [
          {
            "node": "Function - Filtrar Programados Ahora",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function - Filtrar Programados Ahora": {
      "main": [
        [
          {
            "node": "Switch - Plataforma Destino",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch - Plataforma Destino": {
      "main": [
        [
          {
            "node": "HTTP Request - Publicar Facebook",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request - Publicar Instagram",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request - Publicar Facebook",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP Request - Publicar Instagram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Publicar Facebook": {
      "main": [
        [
          {
            "node": "Google Sheets - Marcar Publicado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request - Publicar Instagram": {
      "main": [
        [
          {
            "node": "Google Sheets - Marcar Publicado",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "content-creation"
    },
    {
      "name": "redes-sociales"
    },
    {
      "name": "calendario"
    }
  ]
}
