{
  "name": "Instagram Reel + Story Integrated",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -2224,
        480
      ],
      "id": "1d36e423-c8b3-474e-8178-3ba048bbe11a",
      "name": "When clicking 'Execute workflow'"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "posts",
          "mode": "name"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "status",
              "lookupValue": "CANVA_READY"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -1984,
        480
      ],
      "id": "4be5121c-9bff-4684-b444-1879cd5587cd",
      "name": "Get DRAFT Posts",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -1744,
        480
      ],
      "id": "66619adb-c286-4f6f-9ab2-4ac44c6220e4",
      "name": "Loop"
    },
    {
      "parameters": {
        "jsCode": "const item = $input.first();\nconst contentJson = JSON.parse(item.json.content_json || '{}');\nconst pattern = contentJson.pattern || '';\n\nconst patternToCategory = {\n  'versus': 'A',\n  'instant_hack': 'B',\n  'secret_feature': 'C',\n  'warning': 'D',\n  'ranking': 'E'\n};\n\nconst category = patternToCategory[pattern] || 'A';\nconst now = new Date();\nconst yearMonthDay = now.getFullYear().toString() + String(now.getMonth() + 1).padStart(2, '0') + String(now.getDate()).padStart(2, '0');\nconst folderName = `${yearMonthDay}投稿${category}`;\n\nreturn [{\n  json: {\n    ...item.json,\n    category: category,\n    folder_name: folderName,\n    pattern: pattern\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1504,
        480
      ],
      "id": "2a7f8aca-4730-47e7-9f62-6102c53f801f",
      "name": "Get Category from Pattern"
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "queryString": "={{ $json.folder_name }}",
        "filter": {
          "folderId": {
            "__rl": true,
            "value": "YOUR_PARENT_FOLDER_ID",
            "mode": "id"
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1264,
        480
      ],
      "id": "8065970a-5de7-4a6d-8bbc-0b4190251e70",
      "name": "Search Category Folder",
      "alwaysOutputData": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "queryString": "={{ $('Get Category from Pattern').item.json.post_id }}",
        "filter": {
          "folderId": {
            "__rl": true,
            "value": "={{ $json.id }}",
            "mode": "id"
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -1024,
        480
      ],
      "id": "b2c645c6-e0f3-41e5-b8bd-e52dfedebbd2",
      "name": "Search Video File",
      "alwaysOutputData": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "leftValue": "={{ $json.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              },
              "id": "file-exists-check"
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -784,
        480
      ],
      "id": "b7791881-106a-4303-8754-daac98d67b6f",
      "name": "IF File Exists"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/video/upload",
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "name": "file",
              "value": "=https://drive.google.com/uc?export=download&id={{ $json.id }}"
            },
            {
              "name": "upload_preset",
              "value": "YOUR_UPLOAD_PRESET"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -544,
        480
      ],
      "id": "6969b893-868f-4e27-aa4d-c79576016ce6",
      "name": "Upload to Cloudinary"
    },
    {
      "parameters": {
        "url": "https://graph.instagram.com/v20.0/me",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "user_id,username"
            },
            {
              "name": "access_token",
              "value": "YOUR_INSTAGRAM_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -2048,
        752
      ],
      "id": "0d72ad03-dab7-4283-acbc-407670c1b861",
      "name": "Get User ID"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.instagram.com/v20.0/{{ $json.user_id }}/media",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "media_type",
              "value": "REELS"
            },
            {
              "name": "video_url",
              "value": "={{ $('Upload to Cloudinary').item.json.secure_url }}"
            },
            {
              "name": "caption",
              "value": "={{ $('Get Category from Pattern').item.json.caption }}\n\n{{ $('Get Category from Pattern').item.json.hashtags }}"
            },
            {
              "name": "share_to_feed",
              "value": "true"
            },
            {
              "name": "access_token",
              "value": "YOUR_INSTAGRAM_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -1808,
        752
      ],
      "id": "01281c8d-75e9-4407-855c-c55c9c504a09",
      "name": "Create Reel Container"
    },
    {
      "parameters": {
        "url": "=https://graph.instagram.com/{{ $('Create Reel Container').item.json.id }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "status_code"
            },
            {
              "name": "access_token",
              "value": "YOUR_INSTAGRAM_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -1328,
        752
      ],
      "id": "cbfa5d4f-9430-4ffa-88ef-6334a651f9c7",
      "name": "Check Container Status"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.instagram.com/v20.0/{{ $('Get User ID').item.json.user_id }}/media_publish",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "creation_id",
              "value": "={{ $('Create Reel Container').item.json.id }}"
            },
            {
              "name": "access_token",
              "value": "YOUR_INSTAGRAM_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        -944,
        752
      ],
      "id": "c61a9fa9-516b-4740-b9a6-e32c09e61293",
      "name": "Publish Reel"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "set-postid",
              "name": "post_id",
              "value": "={{ $('Get Category from Pattern').item.json.post_id }}",
              "type": "string"
            },
            {
              "id": "set-status",
              "name": "status",
              "value": "PUBLISHED",
              "type": "string"
            },
            {
              "id": "set-igpostid",
              "name": "ig_post_id",
              "value": "={{ $json.id }}",
              "type": "string"
            },
            {
              "id": "set-publishedat",
              "name": "published_at",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -752,
        752
      ],
      "id": "b0c87483-fc96-4dfa-a9c9-8581a75cc78b",
      "name": "Edit Fields"
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "posts",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "post_id": "={{ $json.post_id }}",
            "status": "={{ $json.status }}",
            "ig_post_id": "={{ $json.ig_post_id }}",
            "published_at": "={{ $json.published_at }}"
          },
          "matchingColumns": [
            "post_id"
          ],
          "schema": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        -544,
        752
      ],
      "id": "4dd77806-8179-4291-8437-be3d01c127eb",
      "name": "Update Posts Sheet",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 0 6,12,18 * * *"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -2224,
        288
      ],
      "id": "298db14e-ee13-41c4-9c3a-71cb7dd14d51",
      "name": "Schedule Trigger1"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "6922c99a-9895-4ec2-8cef-dd56ed8a8b48",
              "leftValue": "={{ $json.status_code }}",
              "rightValue": "FINISHED",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -1136,
        752
      ],
      "id": "2be9aea1-0bdb-4445-930c-4f55d487ea42",
      "name": "If Reel Ready"
    },
    {
      "parameters": {
        "amount": 30
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        -1568,
        752
      ],
      "id": "ceef3824-1dc0-492d-becd-0300c3b0f442",
      "name": "Wait 30s",
      "webhookId": "wait-container"
    },
    {
      "parameters": {
        "command": "=ffmpeg -y -i \"https://drive.google.com/uc?export=download&id={{ $('Search Video File').item.json.id }}\" -ss 00:00:01 -frames:v 1 -q:v 2 /tmp/thumbnail.jpg 2>&1 && ffmpeg -y -i /tmp/thumbnail.jpg -vf \"scale=810:-1,pad=1080:1920:(ow-iw)/2:600:white,drawtext=text='新しい動画を投稿しました！':fontfile=/usr/share/fonts/noto/NotoSansCJK-Regular.ttc:fontsize=72:fontcolor=black:x=(w-text_w)/2:y=120\" /tmp/story.jpg 2>&1"
      },
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        -304,
        752
      ],
      "id": "story-extract-image",
      "name": "Extract Story Image"
    },
    {
      "parameters": {
        "command": "base64 /tmp/story.jpg | tr -d '\\n'"
      },
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        -64,
        752
      ],
      "id": "story-read-base64",
      "name": "Read Story Image"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.cloudinary.com/v1_1/YOUR_CLOUD_NAME/image/upload",
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "name": "file",
              "value": "=data:image/jpeg;base64,{{ $json.stdout }}"
            },
            {
              "name": "upload_preset",
              "value": "YOUR_UPLOAD_PRESET"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        176,
        752
      ],
      "id": "story-upload-cloudinary",
      "name": "Upload Story to Cloudinary"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.instagram.com/v20.0/{{ $('Get User ID').item.json.user_id }}/media",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "media_type",
              "value": "STORIES"
            },
            {
              "name": "image_url",
              "value": "={{ $json.secure_url }}"
            },
            {
              "name": "access_token",
              "value": "YOUR_INSTAGRAM_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        416,
        752
      ],
      "id": "story-create-container",
      "name": "Create Story Container"
    },
    {
      "parameters": {
        "amount": 10
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        656,
        752
      ],
      "id": "story-wait-10s",
      "name": "Wait 10s",
      "webhookId": "wait-story"
    },
    {
      "parameters": {
        "url": "=https://graph.instagram.com/{{ $('Create Story Container').item.json.id }}",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "fields",
              "value": "status_code"
            },
            {
              "name": "access_token",
              "value": "YOUR_INSTAGRAM_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        896,
        752
      ],
      "id": "story-check-status",
      "name": "Check Story Status"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "story-status-check",
              "leftValue": "={{ $json.status_code }}",
              "rightValue": "FINISHED",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1136,
        752
      ],
      "id": "story-if-finished",
      "name": "If Story Ready"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.instagram.com/v20.0/{{ $('Get User ID').item.json.user_id }}/media_publish",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "creation_id",
              "value": "={{ $('Create Story Container').item.json.id }}"
            },
            {
              "name": "access_token",
              "value": "YOUR_INSTAGRAM_ACCESS_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1376,
        640
      ],
      "id": "story-publish",
      "name": "Publish Story"
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "YOUR_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "posts",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "post_id": "={{ $('Get Category from Pattern').item.json.post_id }}",
            "status": "STORY_POSTED"
          },
          "matchingColumns": [
            "post_id"
          ],
          "schema": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1616,
        640
      ],
      "id": "story-update-status",
      "name": "Update Story Status",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "YOUR_CREDENTIAL_ID",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "When clicking 'Execute workflow'": {
      "main": [
        []
      ]
    },
    "Get DRAFT Posts": {
      "main": [
        [
          {
            "node": "Loop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop": {
      "main": [
        [],
        [
          {
            "node": "Get Category from Pattern",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Category from Pattern": {
      "main": [
        [
          {
            "node": "Search Category Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Category Folder": {
      "main": [
        [
          {
            "node": "Search Video File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Video File": {
      "main": [
        [
          {
            "node": "IF File Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF File Exists": {
      "main": [
        [
          {
            "node": "Upload to Cloudinary",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload to Cloudinary": {
      "main": [
        [
          {
            "node": "Get User ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get User ID": {
      "main": [
        [
          {
            "node": "Create Reel Container",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Reel Container": {
      "main": [
        [
          {
            "node": "Wait 30s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Container Status": {
      "main": [
        [
          {
            "node": "If Reel Ready",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Publish Reel": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Update Posts Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger1": {
      "main": [
        [
          {
            "node": "Get DRAFT Posts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Reel Ready": {
      "main": [
        [
          {
            "node": "Publish Reel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait 30s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 30s": {
      "main": [
        [
          {
            "node": "Check Container Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Posts Sheet": {
      "main": [
        [
          {
            "node": "Extract Story Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Story Image": {
      "main": [
        [
          {
            "node": "Read Story Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Story Image": {
      "main": [
        [
          {
            "node": "Upload Story to Cloudinary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload Story to Cloudinary": {
      "main": [
        [
          {
            "node": "Create Story Container",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Story Container": {
      "main": [
        [
          {
            "node": "Wait 10s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 10s": {
      "main": [
        [
          {
            "node": "Check Story Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Story Status": {
      "main": [
        [
          {
            "node": "If Story Ready",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Story Ready": {
      "main": [
        [
          {
            "node": "Publish Story",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait 10s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Publish Story": {
      "main": [
        [
          {
            "node": "Update Story Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Story Status": {
      "main": [
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "integrated-placeholder",
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": ""
  },
  "id": "reel-story-integrated",
  "tags": []
}
