{
  "id": "Pnz5vPhMCcnfaaHU",
  "name": "[STAGE] n8n Server Restart - Ping team channel",
  "tags": [],
  "nodes": [
    {
      "id": "11abd6b1-aa7e-46f7-bbee-a0176dad4de7",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        16,
        -144
      ],
      "parameters": {
        "width": 480,
        "height": 816,
        "content": "## [STAGE] n8n Server Restart - Ping discord channel log-n8n\n\n### How it works\n\nThis workflow runs from an n8n trigger, initializes lookup parameters, and retrieves recent n8n executions after a server restart or instance event. It filters, summarizes, and sorts execution information, merges the processed results with the original execution data, and formats a notification message. The final message is sent to Discord, Slack, and Telegram channels for operational visibility.\n\n### Setup steps\n\n- Configure the n8n credentials used by the \u201cGet many executions\u201d node so it can read execution history.\n- Review the \u201cEdit Fields\u201d node and set values such as minutes_to_fetch_execution, get_max_execution, and time_instance_start for the desired monitoring window.\n- Connect and authorize the Discord, Slack, and Telegram credentials used by the notification nodes.\n- Set the target Discord channel, Slack channel, and Telegram chat/message settings in their respective nodes.\n\n### Customization\n\nAdjust the filter, summarize, and sort nodes to change which executions are reported and how they are ordered. Update the JavaScript code node to customize the notification text, formatting, or per-channel payloads."
      },
      "typeVersion": 1
    },
    {
      "id": "59e3ba91-3565-45ba-acbd-d3f7f68c1d57",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        576,
        80
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 336,
        "content": "## Trigger and parameters\n\nStarts the workflow from the n8n trigger and initializes the key runtime fields such as the execution lookup window, maximum executions to retrieve, and instance start time."
      },
      "typeVersion": 1
    },
    {
      "id": "2b0ebaeb-b1c2-41da-b454-909a0ac1a671",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1008,
        32
      ],
      "parameters": {
        "color": 7,
        "height": 384,
        "content": "## Fetch recent executions\n\nRetrieves recent n8n executions using the configured limits and time window, then feeds both the analysis branch and the later merge step."
      },
      "typeVersion": 1
    },
    {
      "id": "d61bf16e-681e-4d36-9427-136bc35eb697",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1248,
        32
      ],
      "parameters": {
        "color": 7,
        "width": 640,
        "height": 320,
        "content": "## Filter and summarize\n\nProcesses the fetched execution list by filtering relevant items, summarizing the results, and sorting them into the desired order for reporting."
      },
      "typeVersion": 1
    },
    {
      "id": "8ed259ea-cf77-4af0-b20f-427df5870c08",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1920,
        80
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 336,
        "content": "## Merge and format message\n\nCombines the selected execution data with the processed summary branch, then runs JavaScript to prepare the notification payloads for downstream channels."
      },
      "typeVersion": 1
    },
    {
      "id": "7248a9ec-15e9-4d4c-bf30-30d77dfb89b6",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2352,
        -208
      ],
      "parameters": {
        "color": 7,
        "height": 816,
        "content": "## Send channel alerts\n\nSends the generated restart or execution summary notification to the configured Slack, Discord, and Telegram destinations."
      },
      "typeVersion": 1
    },
    {
      "id": "8086645f-6844-45c9-b175-a8f2945da9c4",
      "name": "When Server Restarted",
      "type": "n8n-nodes-base.n8nTrigger",
      "position": [
        624,
        256
      ],
      "parameters": {
        "events": [
          "init"
        ]
      },
      "typeVersion": 1
    },
    {
      "id": "9ac58e28-e240-4616-b78b-9087be5209ac",
      "name": "Ping Discord Channel",
      "type": "n8n-nodes-base.discord",
      "disabled": true,
      "position": [
        2416,
        256
      ],
      "parameters": {
        "content": "={{ $json.discord_message }}",
        "options": {},
        "authentication": "webhook"
      },
      "credentials": {
        "discordWebhookApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 2,
      "waitBetweenTries": 2000
    },
    {
      "id": "3dd43520-59e8-4241-b416-d78ba823ea72",
      "name": "Fetch Execution Logs",
      "type": "n8n-nodes-base.n8n",
      "position": [
        1072,
        256
      ],
      "parameters": {
        "limit": "={{ $json.get_max_execution }}",
        "filters": {},
        "options": {},
        "resource": "execution",
        "requestOptions": {}
      },
      "credentials": {
        "n8nApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "13ba3dfe-6e25-42ed-908a-79ade2e4dab1",
      "name": "Set Execution Parameters",
      "type": "n8n-nodes-base.set",
      "position": [
        848,
        256
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "28ae002e-d81c-47b9-9a34-7cdf4e3c1d3b",
              "name": "minutes_to_fetch_execution",
              "type": "number",
              "value": 20
            },
            {
              "id": "ce4a2a5d-28b6-4569-a933-b2fb3d876a65",
              "name": "get_max_execution",
              "type": "number",
              "value": 200
            },
            {
              "id": "6af37469-eb9b-4293-b928-e5b1f3d5af46",
              "name": "time_instance_start",
              "type": "string",
              "value": "={{ $json.timestamp }}"
            },
            {
              "id": "be363bfd-a792-45d4-83de-7b40ede809c3",
              "name": "discord_mentions",
              "type": "array",
              "value": "[]"
            },
            {
              "id": "b6ab33e5-c6d2-4317-bf1f-27a53c7a229a",
              "name": "slack_mentions",
              "type": "array",
              "value": "[]"
            },
            {
              "id": "ba919a79-76f5-4a67-9ad4-b314b917c59c",
              "name": "telegram_mentions",
              "type": "array",
              "value": "[]"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "5e2af791-1640-4bcd-8402-97b5fc8f8b43",
      "name": "Filter Recent Executions",
      "type": "n8n-nodes-base.filter",
      "position": [
        1296,
        192
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "24678c75-22dc-4209-a20c-757f35ae3385",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ $json.finished }}",
              "rightValue": false
            },
            {
              "id": "621b743e-45e4-496a-8563-174f0f26db02",
              "operator": {
                "type": "dateTime",
                "operation": "afterOrEquals"
              },
              "leftValue": "={{ $json.startedAt }}",
              "rightValue": "={{ $('Set Execution Parameters').first().json.time_instance_start.toDateTime().minus($('Set Execution Parameters').first().json.minutes_to_fetch_execution, 'minutes') }}"
            }
          ]
        }
      },
      "typeVersion": 2.3,
      "alwaysOutputData": true
    },
    {
      "id": "930800d2-dc15-47d4-97bc-eca1b857ccdb",
      "name": "Summarize Execution Data",
      "type": "n8n-nodes-base.summarize",
      "position": [
        1520,
        192
      ],
      "parameters": {
        "options": {},
        "fieldsToSplitBy": "workflowId",
        "fieldsToSummarize": {
          "values": [
            {
              "field": "id"
            }
          ]
        }
      },
      "typeVersion": 1.1,
      "alwaysOutputData": true
    },
    {
      "id": "177ab97b-22ea-4b7c-849c-c857146a6be0",
      "name": "Sort Execution Summary",
      "type": "n8n-nodes-base.sort",
      "position": [
        1744,
        192
      ],
      "parameters": {
        "options": {},
        "sortFieldsUi": {
          "sortField": [
            {
              "order": "descending",
              "fieldName": "count_id"
            }
          ]
        }
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "1bb0a9bb-cb01-4a75-b70f-0ba9f79650c5",
      "name": "Merge Execution Data",
      "type": "n8n-nodes-base.merge",
      "position": [
        1968,
        256
      ],
      "parameters": {
        "mode": "chooseBranch",
        "useDataOfInput": 2
      },
      "typeVersion": 3.2
    },
    {
      "id": "910a45d4-8195-4b61-8f15-e0c11f8c3d69",
      "name": "Process Data in JavaScript",
      "type": "n8n-nodes-base.code",
      "position": [
        2192,
        256
      ],
      "parameters": {
        "jsCode": "// ==================================================================\n// 1. GLOBAL CONFIGURATION (Edit these values easily)\n// ==================================================================\nconst CONFIG = {\n  fallbackBaseUrl: 'https://your-n8n-domain.com',\n  fallbackMinutes: 3600,\n};\n\n// ==================================================================\n// 2. SAFE DATA EXTRACTION & FALLBACKS\n// ==================================================================\n// Safely extract Base URL\nlet baseUrl = CONFIG.fallbackBaseUrl;\nif ($execution?.resumeUrl) {\n  const match = $execution.resumeUrl.match(/^https?:\\/\\/[^/]+/);\n  if (match) baseUrl = match[0];\n}\n\n// Safely fetch variables (Using '?.' prevents crashes if the node is empty)\nconst editFieldsNode = $('Set Execution Parameters').first()?.json || {};\nconst getExecutionsNode = $('Fetch Execution Logs').first()?.json || {};\n\nconst timeInstanceStart = editFieldsNode.time_instance_start || new Date().toISOString();\nconst startedAt = getExecutionsNode.startedAt || new Date().toISOString();\nconst fetchMinutes = editFieldsNode.minutes_to_fetch_execution || CONFIG.fallbackMinutes;\nconst {discord_mentions: discordMentions, slack_mentions: slackMentions, telegram_mentions: telegramMentions} = editFieldsNode\n\n// ==================================================================\n// 3. TIME MATH & FORMATTING\n// ==================================================================\nconst diff = DateTime.fromISO(timeInstanceStart).diff(\n  DateTime.fromISO(startedAt), \n  ['hours', 'minutes', 'seconds']\n);\n\n// Format gap and strip minus signs\nconst gapTimeStr = `${diff.hours}h ${diff.minutes}m ${Math.round(diff.seconds)}s`.replace(/-/g, '');\nconst formattedRestartTime = DateTime.fromISO(timeInstanceStart).toFormat('yyyy-MM-dd HH:mm:ss');\n\n// Parse Mentions into formatted strings\nconst discordTags = discordMentions.map(id => `<@${id}>`).join(' ');\nconst slackTags = slackMentions.map(id => `<@${id}>`).join(' ');\nconst telegramTags = telegramMentions.join(' ');\n// ==================================================================\n// 4. BUILD THE LISTS (Discord vs Slack)\n// ==================================================================\nconst sortedItems = $('Sort Execution Summary').all().filter(i => !!i?.json?.workflowId);\n\nlet discordList = '';\nlet slackList = '';\nlet telegramList = '';\n\nif (sortedItems.length > 0) {\n  let dLines = [];\n  let sLines = [];\n  let tLines = []; \n\n  sortedItems.forEach(item => {\n    const workflowId = item.json.workflowId;\n    const count = item.json.count_id;\n    const workflowLink = `${baseUrl}/workflow/${workflowId}/executions`;\n\n    dLines.push(`- [${workflowId}](${workflowLink}): ${count} times`);\n    sLines.push(`- <${workflowLink}|${workflowId}>: ${count} times`);\n    tLines.push(`- [${workflowId}](${workflowLink}): ${count} times`);\n  });\n\n  discordList = dLines.join('\\n').trim();\n  slackList = sLines.join('\\n').trim();\n  telegramList = tLines.join('\\n').trim();\n\n} else {\n  discordList = slackList = telegramList = '- \ud83d\udfe2 All good no failed workflow recently!';\n}\n\n// ==================================================================\n// 5. COMPOSE FINAL MESSAGES\n// ==================================================================\nconst discordMessage = `\u26a0\ufe0f **n8n Server Restarted At:** ${formattedRestartTime} ${discordTags}\n\n**Downtime / Gap:** ${gapTimeStr}\n\n**Workflow Run Failed (Last ${fetchMinutes} Minutes):**\n${discordList}`;\n\nconst slackMessage = `\u26a0\ufe0f *n8n Server Restarted At:* ${formattedRestartTime} ${slackTags}\n\n*Downtime / Gap:* ${gapTimeStr}\n\n*Workflow Run Failed (Last ${fetchMinutes} Minutes):*\n${slackList}`;\n\n// ... final message construction\nconst telegramMessage = `\u26a0\ufe0f *n8n Server Restarted At:* ${formattedRestartTime} ${telegramTags}\n\n*Downtime / Gap:* ${gapTimeStr}\n\n*Workflow Run Failed (Last ${fetchMinutes} Minutes):*\n${telegramList}`;\n\n// ==================================================================\n// FINAL OUTPUT\n// ==================================================================\nreturn {\n  json: {\n    discord_message: discordMessage,\n    slack_message: slackMessage,\n    telegram_message: telegramMessage\n  }\n};"
      },
      "typeVersion": 2
    },
    {
      "id": "f1d1e127-fd19-49ad-b362-3e36d76b268b",
      "name": "Send Telegram Message",
      "type": "n8n-nodes-base.telegram",
      "disabled": true,
      "position": [
        2416,
        448
      ],
      "parameters": {
        "text": "={{ $json.telegram_message }}",
        "chatId": "123456789",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "5ca01218-c251-497c-baa2-4e092f9ffbc2",
      "name": "Post to Slack Channel",
      "type": "n8n-nodes-base.slack",
      "position": [
        2416,
        64
      ],
      "parameters": {
        "text": "={{ $json.slack_message }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "otherOptions": {}
      },
      "typeVersion": 2.5
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "f0c0e6c7-c4ec-4e3d-82c6-fbafdc2ff3cf",
  "nodeGroups": [],
  "connections": {
    "Fetch Execution Logs": {
      "main": [
        [
          {
            "node": "Merge Execution Data",
            "type": "main",
            "index": 1
          },
          {
            "node": "Filter Recent Executions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Execution Data": {
      "main": [
        [
          {
            "node": "Process Data in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Server Restarted": {
      "main": [
        [
          {
            "node": "Set Execution Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sort Execution Summary": {
      "main": [
        [
          {
            "node": "Merge Execution Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Recent Executions": {
      "main": [
        [
          {
            "node": "Summarize Execution Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Execution Parameters": {
      "main": [
        [
          {
            "node": "Fetch Execution Logs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize Execution Data": {
      "main": [
        [
          {
            "node": "Sort Execution Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Data in JavaScript": {
      "main": [
        [
          {
            "node": "Ping Discord Channel",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send Telegram Message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Post to Slack Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}