{
  "name": "Gmail Job Alert \u2014 AI Summary to Telegram",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 * * * *"
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Every Hour",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "getAll",
        "returnAll": false,
        "limit": 20,
        "filters": {
          "q": "is:unread label:inbox (subject:interview OR subject:offer OR subject:application OR subject:hiring OR subject:opportunity OR subject:position OR subject:recruit OR subject:job OR subject:CTC OR subject:salary OR subject:joining OR subject:HR OR subject:shortlisted OR subject:selected OR subject:rejected) newer_than:1d"
        },
        "options": {
          "format": "full"
        }
      },
      "id": "gmail-read",
      "name": "Fetch Job Emails from Gmail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        460,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false
          },
          "conditions": [
            {
              "id": "has-emails",
              "leftValue": "={{ $json.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            }
          ]
        }
      },
      "id": "check-emails-exist",
      "name": "Any emails found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "aggregate": "aggregateAllItemData",
        "options": {}
      },
      "id": "aggregate-emails",
      "name": "Combine All Emails",
      "type": "n8n-nodes-base.aggregate",
      "typeVersion": 1,
      "position": [
        900,
        220
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $env.GEMINI_API_KEY }}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "contents",
              "value": "=[{\"parts\":[{\"text\": \"You are an assistant helping Fazal Ahmad (a B.Tech Cyber Security student and founder of Valenza automation consultancy) review his job-related emails.\\n\\nHere are his unread job-related emails from the last 24 hours:\\n\\n{{ $json.data.map((e, i) => `Email ${i+1}:\\nFrom: ${e.from}\\nSubject: ${e.subject}\\nDate: ${e.date}\\nBody: ${e.text || e.snippet}\\n`).join('\\n---\\n') }}\\n\\nGive a short WhatsApp-style summary (use emojis, keep it under 300 words):\\n1. List each important email in one line\\n2. Highlight any interviews, offers, or urgent actions needed\\n3. End with a recommendation: what should he do first?\"}]}]"
            }
          ]
        },
        "options": {}
      },
      "id": "gemini-summarize",
      "name": "Summarize with Gemini AI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1120,
        220
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $env.TELEGRAM_CHAT_ID }}",
        "text": "=\ud83d\udcec *Job Email Summary* \u2014 {{ $now.format('DD MMM, hh:mm A') }}\n\n{{ $json.candidates[0].content.parts[0].text }}\n\n_\u2014 Valenza Auto-Assistant_",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "telegram-send",
      "name": "Send to Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1340,
        220
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Fetch Job Emails from Gmail').item.json.id }}",
        "labelIds": [
          "UNREAD"
        ],
        "options": {}
      },
      "id": "mark-read",
      "name": "Mark Emails as Read",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        1560,
        220
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every Hour": {
      "main": [
        [
          {
            "node": "Fetch Job Emails from Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Job Emails from Gmail": {
      "main": [
        [
          {
            "node": "Any emails found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Any emails found?": {
      "main": [
        [
          {
            "node": "Combine All Emails",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Combine All Emails": {
      "main": [
        [
          {
            "node": "Summarize with Gemini AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize with Gemini AI": {
      "main": [
        [
          {
            "node": "Send to Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send to Telegram": {
      "main": [
        [
          {
            "node": "Mark Emails as Read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}