{
  "id": "gXutAybKcZqblZjF",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI Email Reply Assistant with Telegram Approval",
  "tags": [],
  "nodes": [
    {
      "id": "a29a7c9c-cab6-479a-89f3-71c0abf02727",
      "name": "Structured Output Parser1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        680,
        1140
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"email\": \"email address\",\n  \"subject\": \"subject text\",\n  \"body\": \" body text .\"\n}\n"
      },
      "typeVersion": 1.2
    },
    {
      "id": "954febca-8faf-4899-838f-854a5c344b97",
      "name": "Send a message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1340,
        820
      ],
      "parameters": {
        "sendTo": "={{ $('AI: Generate Email Reply').item.json.output.email }}",
        "message": "={{ $('AI: Generate Email Reply').item.json.output.body }}",
        "options": {},
        "subject": "={{ $('AI: Generate Email Reply').item.json.output.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "feff315f-c008-4b67-bd89-0d88c79005ac",
      "name": "Trigger: New Email",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -1000,
        960
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "b69c3e8d-cd36-4c72-8a8d-006dddd0280c",
      "name": "Check: Is Email in Inbox?",
      "type": "n8n-nodes-base.if",
      "position": [
        -780,
        960
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "3f7094d8-2756-493d-8721-be7d4c83297b",
              "operator": {
                "type": "array",
                "operation": "contains",
                "rightType": "any"
              },
              "leftValue": "={{ $json.labelIds }}",
              "rightValue": "INBOX"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "3cc06ffd-8a5d-4daf-a4c1-55953fba0cc8",
      "name": "AI: Should We Reply?",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -320,
        940
      ],
      "parameters": {
        "text": "=todays date:- {{ $now }}\nmail date:- {{ $json.headers.date }}\nfrom :- {{ $json.headers.from }}\nsubject :- {{ $json.headers.subject.toJsonString() }}\n\ncontent :- {{ $json.text }}",
        "options": {
          "systemMessage": "You are a smart AI email assistant. Your job is to analyze each email and decide if it needs a response from the user. Most emails are promotions, newsletters, or notifications that do not need replies \u2014 skip those.\n\nOnly reply \"yes\" if:\n\nThe email asks a question\n\nThe email requests information or action\n\nThe email seems urgent or time-sensitive\n\nThe email involves work, deadlines, appointments, personal matters, or legal/financial implications\n\nOtherwise, reply \"no\".\n\nRespond in strict JSON format like this:\n\njson\nCopy\nEdit\n{\n  \"response\": \"yes\"\n}\nor\n\njson\nCopy\nEdit\n{\n  \"response\": \"no\"\n}\nDo not explain. No comments. Only JSON."
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2
    },
    {
      "id": "47ddc596-591f-470c-bafd-6a5ee7881935",
      "name": "Parse: Reply Decision JSON",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        -120,
        1160
      ],
      "parameters": {
        "jsonSchemaExample": "{\n\t\"response\": \"no\"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "02b4791e-6301-46b8-ad85-4c627651e08a",
      "name": " Check: Response Required?",
      "type": "n8n-nodes-base.if",
      "position": [
        100,
        940
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "8aa9d41a-a218-456c-8b46-70b2e4a1af03",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.output.response }}",
              "rightValue": "yes"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "a5efce44-3718-4ad4-ba19-b558f029228b",
      "name": " Exit: No Reply Needed",
      "type": "n8n-nodes-base.noOp",
      "position": [
        280,
        1200
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "10b4cdbf-b2e0-4a75-baf2-d81184db8967",
      "name": "No Operation, do nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -600,
        1180
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "f44760d0-60bf-4f04-9b7c-b1d4159e76ba",
      "name": "AI: Generate Email Reply",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        500,
        920
      ],
      "parameters": {
        "text": "=todays date:- {{ $now }}\nmail date:- {{ $('Trigger: New Email').item.json.headers.date }}\nfrom :- {{ $('Trigger: New Email').item.json.headers.from }}\nsubject :- {{ $('Trigger: New Email').item.json.headers.subject }}\n\ncontent :- {{ $('Trigger: New Email').item.json.text }}",
        "options": {
          "systemMessage": "=\ud83d\udce7 System Prompt: Email Reply Generator\nYou are an AI email assistant. Based on the following details, write a professional and relevant reply email on behalf of the user.\n\nToday's Date:\nOriginal Email Date:\nFrom: \nSubject:\nEmail Content:\n\n\u270d\ufe0f Instructions:\nWrite a clear and concise reply email that responds meaningfully to the content.\n\nKeep the tone professional, polite, and appropriate to the context.\n\nDo not include greetings like \"Hi\" or sign-offs like \"Best regards\" unless necessary.\n\nYour output must be strictly in the following JSON format:\n\n\n\n{\n  \"email\": \"email address\",\n  \"subject\": \"subject text\",\n  \"body\": \" body text .\"\n}\n\n\n\nIf the email is promotional, spam, or clearly does not require a reply, leave both fields blank:\n\n\n{\n  \"subject\": \"\",\n  \"body\": \"\"\n}\n"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2
    },
    {
      "id": "988af447-e485-4397-b6e8-c848d53384f8",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -300,
        1160
      ],
      "parameters": {
        "options": {
          "baseURL": "https://api.openai.com/v1"
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "79953edf-0e9a-42de-88f7-3202eefaf851",
      "name": "OpenAI Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        520,
        1140
      ],
      "parameters": {
        "options": {
          "baseURL": "https://api.openai.com/v1"
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "d1bbf2f7-4506-4296-97fc-2044619dd524",
      "name": "Telegram: Send + Approve",
      "type": "n8n-nodes-base.telegram",
      "position": [
        900,
        920
      ],
      "parameters": {
        "chatId": "=7917193308",
        "message": "=\ud83d\udce5 *New Email Received*\n\n\ud83d\udc64 *From:*  \n{{ $('Trigger: New Email').item.json.headers.from }}\n\n\ud83d\udcdd *Subject:*  \n{{ $('Trigger: New Email').item.json.headers.subject }}\n\n\ud83d\udcac *Content:*  \n{{ $('Trigger: New Email').item.json.text }}\n\n\n\ud83e\udd16 *AI-Generated Response*\n\n\ud83d\udce4 *To:*  \n{{ $('Trigger: New Email').item.json.headers.from }}\n\n\ud83d\udcdd *Subject:*  \n{{ $json.output.subject }}\n\n\ud83d\udcac *Content:*  \n{{ $json.output.body }}\n",
        "options": {
          "limitWaitTime": {
            "values": {
              "resumeUnit": "minutes",
              "resumeAmount": 5
            }
          }
        },
        "operation": "sendAndWait",
        "approvalOptions": {
          "values": {
            "approvalType": "double"
          }
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f61104a3-2350-4d5e-b613-94b7607dadd2",
      "name": "Check: Telegram Approved?",
      "type": "n8n-nodes-base.if",
      "position": [
        1120,
        920
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "c7315e3c-6ee1-40bb-8d74-0c70862f30a4",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.data.approved }}",
              "rightValue": "true"
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "d1e81ee3-db7b-4a46-b02a-ddd299d5cadf",
      "name": " Exit: Telegram Not Approved",
      "type": "n8n-nodes-base.noOp",
      "position": [
        1340,
        1020
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "27b00238-c6fe-49bb-b147-1121f6f6c897",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1520,
        360
      ],
      "parameters": {
        "width": 3180,
        "height": 1320,
        "content": "## \ud83e\udd16 AI Email Reply Assistant\n\n## \ud83d\udd04 What this workflow does\nThis automation monitors your Gmail inbox, intelligently decides which emails need replies, generates professional responses using AI, and sends them after your approval via Telegram.\n\n---\n\n#### \ud83e\udde9 Node-by-node Breakdown\n\n1. **\ud83d\udce5 Trigger: New Email**\n   - Gmail Trigger node\n   - Polls inbox every minute for new emails\n\n2. **\ud83d\udcc2 Check: Is Email in Inbox?**\n   - Filters emails to only process those in INBOX\n   - Skips sent, drafts, and other folders\n\n3. **\ud83e\udde0 AI: Should We Reply?**\n   - AI analyzes email content to determine if reply is needed\n   - Skips promotions, newsletters, and notifications\n   - Only processes emails with questions, requests, or urgent matters\n\n4. **\u2705 Check: Response Required?**\n   - Routes emails based on AI decision\n   - Continues only if reply is needed\n\n5. **\u270d\ufe0f AI: Generate Email Reply**\n   - Creates professional, contextual email response\n   - Maintains appropriate tone and format\n   - Generates subject line and body content\n\n6. **\ud83d\udcf1 Telegram: Send + Approve**\n   - Shows original email and AI-generated response\n   - Waits for your approval via Telegram buttons\n   - 5-minute timeout for decision\n\n7. **\ud83d\ude80 Send a message**\n   - Sends the approved reply via Gmail\n   - Only executes if you approve via Telegram\n\n---\n\n#### \ud83d\udca1 Key Features\n- **Smart Filtering**: Only replies to emails that actually need responses\n- **Human Approval**: You control what gets sent\n- **Professional Tone**: AI maintains appropriate business communication\n- **Mobile Control**: Approve/reject replies directly from Telegram\n\n---\n\n#### \ud83d\udee0 Perfect for:\n- Busy professionals managing high email volume\n- Customer service representatives\n- Sales teams with quick response requirements\n- Anyone wanting AI assistance with email management\n\n---\n"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "29633a52-57b2-462e-be6e-4473426433d0",
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI: Should We Reply?",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "AI: Generate Email Reply",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Trigger: New Email": {
      "main": [
        [
          {
            "node": "Check: Is Email in Inbox?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI: Should We Reply?": {
      "main": [
        [
          {
            "node": " Check: Response Required?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI: Generate Email Reply": {
      "main": [
        [
          {
            "node": "Telegram: Send + Approve",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram: Send + Approve": {
      "main": [
        [
          {
            "node": "Check: Telegram Approved?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check: Is Email in Inbox?": {
      "main": [
        [
          {
            "node": "AI: Should We Reply?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check: Telegram Approved?": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": " Exit: Telegram Not Approved",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "AI: Generate Email Reply",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    " Check: Response Required?": {
      "main": [
        [
          {
            "node": "AI: Generate Email Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": " Exit: No Reply Needed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse: Reply Decision JSON": {
      "ai_outputParser": [
        [
          {
            "node": "AI: Should We Reply?",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  }
}