{
	"name": "Telegram message when a sprkly post fails",
	"nodes": [
		{
			"id": "e300779d-9d7f-4279-be8b-375bdb0c849d",
			"name": "Read me first",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-1080,
				60
			],
			"parameters": {
				"content": "## Telegram message when a sprkly post fails\nWhen a post fails you get a Telegram message with the reason and a link, and every Monday a count of the week by account and reason. Nothing is sent again.\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-failed-to-telegram\n- A Telegram bot and your chat ID\n\n### Set it up\n1. Add your sprkly key to the 4 sprkly steps.\n2. Add your bot to the 2 Telegram steps. Put your chat ID in **Settings**.\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 fails sends a message. Posts that failed before are not sent.\n- Click Test workflow with **Every Monday** picked to see the roll-up.",
				"height": 728,
				"width": 560,
				"color": 4
			}
		},
		{
			"id": "fdbe7a7c-6919-472a-b08a-0f4e9bfd3437",
			"name": "Note: check the post",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-440,
				60
			],
			"parameters": {
				"content": "## Check the post\nOnly a failed post with an ID goes on. The account list turns the account ID into its name.",
				"height": 600,
				"width": 1360,
				"color": 7
			}
		},
		{
			"id": "9a5d442d-1d68-4481-a3ba-de6d1037748a",
			"name": "Note: send the alert",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				940,
				60
			],
			"parameters": {
				"content": "## Send the alert\nThe message has the account, the time, the reason and a link. Nothing here posts or retries.",
				"height": 600,
				"width": 700,
				"color": 7
			}
		},
		{
			"id": "5d3c782e-fd6f-47f6-8e9c-e78aaf2ad1ab",
			"name": "When a post fails",
			"type": "n8n-nodes-sprkly.sprklyTrigger",
			"typeVersion": 1,
			"position": [
				-400,
				320
			],
			"parameters": {
				"pollTimes": {
					"item": [
						{
							"mode": "everyX",
							"value": 5,
							"unit": "minutes"
						}
					]
				},
				"event": "postFailed"
			}
		},
		{
			"id": "fa03805c-c4c5-40c0-a5c1-f7411467e0c7",
			"name": "Settings",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				-180,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "telegramChatId",
							"type": "string",
							"value": "YOUR_CHAT_ID"
						},
						{
							"id": "f2",
							"name": "timeZone",
							"type": "string",
							"value": "UTC"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "c585c3e7-4f18-47a7-b8e5-75323a85b1ad",
			"name": "Is it a whole failed post?",
			"type": "n8n-nodes-base.if",
			"typeVersion": 2.2,
			"position": [
				280,
				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": "failed",
							"operator": {
								"type": "string",
								"operation": "equals"
							}
						}
					],
					"combinator": "and"
				},
				"looseTypeValidation": true,
				"options": {}
			}
		},
		{
			"id": "4b9ed033-f761-4dd9-b1cf-674ad3e4360a",
			"name": "Get account names",
			"type": "n8n-nodes-sprkly.sprkly",
			"typeVersion": 1.1,
			"position": [
				520,
				480
			],
			"parameters": {
				"resource": "account",
				"operation": "getAll",
				"returnAll": true
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "1e03307e-3deb-410d-887c-16d5cca44a6b",
			"name": "Wait for both",
			"type": "n8n-nodes-base.merge",
			"typeVersion": 3.2,
			"position": [
				740,
				320
			],
			"parameters": {
				"mode": "chooseBranch",
				"useDataOfInput": 1
			}
		},
		{
			"id": "522d1aa3-4066-433f-98b6-7cc15abbfee4",
			"name": "Pick the details",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				980,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "account",
							"type": "string",
							"value": "={{ ($('Get account names').all().find(a => a.json.id === ($json.profileIds || [])[0]) || { json: {} }).json.handle || 'unknown account' }}"
						},
						{
							"id": "f2",
							"name": "platform",
							"type": "string",
							"value": "={{ String(($json.platforms || [])[0] || 'unknown') }}"
						},
						{
							"id": "f3",
							"name": "setFor",
							"type": "string",
							"value": "={{ $json.scheduledTime ? DateTime.fromISO($json.scheduledTime).setZone($json.timeZone || 'UTC').toFormat('d LLL yyyy, HH:mm') + ' ' + ($json.timeZone || 'UTC') : 'no time set' }}"
						},
						{
							"id": "f4",
							"name": "caption",
							"type": "string",
							"value": "={{ String($json.caption || '').length > 80 ? String($json.caption).slice(0, 77) + '...' : String($json.caption || '(no caption)') }}"
						},
						{
							"id": "f5",
							"name": "reason",
							"type": "string",
							"value": "={{ $json.error || 'sprkly did not give a reason.' }}"
						},
						{
							"id": "f6",
							"name": "sprklyLink",
							"type": "string",
							"value": "={{ 'https://sprkly.app/schedule?utm_source=n8n&utm_medium=workflow&utm_campaign=post-failed-to-telegram&post=' + encodeURIComponent($json.id) }}"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "6031594f-3737-420e-b2e6-dc0b0b1b6e58",
			"name": "Write the message",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				1200,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "message",
							"type": "string",
							"value": "={{ 'A post did not go out.\\n\\nAccount: ' + $json.platform + ' ' + $json.account + '\\nSet for: ' + $json.setFor + '\\nPost: ' + $json.caption + '\\nWhy: ' + $json.reason + '\\n\\nsprkly will not send it again. Open it in sprkly to see what to fix:\\n' + $json.sprklyLink }}"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "388c7376-0676-41f8-a573-cce02456cf25",
			"name": "Send to Telegram",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				1420,
				320
			],
			"parameters": {
				"chatId": "={{ $json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		},
		{
			"id": "5357e622-6b87-4fcf-813f-905d39d74752",
			"name": "When this workflow fails",
			"type": "n8n-nodes-base.errorTrigger",
			"typeVersion": 1,
			"position": [
				-1040,
				868
			],
			"parameters": {}
		},
		{
			"id": "8dbc8c61-6ec7-4388-a843-098ab0e686ca",
			"name": "Write the failure alert",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				-820,
				868
			],
			"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": "325cb240-46bf-42f0-a500-783c6767d2f3",
			"name": "Send the failure alert",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				-600,
				868
			],
			"parameters": {
				"chatId": "={{ $json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		},
		{
			"id": "889cf90c-7355-4fce-a9b3-ca8f6671d3dd",
			"name": "Every Monday",
			"type": "n8n-nodes-base.scheduleTrigger",
			"typeVersion": 1.3,
			"position": [
				-400,
				500
			],
			"parameters": {
				"rule": {
					"interval": [
						{
							"field": "weeks",
							"triggerAtDay": [
								1
							],
							"triggerAtHour": 9
						}
					]
				}
			}
		},
		{
			"id": "04f83356-feac-41ae-b7e3-a1ab107ab38f",
			"name": "Monday roll-up?",
			"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": "={{ $('Every Monday').isExecuted }}",
							"rightValue": "",
							"operator": {
								"type": "boolean",
								"operation": "true",
								"singleValue": true
							}
						}
					],
					"combinator": "and"
				},
				"looseTypeValidation": true,
				"options": {}
			}
		},
		{
			"id": "73497822-c5ff-4a6a-a5ab-439f36f40725",
			"name": "Note: weekly roll-up",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				240,
				700
			],
			"parameters": {
				"content": "## Monday roll-up\nEvery Monday: the posts that failed in the last 7 days, by account and by reason. It only reads.",
				"height": 320,
				"width": 1160,
				"color": 7
			}
		},
		{
			"id": "0ae321f6-688a-412d-8314-d543ac64d69a",
			"name": "Get failed posts",
			"type": "n8n-nodes-sprkly.sprkly",
			"typeVersion": 1.1,
			"position": [
				280,
				820
			],
			"parameters": {
				"resource": "post",
				"operation": "getAll",
				"returnAll": true,
				"filters": {
					"status": "failed"
				},
				"simplify": true
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "1790c829-a2a1-4824-8c02-2ab0b7330d3b",
			"name": "Get account names for the roll-up",
			"type": "n8n-nodes-sprkly.sprkly",
			"typeVersion": 1.1,
			"position": [
				500,
				820
			],
			"parameters": {
				"resource": "account",
				"operation": "getAll",
				"returnAll": true
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "3f6e5e21-f223-4e27-9119-72e85842473b",
			"name": "Write the roll-up",
			"type": "n8n-nodes-base.code",
			"typeVersion": 2,
			"position": [
				720,
				820
			],
			"parameters": {
				"mode": "runOnceForAllItems",
				"jsCode": "const since = $now.minus({ days: 7 });\nconst names = new Map($('Get account names for the roll-up').all().map(a => [a.json.id, String(a.json.platform || '') + ' ' + String(a.json.handle || '')]));\nconst failed = $('Get failed posts').all().map(i => i.json).filter(p => p.id && !p.deleted && p.scheduledTime && DateTime.fromISO(p.scheduledTime) >= since);\nif (failed.length === 0) return [{ json: { message: 'No sprkly posts failed in the last 7 days.' } }];\nconst count = (list, key) => { const m = {}; for (const x of list) { const k = key(x); m[k] = (m[k] || 0) + 1; } return Object.entries(m).sort((a, b) => b[1] - a[1]); };\nconst byAccount = count(failed, p => names.get((p.profileIds || [])[0]) || 'unknown account');\nconst reason = p => { const e = typeof p.error === 'string' ? p.error : (p.error && p.error.message) || 'no reason given'; return e.length > 80 ? e.slice(0, 77) + '...' : e; };\nconst byReason = count(failed, reason);\nreturn [{ json: { message: 'Posts that failed in the last 7 days: ' + failed.length\n  + '\\n\\nBy account:\\n' + byAccount.slice(0, 10).map(([k, n]) => '- ' + k + ': ' + n).join('\\n')\n  + '\\n\\nMost common reasons:\\n' + byReason.slice(0, 3).map(([k, n]) => '- ' + k + ' (' + n + ')').join('\\n')\n  + '\\n\\nsprkly never sends a failed post again. See them in sprkly: https://sprkly.app/schedule?utm_source=n8n&utm_medium=workflow&utm_campaign=post-failed-to-telegram' } }];"
			}
		},
		{
			"id": "bab4b4d1-5aa6-444b-a824-013bcff7b2b9",
			"name": "Send the roll-up",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				940,
				820
			],
			"parameters": {
				"chatId": "={{ $('Settings').first().json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		}
	],
	"connections": {
		"When a post fails": {
			"main": [
				[
					{
						"node": "Settings",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Settings": {
			"main": [
				[
					{
						"node": "Monday roll-up?",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Is it a whole failed 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": "Pick the details",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Pick the details": {
			"main": [
				[
					{
						"node": "Write the message",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Write the message": {
			"main": [
				[
					{
						"node": "Send to Telegram",
						"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
					}
				]
			]
		},
		"Every Monday": {
			"main": [
				[
					{
						"node": "Settings",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Monday roll-up?": {
			"main": [
				[
					{
						"node": "Get failed posts",
						"type": "main",
						"index": 0
					}
				],
				[
					{
						"node": "Is it a whole failed post?",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Get failed posts": {
			"main": [
				[
					{
						"node": "Get account names for the roll-up",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Get account names for the roll-up": {
			"main": [
				[
					{
						"node": "Write the roll-up",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Write the roll-up": {
			"main": [
				[
					{
						"node": "Send the roll-up",
						"type": "main",
						"index": 0
					}
				]
			]
		}
	},
	"pinData": {},
	"active": false,
	"settings": {
		"executionOrder": "v1"
	}
}
