{
  "id": "ONWuEfvNCDiytl6Q",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "\ud83d\udca5 AI Email Assistant: Get Actionable Gmail Alerts on Telegram",
  "tags": [],
  "nodes": [
    {
      "id": "fc137a8e-978b-4451-8343-6f70023cd964",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -336,
        -592
      ],
      "parameters": {
        "width": 368,
        "height": 752,
        "content": "# \ud83d\udcec AI Email Assistant: Get Actionable Gmail Alerts on Telegram\n\n**Flux:** Schedule (3x/jour) \u2192 Gmail \u2192 AI Analysis \u2192 Telegram\n\n### Who is this for?\nThis template is designed for **busy professionals, founders, freelancers, and support teams** who want to stay on top of important emails without constantly checking Gmail. It\u2019s ideal for anyone who needs **instant, actionable notifications** directly on Telegram.\n\n### What problem is this workflow solving? / Use case\nImportant emails often get buried under newsletters, promotions, and low-priority messages. Constantly refreshing Gmail wastes time and breaks focus.  \n\nThis workflow acts as an **AI-powered email filter and alert system**. It monitors your inbox, analyzes incoming messages, and sends only **relevant, actionable summaries** to Telegram \u2014 so you can react quickly without inbox overload.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "f32e6773-a8fe-49a3-9ef4-553e47e6f2ab",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        64,
        -592
      ],
      "parameters": {
        "color": 7,
        "width": 448,
        "height": 752,
        "content": "### What this workflow does\n- Monitors your **Gmail inbox** for new emails.\n- Uses **AI (OpenAI)** to analyze and classify each email.\n- Extracts key information such as:\n  - Urgency level  \n  - Required action  \n  - Summary of the message  \n- Sends a structured, concise alert to **Telegram**.\n- Optionally highlights emails that require:\n  - Immediate reply  \n  - Payment confirmation  \n  - Meeting scheduling  \n  - Client follow-up  \n\nThe result: you receive **smart, decision-ready notifications** instead of raw email forwards.\n\n### Setup\n1. **Import the workflow** into your n8n instance.  \n2. **Connect Gmail credentials**\n   - Add your Google account in n8n.\n   - Grant permission to read your inbox.\n   - Select the mailbox/folder you want to monitor.\n3. **Set up OpenAI credentials**\n   - Add your OpenAI API key in n8n.\n   - Ensure billing is active.\n4. **Connect Telegram**\n   - Create a Telegram bot via @BotFather.\n   - Copy the bot token into n8n.\n   - Add your Chat ID (or group ID) where alerts should be sent."
      },
      "typeVersion": 1
    },
    {
      "id": "6dc196c1-3d5c-4299-ad84-4fdf41450545",
      "name": "Schedule 3x Daily Check",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -336,
        368
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8
            },
            {
              "triggerAtHour": 13
            },
            {
              "triggerAtHour": 18
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "444dbdf9-049a-4c4f-8922-c7cf4bb04da5",
      "name": "Gmail \u2013 Fetch Unread Emails",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -80,
        368
      ],
      "parameters": {
        "limit": 30,
        "filters": {
          "labelIds": "={{ [\"INBOX\"] }}",
          "readStatus": "unread",
          "receivedAfter": "={{ new Date(Date.now() - 8*60*60*1000).toISOString() }}"
        },
        "operation": "getAll"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "3167cfec-7bb3-42d8-999a-ae26bf4c8bd8",
      "name": "Unread Emails Found?",
      "type": "n8n-nodes-base.if",
      "position": [
        144,
        368
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "has-messages",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ .all().length }}",
              "rightValue": 0
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "1d2d4fb0-a912-477f-bcd0-db71ea2a279c",
      "name": "Prepare Email Data",
      "type": "n8n-nodes-base.code",
      "position": [
        352,
        272
      ],
      "parameters": {
        "jsCode": "// Pr\u00e9parer les emails pour l'analyse AI\nconst emails = .all().map(item => {\n  const d = item.json;\n  return {\n    id: d.id,\n    from: d.from?.text || d.from || 'Unknown',\n    subject: d.subject || '(sans objet)',\n    date: d.date || d.internalDate || '',\n    snippet: (d.snippet || d.textPlain || d.text || '').substring(0, 500),\n    labelIds: d.labelIds || []\n  };\n});\n\nreturn [{ json: { emails: emails, emailList: JSON.stringify(emails, null, 2) } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "a79db98d-3176-406b-acb5-84d629962585",
      "name": "AI \u2013 Analyze & Classify Emails",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        544,
        272
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {
          "temperature": 0.3
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "=You are an email assistant. You are given a list of emails. For each email, you must:\n\nDetermine whether it requires a personal reply (exclude newsletters, automated notifications, spam, marketing emails, and social network notifications).\n\nIf it requires a reply, generate a short summary (maximum 2\u20133 lines).\n\nRespond ONLY with valid JSON.\nDo NOT use markdown or any additional text.\n\n{\n  \"emails\": [\n    {\n      \"from\": \"...\",\n      \"subject\": \"...\",\n      \"needs_reply\": true,\n      \"summary\": \"...\",\n      \"priority\": \"high/medium/low\"\n    }\n  ]\n}"
            },
            {
              "content": "=Here are the emails to analyze:\n\n{{ .emailList }}"
            }
          ]
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "50ede624-8c42-4d7d-a371-13a0deec38d0",
      "name": "Format Telegram Alert",
      "type": "n8n-nodes-base.code",
      "position": [
        832,
        272
      ],
      "parameters": {
        "jsCode": "// Parser la r\u00e9ponse AI et filtrer les emails n\u00e9cessitant une r\u00e9ponse\nlet content = .first().json.message?.content || .first().json.choices?.[0]?.message?.content || .first().json.text || JSON.stringify(.first().json);\n\n// Nettoyer le contenu (enlever markdown si pr\u00e9sent)\ncontent = content.replace(/\\n?/g, '').trim();\n\nlet parsed;\ntry {\n  parsed = JSON.parse(content);\n} catch(e) {\n  // Fallback: essayer d'extraire le JSON\n  const match = content.match(/\\{[\\s\\S]*\\}/);\n  parsed = match ? JSON.parse(match[0]) : { emails: [] };\n}\n\nconst needsReply = (parsed.emails || []).filter(e => e.needs_reply);\n\nif (needsReply.length === 0) {\n  return [{ json: { hasEmails: false, message: 'Aucun email n\u00e9cessitant une r\u00e9ponse.' } }];\n}\n\n// Construire le message Telegram\nconst priorityEmoji = { haute: '\ud83d\udd34', moyenne: '\ud83d\udfe1', basse: '\ud83d\udfe2' };\nlet msg = '\ud83d\udcec **Emails n\u00e9cessitant ta r\u00e9ponse:**\\n\\n';\n\nneedsReply.forEach((e, i) => {\n  const emoji = priorityEmoji[e.priority] || '\u26aa';\n  msg += \\;\n  msg += \\;\n  msg += \\;\n});\n\nmsg += \\;\n\nreturn [{ json: { hasEmails: true, telegramMessage: msg, count: needsReply.length } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "fca71dee-65af-477d-9b2d-5595fa9f1471",
      "name": "Action Required?",
      "type": "n8n-nodes-base.if",
      "position": [
        1040,
        272
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "has-actionable",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              },
              "leftValue": "={{ .hasEmails }}",
              "rightValue": true
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "bc372741-e965-4c14-831e-ca5b6b0e8c79",
      "name": "Telegram \u2013 Send Smart Summary",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1312,
        176
      ],
      "parameters": {
        "text": "={{ .telegramMessage }}",
        "chatId": "YOUR CHAT ID",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "13809da0-d1c1-4432-ab10-d109f29e30dc",
  "connections": {
    "Action Required?": {
      "main": [
        [
          {
            "node": "Telegram \u2013 Send Smart Summary",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Prepare Email Data": {
      "main": [
        [
          {
            "node": "AI \u2013 Analyze & Classify Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Unread Emails Found?": {
      "main": [
        [
          {
            "node": "Prepare Email Data",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Format Telegram Alert": {
      "main": [
        [
          {
            "node": "Action Required?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule 3x Daily Check": {
      "main": [
        [
          {
            "node": "Gmail \u2013 Fetch Unread Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail \u2013 Fetch Unread Emails": {
      "main": [
        [
          {
            "node": "Unread Emails Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI \u2013 Analyze & Classify Emails": {
      "main": [
        [
          {
            "node": "Format Telegram Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}