{
	"name": "Warning when your sprkly queue runs low",
	"nodes": [
		{
			"id": "1bed23e2-d959-4318-be0e-153cff015a71",
			"name": "Read me first",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-1080,
				60
			],
			"parameters": {
				"content": "## Warning when your sprkly queue runs low\nEach morning it counts the posts set for the next 2 days and tells you on Telegram when there are too few.\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=queue-running-low-alert\n- A Telegram bot and your chat ID\n\n### Set it up\n1. Add your sprkly key to **Get scheduled posts**.\n2. Add your bot to **Send to Telegram**. Put your chat ID in **Settings**.\n3. In **Settings**, set daysAhead and minimumPosts. Set your time zone in Workflow 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- Set minimumPosts to 100 and run the workflow once. You get the warning. Set it back after.",
				"height": 676,
				"width": 560,
				"color": 4
			}
		},
		{
			"id": "4e29b08d-62bf-4e5c-a861-55ff8bd5ed04",
			"name": "Note: count the queue",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				-440,
				60
			],
			"parameters": {
				"content": "## Count the queue\nGets every scheduled post and counts the ones inside the next daysAhead days.",
				"height": 520,
				"width": 900,
				"color": 7
			}
		},
		{
			"id": "5bc632b2-8742-4220-8839-73a31428f5cf",
			"name": "Note: warn when low",
			"type": "n8n-nodes-base.stickyNote",
			"typeVersion": 1,
			"position": [
				480,
				60
			],
			"parameters": {
				"content": "## Warn when low\nA message goes out only when the count is under minimumPosts.",
				"height": 520,
				"width": 480,
				"color": 7
			}
		},
		{
			"id": "15c98197-de92-428c-8a9d-89f90a47e8bc",
			"name": "Every morning",
			"type": "n8n-nodes-base.scheduleTrigger",
			"typeVersion": 1.3,
			"position": [
				-400,
				320
			],
			"parameters": {
				"rule": {
					"interval": [
						{
							"field": "days",
							"triggerAtHour": 9
						}
					]
				}
			}
		},
		{
			"id": "82eb8d6f-85d4-48f2-ac46-dbf079b8ff58",
			"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": "daysAhead",
							"type": "number",
							"value": 2
						},
						{
							"id": "f3",
							"name": "minimumPosts",
							"type": "number",
							"value": 1
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "8c9d4bcc-789a-4402-8808-33ac0cf8ca8a",
			"name": "Get scheduled posts",
			"type": "n8n-nodes-sprkly.sprkly",
			"typeVersion": 1.1,
			"position": [
				40,
				320
			],
			"parameters": {
				"resource": "post",
				"operation": "getAll",
				"returnAll": true,
				"filters": {
					"status": "scheduled"
				},
				"simplify": true
			},
			"executeOnce": true,
			"alwaysOutputData": true
		},
		{
			"id": "a275f15c-4aa4-400d-9703-051ed08c75ea",
			"name": "Count upcoming posts",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				260,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "upcoming",
							"type": "number",
							"value": "={{ $('Get scheduled posts').all().filter(p => p.json.id && p.json.scheduledTime && DateTime.fromISO(p.json.scheduledTime).toMillis() >= $now.toMillis() && DateTime.fromISO(p.json.scheduledTime).toMillis() < $now.plus({ days: Number($('Settings').first().json.daysAhead) || 2 }).toMillis()).length }}"
						},
						{
							"id": "f2",
							"name": "daysAhead",
							"type": "number",
							"value": "={{ Number($('Settings').first().json.daysAhead) || 2 }}"
						},
						{
							"id": "f3",
							"name": "minimumPosts",
							"type": "number",
							"value": "={{ Number($('Settings').first().json.minimumPosts) || 1 }}"
						}
					]
				},
				"includeOtherFields": false,
				"options": {}
			},
			"executeOnce": true
		},
		{
			"id": "a7504595-1085-4eca-9b4c-ab87008efad1",
			"name": "Running low?",
			"type": "n8n-nodes-base.if",
			"typeVersion": 2.2,
			"position": [
				520,
				320
			],
			"parameters": {
				"conditions": {
					"options": {
						"caseSensitive": true,
						"leftValue": "",
						"typeValidation": "loose",
						"version": 2
					},
					"conditions": [
						{
							"id": "running-low",
							"leftValue": "={{ $json.upcoming }}",
							"rightValue": "={{ $json.minimumPosts }}",
							"operator": {
								"type": "number",
								"operation": "lt"
							}
						}
					],
					"combinator": "and"
				},
				"looseTypeValidation": true,
				"options": {}
			}
		},
		{
			"id": "a0d044bc-9743-41f2-a449-3920d10500b7",
			"name": "Write the message",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				740,
				320
			],
			"parameters": {
				"assignments": {
					"assignments": [
						{
							"id": "f1",
							"name": "message",
							"type": "string",
							"value": "={{ 'Your sprkly queue is running low. ' + ($json.upcoming === 0 ? 'No posts are' : $json.upcoming === 1 ? 'Only 1 post is' : 'Only ' + $json.upcoming + ' posts are') + ' set to go out in the next ' + $json.daysAhead + ' days.\\n\\nAdd more here: https://sprkly.app/schedule?utm_source=n8n&utm_medium=workflow&utm_campaign=queue-running-low-alert' }}"
						}
					]
				},
				"includeOtherFields": true,
				"options": {}
			}
		},
		{
			"id": "db9827a6-f50c-44cc-8717-6ba021a54a8c",
			"name": "Send to Telegram",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				960,
				320
			],
			"parameters": {
				"chatId": "={{ $('Settings').first().json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		},
		{
			"id": "0e6582ef-7fb8-458a-a1b0-1f1dc792a3e0",
			"name": "When this workflow fails",
			"type": "n8n-nodes-base.errorTrigger",
			"typeVersion": 1,
			"position": [
				-1040,
				816
			],
			"parameters": {}
		},
		{
			"id": "df30b9fa-7b31-4693-9f79-47756c5e0514",
			"name": "Write the failure alert",
			"type": "n8n-nodes-base.set",
			"typeVersion": 3.4,
			"position": [
				-820,
				816
			],
			"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": "8e796fac-dd7a-463a-b4f2-91e839e9eb66",
			"name": "Send the failure alert",
			"type": "n8n-nodes-base.telegram",
			"typeVersion": 1.2,
			"position": [
				-600,
				816
			],
			"parameters": {
				"chatId": "={{ $json.telegramChatId }}",
				"text": "={{ $json.message }}",
				"additionalFields": {
					"appendAttribution": false
				}
			}
		}
	],
	"connections": {
		"Every morning": {
			"main": [
				[
					{
						"node": "Settings",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Settings": {
			"main": [
				[
					{
						"node": "Get scheduled posts",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Get scheduled posts": {
			"main": [
				[
					{
						"node": "Count upcoming posts",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Count upcoming posts": {
			"main": [
				[
					{
						"node": "Running low?",
						"type": "main",
						"index": 0
					}
				]
			]
		},
		"Running low?": {
			"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
					}
				]
			]
		}
	},
	"pinData": {},
	"active": false,
	"settings": {
		"executionOrder": "v1"
	}
}
