{
  "name": "New YouTube video to sprkly",
  "nodes": [
    {
      "id": "f9d96846-cb02-428a-a5ad-11fb2d1d3f52",
      "name": "Read me first",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -1640,
        -630
      ],
      "parameters": {
        "content": "## New YouTube video to sprkly\nEach new video on your channel becomes a post on your other accounts, with its thumbnail. Shorts and old videos are skipped, and every video gets a log row.\n\n### You need\n- The sprkly node: Settings, Community nodes, install `n8n-nodes-sprkly` (self-hosted n8n)\n- A sprkly API key: https://sprkly.app/docs/api?utm_source=n8n&utm_medium=workflow&utm_campaign=youtube-to-sprkly\n- A Telegram bot and your chat ID\n- A Google Sheets login, a Video Announcements tab (Video ID, Title, Link, Status, Post ID, Goes Out, Error) and a sheet for the log of live posts\n\n### Set it up\n1. In **New video on your channel**, swap YOUR_CHANNEL_ID for your channel ID. It starts with UC.\n2. In **Settings**, write your accounts as platform:handle, with commas. Leave YouTube out. Add your chat ID. Turn on includeShorts or holdForOK if you want them.\n3. Add your sprkly key and your bot where a step asks. Pick your sheet in **Log the video**, and put its ID in **Published log settings**.\n4. Failure alert: put your chat ID in **Write the failure alert** and your bot in **Send the failure alert**. Then in Workflow settings, set Error workflow to this workflow.\n\n### Test it\n- Turn the workflow on and wait for your next video.\n- You get a Telegram message with the time the post goes out, and the video gets a log row.",
        "height": 884,
        "width": 560,
        "color": 4
      }
    },
    {
      "id": "57cb542f-5c4f-4da9-9b5e-95b5acc85d24",
      "name": "Note: announce",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -440,
        60
      ],
      "parameters": {
        "content": "## Announce the video\nNo YouTube login is needed. The feed is public. Instagram and TikTok need an image, so the thumbnail goes on every post. Shorts and videos older than 2 days are skipped unless you change **Settings**. Every video gets a row in your log.",
        "height": 440,
        "width": 1400,
        "color": 7
      }
    },
    {
      "id": "c551175a-e8f5-4e8a-944c-4ff6c93530e5",
      "name": "New video on your channel",
      "type": "n8n-nodes-base.rssFeedReadTrigger",
      "typeVersion": 1,
      "position": [
        -400,
        320
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyX",
              "value": 15,
              "unit": "minutes"
            }
          ]
        },
        "feedUrl": "https://www.youtube.com/feeds/videos.xml?channel_id=YOUR_CHANNEL_ID"
      }
    },
    {
      "id": "af54660b-3cf5-4d47-a627-87ccdd75101a",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -180,
        320
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f1",
              "name": "accounts",
              "type": "string",
              "value": "instagram:yourhandle, facebook:Your Page, x:yourhandle, threads:yourhandle"
            },
            {
              "id": "f2",
              "name": "telegramChatId",
              "type": "string",
              "value": "YOUR_CHAT_ID"
            },
            {
              "id": "f3",
              "name": "includeShorts",
              "type": "boolean",
              "value": false
            },
            {
              "id": "f4",
              "name": "maxAgeHours",
              "type": "number",
              "value": 48
            },
            {
              "id": "f5",
              "name": "holdForOK",
              "type": "boolean",
              "value": false
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      }
    },
    {
      "id": "9733d356-702d-49dd-9cb0-468ef4223cff",
      "name": "Announce in sprkly",
      "type": "n8n-nodes-sprkly.sprkly",
      "typeVersion": 1.1,
      "position": [
        280,
        320
      ],
      "parameters": {
        "resource": "post",
        "operation": "create",
        "accountMode": "name",
        "accountNames": "={{ $json.accounts }}",
        "caption": "={{ 'New video: ' + $json.title + '\\n\\n' + $json.link }}",
        "scheduledTime": "={{ $now.plus({ minutes: 15 }).toISO() }}",
        "output": "resultRow",
        "additionalFields": {
          "mediaUrls": "={{ 'https://i.ytimg.com/vi/' + String($json.id || '').replace('yt:video:', '') + '/hqdefault.jpg' }}",
          "externalId": "={{ String($json.id || '').replace('yt:video:', '') ? 'yt-' + String($json.id || '').replace('yt:video:', '') : '' }}",
          "requireExternalId": true,
          "requiresApproval": "={{ $json.holdForOK === true }}"
        }
      }
    },
    {
      "id": "b18f3e55-2300-4c88-8ba8-fec0b5b45196",
      "name": "Send result to Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        500,
        320
      ],
      "parameters": {
        "chatId": "={{ $('Settings').item.json.telegramChatId }}",
        "text": "={{ $json.ok ? 'Video post scheduled for ' + $json.scheduledFor + ': ' + $json.title : 'Video post not scheduled: ' + $json.title + '. ' + $json.error }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    },
    {
      "name": "Note: automatic published post log",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "id": "c89a1dbd-2aa4-5aba-a7c9-ddd9bf8f89cd",
      "position": [
        -1000,
        -630
      ],
      "parameters": {
        "content": "## Log posts as they go live\n\nSet **Spreadsheet ID** in **Published log settings**. Use the same Google Sheets credential in the two log steps and your sprkly API credential on **When this workflow's post goes live**.\n\nThe workflow checks each minute for its published posts. It creates **Published Posts** and its headers as needed, then writes one row per Post ID. This branch runs after publication. It leaves the scheduling and source status branches intact.",
        "height": 215,
        "width": 520,
        "color": 5
      }
    },
    {
      "name": "When this workflow's post goes live",
      "type": "n8n-nodes-sprkly.sprklyTrigger",
      "typeVersion": 1,
      "id": "4b78d9ea-375a-5b27-adcb-e3963714b12d",
      "position": [
        -1000,
        -380
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "event": "postPublished",
        "externalIdPrefix": "yt-",
        "stripPrefix": false
      }
    },
    {
      "name": "Build published log rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "id": "22f102fb-4113-5876-b8d0-4c7906626dae",
      "position": [
        -760,
        -380
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const prefix = \"yt-\";\nconst text = value => typeof value === 'string' || typeof value === 'number' ? String(value).trim() : '';\nconst values = input => Array.isArray(input) ? input.map(text).filter(Boolean) : [];\nconst rows = new Map();\n\nfor (const [index, item] of $input.all().entries()) {\n  const post = item.json;\n  const id = text(post.id);\n  const externalId = text(post.externalId);\n  if (!id || post.deleted || !['posted', 'published'].includes(post.status) ||\n      !externalId.startsWith(prefix) || externalId.length === prefix.length) continue;\n\n  const platforms = values(post.platforms);\n  const accounts = values(post.profileIds);\n  const suffix = ':' + platforms[0] + ':' + accounts[0];\n  const source = platforms.length && accounts.length && externalId.endsWith(suffix)\n    ? externalId.slice(0, -suffix.length) : externalId;\n  if (source.length <= prefix.length) continue;\n  const previous = rows.get(id)?.json;\n  rows.set(id, {\n    json: {\n      'Post ID': id,\n      Status: 'Published',\n      'Published At': text(post.publishedAt) || previous?.['Published At'] || '',\n      Platform: platforms.join(', '),\n      'Account ID': accounts.join(', '),\n      'Source ID': source,\n      'External ID': externalId,\n      'Live URL': text(post.permalink) || text(post.link) || previous?.['Live URL'] || '',\n      'Sprkly URL': 'https://sprkly.app/schedule?utm_source=n8n&utm_medium=workflow&utm_campaign=youtube-to-sprkly&post=' + encodeURIComponent(id),\n    },\n    pairedItem: index,\n  });\n}\nreturn [...rows.values()];"
      }
    },
    {
      "name": "Published log settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "id": "270b4ec0-684e-54f9-824c-0a416275f186",
      "position": [
        -520,
        -380
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "adcef569-c23f-5771-9f57-53fe0e79a6db",
              "name": "Spreadsheet ID",
              "type": "string",
              "value": ""
            },
            {
              "id": "fb9b0286-06b4-5527-8b9c-dbfb070f797f",
              "name": "Tab name",
              "type": "string",
              "value": "Published Posts"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "executeOnce": true
    },
    {
      "name": "Ensure published log tab",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "id": "95398f48-f0a7-5798-890f-1d04c3e91643",
      "position": [
        -280,
        -380
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "create",
        "documentId": {
          "__rl": true,
          "value": "={{ String($json[\"Spreadsheet ID\"]).trim() }}",
          "mode": "id"
        },
        "title": "={{ String($json[\"Tab name\"]).trim() }}",
        "options": {}
      },
      "alwaysOutputData": true,
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "name": "Restore published log rows",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "id": "82cdc708-2775-5319-8c70-e260c0a05ae0",
      "position": [
        -40,
        -380
      ],
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Create Sheet has one output for the setup call. Restore every publication in the trigger batch.\nreturn $('Build published log rows').all().map(({ json }) => ({ json, pairedItem: 0 }));"
      }
    },
    {
      "name": "Upsert published post log",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "id": "f1205586-39af-5ffd-87a6-5821f20d038a",
      "position": [
        200,
        -380
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "={{ String($(\"Published log settings\").first().json[\"Spreadsheet ID\"]).trim() }}",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "={{ String($(\"Published log settings\").first().json[\"Tab name\"]).trim() }}",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "matchingColumns": [
            "Post ID"
          ],
          "schema": []
        },
        "options": {
          "cellFormat": "RAW",
          "handlingExtraData": "insertInNewColumn",
          "useAppend": true
        }
      },
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "id": "63c610cd-9a4d-4468-acfa-a2ce04aa20ae",
      "name": "When this workflow fails",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -1600,
        334
      ],
      "parameters": {}
    },
    {
      "id": "8db85f03-7d0e-4fde-bf49-a316cb6bbfd5",
      "name": "Write the failure alert",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -1380,
        334
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "f1",
              "name": "telegramChatId",
              "type": "string",
              "value": "YOUR_TELEGRAM_CHAT_ID"
            },
            {
              "id": "f2",
              "name": "message",
              "type": "string",
              "value": "={{ 'A workflow stopped.\\n\\nWorkflow: ' + (($json.workflow || {}).name || 'unknown') + '\\nFailed step: ' + (($json.execution || {}).lastNodeExecuted || 'unknown') + '\\nWhy: ' + String((($json.execution || {}).error || {}).message || 'n8n did not give a reason.').slice(0, 300) + (($json.execution || {}).url ? '\\nOpen the run: ' + $json.execution.url : '') }}"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      }
    },
    {
      "id": "3b214460-e48f-44d1-a89c-a920f7bf4507",
      "name": "Send the failure alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -1160,
        334
      ],
      "parameters": {
        "chatId": "={{ $json.telegramChatId }}",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "585d33c7-be5f-427a-b344-7945e8b4f552",
      "name": "New and wanted?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        40,
        320
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ (!DateTime.fromISO(String($json.isoDate || '')).isValid || DateTime.fromISO(String($json.isoDate)) >= $now.minus({ hours: Number($json.maxAgeHours) || 48 })) && ($json.includeShorts === true || !/\\/shorts\\//.test(String($json.link || ''))) }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      }
    },
    {
      "id": "7bade47a-824e-4fe6-9871-6ad882ad853f",
      "name": "Log the video",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        720,
        320
      ],
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "",
          "mode": "url"
        },
        "sheetName": {
          "__rl": true,
          "value": "Video Announcements",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Video ID": "={{ String($('Settings').item.json.id || '').replace('yt:video:', '') }}",
            "Title": "={{ $('Settings').item.json.title }}",
            "Link": "={{ $('Settings').item.json.link }}",
            "Status": "={{ $('Announce in sprkly').item.json.status }}",
            "Post ID": "={{ $('Announce in sprkly').item.json.postId }}",
            "Goes Out": "={{ $('Announce in sprkly').item.json.scheduledFor }}",
            "Error": "={{ typeof $('Announce in sprkly').item.json.error === 'string' ? $('Announce in sprkly').item.json.error : '' }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Video ID",
              "displayName": "Video ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Title",
              "displayName": "Title",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Link",
              "displayName": "Link",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Post ID",
              "displayName": "Post ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Goes Out",
              "displayName": "Goes Out",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Error",
              "displayName": "Error",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "New video on your channel": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "New and wanted?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Announce in sprkly": {
      "main": [
        [
          {
            "node": "Send result to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When this workflow's post goes live": {
      "main": [
        [
          {
            "node": "Build published log rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build published log rows": {
      "main": [
        [
          {
            "node": "Published log settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Published log settings": {
      "main": [
        [
          {
            "node": "Ensure published log tab",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ensure published log tab": {
      "main": [
        [
          {
            "node": "Restore published log rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restore published log rows": {
      "main": [
        [
          {
            "node": "Upsert published post log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When this workflow fails": {
      "main": [
        [
          {
            "node": "Write the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the failure alert": {
      "main": [
        [
          {
            "node": "Send the failure alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "New and wanted?": {
      "main": [
        [
          {
            "node": "Announce in sprkly",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send result to Telegram": {
      "main": [
        [
          {
            "node": "Log the video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}
