{
	"name": "Log every live sprkly post in a Google Sheet",
	"nodes": [
		{
			"id": "26c19951-0a3a-485f-b748-4654b198fbbc",
			"name": "Read me first",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-1080,
				60
			],
			"parameters": {
				"content": "## Log every live sprkly post in a Google Sheet\nEach post that goes live gets one row in your sheet, with its account, time and link.\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=post-published-to-google-sheets-log\n- A Google Sheets login and a sheet with: Post ID, Account, Platform, Published At, Link\n- A Telegram bot and your chat ID, for the failure alert\n\n### Set it up\n1. Add your sprkly key to **When a post goes out** and **Get account names**.\n2. Add your Google login to **Add or update the log row** and pick your sheet.\n3. In **Settings**, set your time zone, like Europe/London.\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. The next post that goes live adds a row.\n- Posts that went out before are not added.",
				"height": 754,
				"width": 560,
				"color": 4
			}
		},
		{
			"id": "83ba50c0-9c04-48b1-a80e-06ea74073934",
			"name": "Note: check the post",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-440,
				60
			],
			"parameters": {
				"content": "## Check the post\nOnly a published post with an ID goes on. The account list turns the account ID into its name.",
				"height": 600,
				"width": 1120,
				"color": 7
			}
		},
		{
			"id": "66548e99-6918-4b6e-b17d-7b621f352718",
			"name": "Note: one row per post",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				700,
				60
			],
			"parameters": {
				"content": "## One row per post\nPost ID is the match column. A second run for the same post updates its row, so no post is logged twice.",
				"height": 600,
				"width": 480,
				"color": 7
			}
		},
		{
			"id": "048756f5-d156-4086-8041-52073a7c36c4",
			"name": "When a post goes out",
			"type": "n8n-nodes-sprkly.sprklyTrigger",
			"typeVersion": 1,
			"position": [
				-400,
				320
			],
			"parameters": {
				"pollTimes": {
					"item": [
						{
							"mode": "everyX",
							"value": 5,
							"unit": "minutes"
						}
					]
				},
				"event": "postPublished"
			}
		},
		{
			"id": "94cba50d-4090-4898-b763-65021ded9e07",
			"name": "Settings",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				-180,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "timeZone",
							"type": "string",
							"value": "UTC"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "8df743b8-01f5-4f8f-8de5-4a78dc1e2a0d",
			"name": "Is it a whole published post?",
			"type": "n8n-nodes-base.if",
			"typeVersion": 2.2,
			"position": [
				40,
				320
			],
			"parameters": {
				"conditions": {
					"options": {
						"caseSensitive": true,
						"leftValue": "",
						"typeValidation": "loose",
						"version": 2
					},
					"conditions": [
						{
							"id": "has-id",
							"leftValue": "={{ String($json.id || '') }}",
							"rightValue": "",
							"operator": {
								"type": "string",
								"operation": "notEmpty",
								"singleValue": true
							}
						},
						{
							"id": "status",
							"leftValue": "={{ $json.status }}",
							"rightValue": "posted",
							"operator": {
								"type": "string",
								"operation": "equals"
							}
						}
					],
					"combinator": "and"
				},
				"looseTypeValidation": true,
				"options": {}
			}
		},
		{
			"id": "9acf23ab-7858-4744-966e-852f5956d3e3",
			"name": "Get account names",
			"type": "n8n-nodes-sprkly.sprkly",
			"typeVersion": 1.1,
			"position": [
				280,
				480
			],
			"parameters": {
				"resource": "account",
				"operation": "getAll",
				"returnAll": true
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "b54c6680-b3ad-486d-950e-c64b2b8f602a",
			"name": "Wait for both",
			"type": "n8n-nodes-base.merge",
			"typeVersion": 3.2,
			"position": [
				500,
				320
			],
			"parameters": {
				"mode": "chooseBranch",
				"useDataOfInput": 1
			}
		},
		{
			"id": "b983a08d-fd57-453a-a7b5-f83dda70dbcf",
			"name": "Build the row",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				740,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "postId",
							"type": "string",
							"value": "={{ $json.id }}"
						},
						{
							"id": "f2",
							"name": "account",
							"type": "string",
							"value": "={{ ($('Get account names').all().find(a => a.json.id === ($json.profileIds || [])[0]) || { json: {} }).json.handle || 'unknown account' }}"
						},
						{
							"id": "f3",
							"name": "platform",
							"type": "string",
							"value": "={{ String(($json.platforms || [])[0] || 'unknown') }}"
						},
						{
							"id": "f4",
							"name": "publishedAt",
							"type": "string",
							"value": "={{ $json.publishedAt ? DateTime.fromISO($json.publishedAt).setZone($json.timeZone || 'UTC').toFormat('yyyy-MM-dd HH:mm') + ' ' + ($json.timeZone || 'UTC') : 'no time set' }}"
						},
						{
							"id": "f5",
							"name": "postLink",
							"type": "string",
							"value": "={{ $json.link || 'https://sprkly.app/schedule?utm_source=n8n&utm_medium=workflow&utm_campaign=post-published-to-google-sheets-log&post=' + encodeURIComponent($json.id) }}"
						}
					]
				},
				"includeOtherFields": false,
				"options": {}
			}
		},
		{
			"id": "b73bbb0c-4af0-450b-b2e1-faa8860c5c6e",
			"name": "Add or update the log row",
			"type": "n8n-nodes-base.googleSheets",
			"typeVersion": 4.7,
			"position": [
				960,
				320
			],
			"parameters": {
				"operation": "appendOrUpdate",
				"documentId": {
					"__rl": true,
					"value": "",
					"mode": "url"
				},
				"sheetName": {
					"__rl": true,
					"value": "",
					"mode": "list"
				},
				"columns": {
					"mappingMode": "defineBelow",
					"value": {
						"Post ID": "={{ $json.postId }}",
						"Account": "={{ $json.account }}",
						"Platform": "={{ $json.platform }}",
						"Published At": "={{ $json.publishedAt }}",
						"Link": "={{ $json.postLink }}"
					},
					"matchingColumns": [
						"Post ID"
					],
					"schema": [
						{
							"id": "Post ID",
							"displayName": "Post ID",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Account",
							"displayName": "Account",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Platform",
							"displayName": "Platform",
							"required": false,
							"defaultMatch": false,
							"display": true,
							"type": "string",
							"canBeUsedToMatch": true,
							"removed": false
						},
						{
							"id": "Published At",
							"displayName": "Published At",
							"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
						}
					]
				},
				"options": {}
			}
		},
		{
			"id": "a627d3fc-9487-48a8-919f-99c448036123",
			"name": "When this workflow fails",
			"type": "n8n-nodes-base.errorTrigger",
			"typeVersion": 1,
			"position": [
				-1040,
				894
			],
			"parameters": {}
		},
		{
			"id": "f06d673c-323d-4b43-9cd5-1d8ec8252cfd",
			"name": "Write the failure alert",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				-820,
				894
			],
			"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": "0086e5ae-168e-4242-91fb-4cac96f644d1",
			"name": "Send the failure alert",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				-600,
				894
			],
			"parameters": {
				"chatId": "={{ $json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		}
	],
	"connections": {
		"When a post goes out": {
			"main": [
				[
					{
						"node": "Settings",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Settings": {
			"main": [
				[
					{
						"node": "Is it a whole published post?",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Is it a whole published post?": {
			"main": [
				[
					{
						"node": "Wait for both",
						"type": "main",
						"index": 0
					},
					{
						"node": "Get account names",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Get account names": {
			"main": [
				[
					{
						"node": "Wait for both",
						"type": "main",
						"index": 1
					}
				]
			]
		},
		"Wait for both": {
			"main": [
				[
					{
						"node": "Build the row",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Build the row": {
			"main": [
				[
					{
						"node": "Add or update the log row",
						"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
					}
				]
			]
		}
	},
	"pinData": {},
	"active": false,
	"settings": {
		"executionOrder": "v1"
	}
}
