AutomationFlowsAI & RAG › AI Gmail Spam Filter with Telegram Alerts

AI Gmail Spam Filter with Telegram Alerts

Original n8n title: Telegram (gmail)

Telegram. Uses gmail, manualTrigger, lmChatGoogleGemini, agent. Event-driven trigger; 17 nodes.

Event trigger★★★★☆ complexityAI-powered17 nodesGmailGoogle Gemini ChatAgentOutput Parser StructuredTelegram
AI & RAG Trigger: Event Nodes: 17 Complexity: ★★★★☆ AI nodes: yes Added:

This workflow follows the Agent → Gmail recipe pattern — see all workflows that pair these two integrations.

The workflow JSON

Copy or download the full n8n JSON below. Paste it into a new n8n workflow, add your credentials, activate. Full import guide →

Download .json
{
  "id": "dgBdnnnY0622JwGy",
  "tags": [],
  "nodes": [
    {
      "id": "e205a1ba-9606-457f-9a2f-d433766b3786",
      "name": "Gmail Get Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        60,
        840
      ],
      "parameters": {
        "filters": {
          "q": "=before:{{ $now.minus(14 * $('Increment Loop Var').first().json.page, 'days').format('yyyy/MM/dd') }} after: {{ $now.minus(14 * ($('Increment Loop Var').first().json.page + 1), 'days').format('yyyy/MM/dd') }} -label:n8n-skipped",
          "includeSpamTrash": false
        },
        "operation": "getAll",
        "returnAll": true
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 2.1,
      "alwaysOutputData": false
    },
    {
      "id": "380f6e02-7f74-44e4-9229-4784f2f0c66f",
      "name": "When clicking \u2018Test workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -600,
        965
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "8acc6bc1-c82e-4c1c-a3b0-dd6cb75cf8c3",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        308,
        1060
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-1.5-flash"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "f067e64d-bf43-4961-82ff-dcc8ce398375",
      "name": "GmailDeleteEmail",
      "type": "n8n-nodes-base.gmail",
      "onError": "continueErrorOutput",
      "position": [
        876,
        640
      ],
      "parameters": {
        "messageId": "={{ $json.output.emailId }}",
        "operation": "delete"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": false,
      "typeVersion": 2.1,
      "alwaysOutputData": false
    },
    {
      "id": "5840c9c6-7afc-4ca1-a8d2-5d0b86339a58",
      "name": "AI Check Email",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "onError": "continueErrorOutput",
      "position": [
        280,
        840
      ],
      "parameters": {
        "text": "=Classify the email with decimal values (0 to 1) for isUnwantedConfidence, isMarketingConfidence, and isSpamConfidence, where 0 means clearly wanted (e.g., billing, invoices, orders, job applications, security) and 1 means clearly unwanted (e.g., promotions, setup reminders, irrelevant alerts); treat system-generated alerts or device activity (like sound played, device found, location pinged) as unwanted unless they are security-related; use 0.5 as the baseline for deletion and provide a concise briefReason explaining the classification.\n\n{{ JSON.stringify($json) }}",
        "options": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "retryOnFail": true,
      "typeVersion": 1.8,
      "alwaysOutputData": true
    },
    {
      "id": "e10f96ce-0a92-4817-b340-0d284039a212",
      "name": "Unwanted Email Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        428,
        1060
      ],
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n  \"type\": \"object\",\n  \"required\": [\n    \"emailId\",\n    \"isUnwantedConfidence\",\n    \"isMarketingConfidence\",\n    \"isSpamConfidence\",\n    \"briefReason\",\n    \"emailFrom\"\n  ],\n  \"properties\": {\n    \"emailId\": {\n      \"type\": \"string\",\n      \"description\": \"id from the email itself\"\n    },\n    \"isUnwantedConfidence\": {\n      \"type\": \"number\",\n      \"minimum\": 0,\n      \"maximum\": 1,\n      \"description\": \"confidence that the email is unwanted\"\n    },\n    \"isMarketingConfidence\": {\n      \"type\": \"number\",\n      \"minimum\": 0,\n      \"maximum\": 1,\n      \"description\": \"confidence that the email is marketing\"\n    },\n    \"isSpamConfidence\": {\n      \"type\": \"number\",\n      \"minimum\": 0,\n      \"maximum\": 1,\n      \"description\": \"confidence that the email is spam\"\n    },\n    \"briefReason\": {\n      \"type\": \"string\",\n      \"description\": \"a short reason why, more context for the reader\"\n    },\n    \"emailFrom\": {\n      \"type\": \"string\",\n      \"description\": \"the email address of the sender\"\n    }\n  }\n}\n"
      },
      "typeVersion": 1.2
    },
    {
      "id": "796a0d3e-a1e9-4b2c-8f9c-32466c046d22",
      "name": "If Unwanted Marketing or Spam",
      "type": "n8n-nodes-base.if",
      "position": [
        656,
        740
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "c2b58601-60ff-45b4-a8a3-0d8543844a2d",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $json.output.isUnwantedConfidence }}",
              "rightValue": 0.5
            },
            {
              "id": "ec441e67-046a-4c9c-bce7-85d984b86442",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $json.output.isMarketingConfidence }}",
              "rightValue": 0.5
            },
            {
              "id": "80f9ced7-15b0-4dee-97a4-4b3f9ae9c81f",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $json.output.isSpamConfidence }}",
              "rightValue": 0.5
            }
          ]
        }
      },
      "typeVersion": 2.2,
      "alwaysOutputData": false
    },
    {
      "id": "44a68cfc-b1c7-49c1-b268-42810fcd3eeb",
      "name": "Telegram Sent Email Deleted Notification",
      "type": "n8n-nodes-base.telegram",
      "onError": "continueErrorOutput",
      "position": [
        1096,
        740
      ],
      "parameters": {
        "text": "=Email Deleted | {{ $('If Unwanted Marketing or Spam').item.json.output.emailFrom }} | {{ $('If Unwanted Marketing or Spam').item.json.output.briefReason }}",
        "chatId": "273696245",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2,
      "alwaysOutputData": false
    },
    {
      "id": "01dee5a4-2ba7-4606-98d8-88914621be48",
      "name": "Telegram Sent Email Not Deleted Notification",
      "type": "n8n-nodes-base.telegram",
      "onError": "continueErrorOutput",
      "position": [
        1096,
        940
      ],
      "parameters": {
        "text": "=Skipping Email | {{ $('If Unwanted Marketing or Spam').item.json.output.emailFrom }} | {{ $('If Unwanted Marketing or Spam').item.json.output.briefReason }}",
        "chatId": "273696245",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2,
      "alwaysOutputData": false
    },
    {
      "id": "03292140-5f20-42e8-8d2f-e07a0621844c",
      "name": "Telegram Sent AI Error Notification",
      "type": "n8n-nodes-base.telegram",
      "onError": "continueErrorOutput",
      "position": [
        1096,
        1140
      ],
      "parameters": {
        "text": "=AI Error | Can't Check Email | Error: {{ JSON.stringify($json) }}",
        "chatId": "273696245",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2,
      "alwaysOutputData": false
    },
    {
      "id": "cec5d43c-0e06-45a2-b7bc-f943ef205496",
      "name": "Telegram Sent Delete Email Failed Notification",
      "type": "n8n-nodes-base.telegram",
      "onError": "continueErrorOutput",
      "position": [
        1096,
        540
      ],
      "parameters": {
        "text": "=Can't Delete Email",
        "chatId": "273696245",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2,
      "alwaysOutputData": false
    },
    {
      "id": "bad2dbcc-be22-4470-9b69-06c37929fe65",
      "name": "Success",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1316,
        840
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "1c38ad6f-ef9c-4e34-8b5e-a6d94ddbd221",
      "name": "Increment Loop Var",
      "type": "n8n-nodes-base.set",
      "position": [
        -160,
        840
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "596ff68a-1df1-4148-8899-fdfa36238023",
              "name": "page",
              "type": "number",
              "value": "={{ ($('Forward Prev Page Num').isExecuted) ? $('Forward Prev Page Num').first().json.prevPage + 1 : $('Initialize Loop Vars').first().json.page }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "66afc6b4-f36f-4564-bb77-b8da9e35331a",
      "name": "Forward Prev Page Num",
      "type": "n8n-nodes-base.set",
      "position": [
        1756,
        1065
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "596ff68a-1df1-4148-8899-fdfa36238023",
              "name": "prevPage",
              "type": "number",
              "value": "={{ $('Increment Loop Var').first().json.page }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "6ab44236-17fa-4790-82b5-a0db8f051d19",
      "name": "Initialize Loop Vars",
      "type": "n8n-nodes-base.set",
      "position": [
        -380,
        965
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "5e1583e5-597d-40e9-b656-5f3259b4fe25",
              "name": "page",
              "type": "number",
              "value": 0
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "04432130-1a36-4776-9c87-07e935990310",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1536,
        840
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "73fae867-dc79-43e0-80c8-8f60c7e5a8f4",
      "name": "Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        876,
        940
      ],
      "parameters": {
        "labelIds": [
          "Label_1321570453811516949"
        ],
        "messageId": "={{ $json.output.emailId }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    }
  ],
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "executionOrder": "v1"
  },
  "versionId": "9c3ca437-2184-4475-99d6-46a4fcda40a3",
  "connections": {
    "Gmail": {
      "main": [
        [
          {
            "node": "Telegram Sent Email Not Deleted Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Success": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Forward Prev Page Num",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Check Email": {
      "main": [
        [
          {
            "node": "If Unwanted Marketing or Spam",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Telegram Sent AI Error Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Get Email": {
      "main": [
        [
          {
            "node": "AI Check Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GmailDeleteEmail": {
      "main": [
        [
          {
            "node": "Telegram Sent Email Deleted Notification",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Telegram Sent Delete Email Failed Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Increment Loop Var": {
      "main": [
        [
          {
            "node": "Gmail Get Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Initialize Loop Vars": {
      "main": [
        [
          {
            "node": "Increment Loop Var",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Forward Prev Page Num": {
      "main": [
        [
          {
            "node": "Initialize Loop Vars",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Check Email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Unwanted Email Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Check Email",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "If Unwanted Marketing or Spam": {
      "main": [
        [
          {
            "node": "GmailDeleteEmail",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Test workflow\u2019": {
      "main": [
        [
          {
            "node": "Initialize Loop Vars",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Sent AI Error Notification": {
      "main": [
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Sent Email Deleted Notification": {
      "main": [
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Sent Email Not Deleted Notification": {
      "main": [
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Sent Delete Email Failed Notification": {
      "main": [
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Credentials you'll need

Each integration node will prompt for credentials when you import. We strip credential IDs before publishing — you'll add your own.

Pro

For the full experience including quality scoring and batch install features for each workflow upgrade to Pro

How this works

This workflow empowers you to maintain a clutter-free inbox by automatically detecting and removing unwanted marketing or spam emails from Gmail, while notifying you via Telegram for transparency. It's ideal for busy professionals or anyone overwhelmed by promotional clutter who values quick, hands-off email management without constant manual checks. The key step involves an AI agent powered by Google Gemini analysing email content to classify it as unwanted, triggering deletion and a Telegram alert if confirmed.

Use this workflow when you receive frequent marketing emails that distract from important messages, especially in high-volume inboxes. Avoid it for emails requiring nuanced personal judgement, like potential client leads disguised as promotions, or if you prefer keeping all correspondence for records. Common variations include customising the AI prompts for stricter spam detection or integrating additional filters for specific senders.

About this workflow

Telegram. Uses gmail, manualTrigger, lmChatGoogleGemini, agent. Event-driven trigger; 17 nodes.

Source: https://github.com/Zie619/n8n-workflows — original creator credit. Request a take-down →

More AI & RAG workflows → · Browse all categories →

Related workflows

Workflows that share integrations, category, or trigger type with this one. All free to copy and import.

AI & RAG

22-Automate_Multi_Platform_Social_Media_Content_Creation. Uses outputParserStructured, lmChatGoogleGemini, lmChatOpenAi, httpRequest. Event-driven trigger; 57 nodes.

Output Parser Structured, Google Gemini Chat, OpenAI Chat +11
AI & RAG

This workflow automatically transforms your messy inbox into a neatly organized space while ensuring you never miss a critical message. It connects to your Gmail account and triggers for every new ema

Gmail Trigger, Output Parser Structured, Gmail Tool +8
AI & RAG

This automated workflow intelligently qualifies interior design leads, generates personalized client emails, and manages follow-up through a human-approval process. Built with n8n, Claude AI, Telegram

Form Trigger, Google Gemini Chat, Output Parser Structured +5
AI & RAG

This n8n template automatically monitors a Zoho Mail inbox, analyzes every incoming email with Google Gemini AI, then forwards it — including all attachments — to a designated recipient via Gmail. The

@Zohomail/N8N Nodes Zohomail, Agent, Output Parser Structured +4
AI & RAG

The n8n Telegram Gmail Assistant is an intelligent workflow that lets you search and retrieve specific Gmail emails simply by messaging a Telegram bot. Powered by advanced language models, it turns pl

Chain Llm, Google Gemini Chat, Agent +4