{
  "name": "Filter urgent Slack & Gmail alerts to Telegram using Gemini",
  "nodes": [
    {
      "parameters": {
        "content": "## \ud83d\udea8 Urgent Alert Filter\n\nNever miss critical messages again. This workflow monitors **Slack** and **Gmail** for new messages, uses **Gemini** to analyze their urgency, and forwards ONLY the urgent ones to **Telegram**.\n\n## How it works\n1. **Listen:** Monitors Slack channels and Gmail inbox for new incoming messages.\n   - *Includes a **Test Mode** to simulate urgent messages without waiting for real ones.*\n2. **Analyze:** Gemini reads the message content and assigns a priority score (High/Low).\n3. **Filter:** If the message is deemed \"Urgent\" or \"High Priority,\" it proceeds.\n4. **Notify:** Sends an instant alert to your Telegram with the summary and link.\n\n## Setup steps\n1. **Connect:** Slack, Gmail, Gemini, Telegram.\n2. **Config:** Open the **\"Configuration\"** node to set your `TELEGRAM_CHAT_ID`.\n3. **Test:** Set `TEST_MODE` to `true` to verify the alert logic instantly.",
        "height": 320,
        "width": 500,
        "color": 3
      },
      "id": "sticky-main",
      "name": "Sticky Note - Main",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -380,
        240
      ]
    },
    {
      "parameters": {
        "content": "## \u2699\ufe0f Config\nSet IDs & Mode.",
        "height": 140,
        "width": 240,
        "color": 6
      },
      "id": "sticky-config",
      "name": "Sticky Note - Config",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -380,
        600
      ]
    },
    {
      "parameters": {
        "content": "## \ud83d\udce5 Unified Inputs\nReceives messages from Slack & Gmail.",
        "height": 300,
        "width": 460,
        "color": 6
      },
      "id": "sticky-inputs",
      "name": "Sticky Note - Inputs",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        180,
        240
      ]
    },
    {
      "parameters": {
        "content": "## \ud83e\udde0 AI Priority Check\nDetermines urgency.",
        "height": 140,
        "width": 440,
        "color": 6
      },
      "id": "sticky-ai",
      "name": "Sticky Note - AI",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        680,
        240
      ]
    },
    {
      "parameters": {
        "content": "## \ud83d\ude80 Telegram Alert\nSends only urgent alerts.",
        "height": 140,
        "width": 440,
        "color": 6
      },
      "id": "sticky-alert",
      "name": "Sticky Note - Alert",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1160,
        240
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "conf_1",
              "name": "TELEGRAM_CHAT_ID",
              "value": "",
              "type": "string"
            },
            {
              "id": "conf_2",
              "name": "TEST_MODE",
              "value": "true",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "config-node",
      "name": "Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -360,
        660
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "manual-trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -600,
        660
      ]
    },
    {
      "parameters": {
        "channelId": {
          "__rl": true,
          "value": "general",
          "mode": "list",
          "cachedResultName": "general"
        },
        "resolveData": true,
        "options": {}
      },
      "id": "slack-trigger",
      "name": "Slack Trigger",
      "type": "n8n-nodes-base.slackTrigger",
      "typeVersion": 1,
      "position": [
        -600,
        460
      ],
      "credentials": {
        "slackTokenApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "readStatus": "unread"
        },
        "options": {}
      },
      "id": "gmail-trigger",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [
        -600,
        860
      ],
      "credentials": {
        "gmailOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Normalize Input Data\nconst items = $input.all();\nconst config = $('Configuration').first().json;\n\n// If Test Mode is ON (via Manual Trigger)\nif (config.TEST_MODE === 'true' && items[0].json.source === undefined) {\n  return [{ \n    json: { \n      text: \"Server Down! Database connection failed in production.\", \n      source: \"Test Mode\",\n      sender: \"System\"\n    } \n  }];\n}\n\n// Handle Slack\nif (items[0].json.text) {\n  return [{ \n    json: { \n      text: items[0].json.text, \n      source: \"Slack\",\n      sender: items[0].json.user || \"User\"\n    } \n  }];\n}\n\n// Handle Gmail\nif (items[0].json.snippet) {\n  return [{ \n    json: { \n      text: items[0].json.snippet, \n      source: \"Gmail\",\n      sender: items[0].json.from || \"Sender\"\n    } \n  }];\n}\n\nreturn [{ json: { text: \"\", source: \"Unknown\" } }];"
      },
      "id": "normalize-data",
      "name": "Normalize Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        380
      ]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "models/gemini-1.5-flash",
          "mode": "list",
          "cachedResultName": "models/gemini-1.5-flash"
        },
        "messages": {
          "values": [
            {
              "content": "=Analyze urgency for this message:\n\"{{ $json.text }}\"\n\n[Rules]\n- URGENT: Server down, Crash, Payment failed, VIP client.\n- NOT URGENT: Newsletter, Meeting update, Hello.\n\n[Output JSON ONLY]\n{ \"is_urgent\": true/false, \"reason\": \"short reason\" }"
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "typeVersion": 1,
      "position": [
        720,
        380
      ],
      "id": "gemini-analyze",
      "name": "Gemini: Analyze",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Parse Gemini Response\nconst text = $input.first().json.content.parts[0].text;\nlet data = {};\ntry {\n  const jsonMatch = text.match(/\\{[\\s\\S]*\\}/);\n  if (jsonMatch) data = JSON.parse(jsonMatch[0]);\n  else data = { is_urgent: false, reason: \"Parse Error\" };\n} catch (e) {\n  data = { is_urgent: false, reason: \"Error\" };\n}\n\nreturn { json: data };"
      },
      "id": "parse-response",
      "name": "Parse Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        920,
        380
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is_urgent",
              "leftValue": "={{ $json.is_urgent }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "filter-urgency",
      "name": "Is Urgent?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1200,
        380
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $('Configuration').first().json.TELEGRAM_CHAT_ID }}",
        "text": "=\ud83d\udea8 **Urgent Alert from {{ $('Normalize Data').first().json.source }}**\n\n**Reason:** {{ $json.reason }}\n**Message:** {{ $('Normalize Data').first().json.text }}",
        "additionalFields": {}
      },
      "id": "telegram-alert",
      "name": "Telegram Alert",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1420,
        360
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Configuration": {
      "main": [
        [
          {
            "node": "Normalize Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slack Trigger": {
      "main": [
        [
          {
            "node": "Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Data": {
      "main": [
        [
          {
            "node": "Gemini: Analyze",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini: Analyze": {
      "main": [
        [
          {
            "node": "Parse Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Response": {
      "main": [
        [
          {
            "node": "Is Urgent?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Urgent?": {
      "main": [
        [
          {
            "node": "Telegram Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}