{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "78a9a2ba-dfbd-4b29-abb0-0d8e50fca199",
      "name": "Basic LLM Chain",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "maxTries": 2,
      "position": [
        60,
        260
      ],
      "parameters": {
        "text": "=Convert this email search request into a valid Gmail search query. Output ONLY JSON with:\n1. \"search\": The complete Gmail query using operators (from:, subject:, after:, etc.)\n2. \"metadata\": Extracted components for reference\n\nRules:\n- Use exact Gmail search syntax\n- Convert dates to YYYY/MM/DD or newer_than/older_than\n- Group related terms with (parentheses)\n- Prioritize exact matches with \"quotes\"\n- Include multiple variations of keywords (e.g., invoice/invoices)\n\nExample Output:\n```json\n{\n  \"search\": \"from:amazon AND (invoice OR invoices) AND after:2024/06/01\",\n  \"metadata\": {\n    \"senders\": [\"amazon\"],\n    \"keywords\": [\"invoice\", \"invoices\"],\n    \"timeFilters\": {\"after\": \"2024/06/01\"}\n  }\n}\n\nUser Input: {{ $('User Request - Telegram Trigger').item.json.message.text }}",
        "promptType": "define",
        "hasOutputParser": true
      },
      "executeOnce": false,
      "retryOnFail": true,
      "typeVersion": 1.6,
      "alwaysOutputData": false
    },
    {
      "id": "967d4a5f-f911-4c1d-b9ee-36f7edddac96",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        160,
        600
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.0-flash"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e5b584a1-c153-46bc-adb3-00a6e355ba99",
      "name": "Google Gemini Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        980,
        480
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.0-flash"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "7cd5d1d5-a75f-43d6-9e68-2212396394df",
      "name": "Basic LLM Chain1",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        1020,
        260
      ],
      "parameters": {
        "text": "=Convert this email data into a well-formatted Telegram message with these sections:\n1. \ud83d\udcc5 Date (DD/MM/YYYY format)\n2. \ud83d\udc64 Sender (extract name & email)\n3. \ud83d\udccc Subject\n4. \u2702\ufe0f Preview (full snippet)\n\nRules:\n- Use emojis for visual separation\n- Keep entire message under 400 characters\n- Format dates as \"09/06/2025\"\n- Extract names from \"From\" field (remove quotes/email)\n- Full snippets that conveys the full summarised email (max 4 lines)\n\nExample Output:\n\ud83d\udcc5Date:  09/06/2025  \n\ud83d\udc64Sender: elad malka (example@gmail.com)\n\ud83d\udcccSubject Line: Share request for \"Acuity to Sheets\"  \n\u2702\ufe0fSnippet: Requesting access to spreadsheet: Acuity to Sheets \n\nEmail Data: \ndate: {{ $json.Date }}\nfrom: {{ $json.From }}\nsubject: {{ $json.Subject }}\nsender: {{ $json.snippet }}",
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.6
    },
    {
      "id": "30113c5d-07b9-4015-ab26-9a69fc874038",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -500,
        260
      ],
      "parameters": {
        "text": "=Analyze the user's request (User request:{{ $json.message.text }} )  about fetching Gmail emails and output a JSON with:\n1. `sender` (name or email, if specified).\n2. `keywords` (list of words/phrases to search for).\n3. `afterDate` (ISO format: YYYY-MM-DD, ONLY if user specifies \"after [date]\" or \"since [date]\").\n4. `beforeDate` (ISO format: YYYY-MM-DD, ONLY if user specifies \"before [date]\" or \"until [date]\").\n\nRules:\n- Convert relative dates (e.g., \"since Monday\") to ISO format using TODAY as reference.\n- For ambiguous dates (e.g., \"last week\"), use the afterDate (calculated from present date to last 7 days).\n- Strictly return ONLY valid JSON.\n\nExamples:\n\nUser Input: \"Get emails from Amazon after 2024-06-10 about returns\"\nOutput:\n```json\n{\n  \"sender\": \"Amazon\",\n  \"keywords\": [\"returns\"],\n  \"afterDate\": \"2024-06-10\"\n} ",
        "options": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 1.9
    },
    {
      "id": "ed601ecc-c4c0-4de1-9e3a-cb398ffe35c8",
      "name": "Google Gemini Chat Model2",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -460,
        600
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.0-flash"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5c577624-52e0-4a17-8604-80e2cd4b77de",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        -200,
        600
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"sender\": \"Amazon\",\n  \"keywords\": [\"returns\"],\n  \"afterDate\": \"2024-06-10\",\n  \"beforeDate\": \"2024-06-30\"\n} "
      },
      "typeVersion": 1.2
    },
    {
      "id": "371362b5-44c3-490a-bea9-a5e1d0246fd9",
      "name": "Structured Output Parser1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        420,
        600
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"search\": \"from:amazon AND (invoice OR invoices) AND after:2024/06/01\",\n  \"metadata\": {\n    \"senders\": [\"amazon\"],\n    \"keywords\": [\"invoice\", \"invoices\"],\n    \"timeFilters\": {\"after\": \"2024/06/01\"}\n  }\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "2e2dc24c-f458-4339-86bf-6501e84c4ea3",
      "name": "Structured Output Parser2",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1220,
        500
      ],
      "parameters": {
        "jsonSchemaExample": "{\n\t\"text\": \"\ud83d\udcc5Date: 09/06/2025\\n\ud83d\udc64Sender: elad malka (user@example.com)\\n\ud83d\udcccSubject: Share request for \"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "c2aa8043-0df7-48fe-87eb-f6bcdc09a6f9",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -960,
        -20
      ],
      "parameters": {
        "width": 360,
        "height": 480,
        "content": "## 1. Telegram Bot (API)\n1. Create a bot using Bot_Father on telegram by sending a message to @BotFather.\n\n2. Send a command /newbot in the message.\n\n3. Choose username and then copy the api.\n\n4. Create Telegram credential here in n8n. By inputting the api token in access token and base url (https://api.telegram.org).\n\n5. Hit Save.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "eb75fe77-953b-4c81-8d42-79b70171b947",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -580,
        500
      ],
      "parameters": {
        "width": 340,
        "height": 260,
        "content": "Replace the API with your own API of Gemini or choose an OpenAi node if you want to use openai model. (depends on your choice)."
      },
      "typeVersion": 1
    },
    {
      "id": "faca346a-8a08-49d2-902d-a29cfb366228",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        40,
        500
      ],
      "parameters": {
        "width": 340,
        "height": 260,
        "content": "Replace the API with your own API of Gemini or choose an OpenAi node if you want to use openai model. (depends on your choice)."
      },
      "typeVersion": 1
    },
    {
      "id": "4067630a-3888-42f9-ad7e-e01f87d6430e",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        440,
        -80
      ],
      "parameters": {
        "width": 320,
        "height": 520,
        "content": "## 2. Connecting Your Gmail Account.\n\n1. Connect your Gmail account \u2013 Choose the account you want to retrieve emails from.\n\n2. Set the email limit \u2013 Enter the number of emails you want to fetch, or check \"Return all\" to retrieve every email matching your request.\n\n3. Select read status \u2013 Choose whether to fetch unread or read emails."
      },
      "typeVersion": 1
    },
    {
      "id": "be41decd-60dc-4055-acc7-9ffc8201c7a3",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1400,
        20
      ],
      "parameters": {
        "width": 340,
        "height": 420,
        "content": "## 3. Chat ID.\n\n1. Get the Chat ID \u2013 Copy the output from the first Telegram trigger node, then pass it to an AI model to extract the Chat ID.\n\n2. Use the Chat ID \u2013 Once identified, simply drag and drop the Chat ID value into the field.\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "d3371952-c3aa-4cd9-ab08-594b4760ad48",
      "name": "User Request - Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -800,
        260
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "683f78bd-1ac4-4b51-90ed-17f0675bc1cb",
      "name": "Set Parameters From User Request (for fetching emails)",
      "type": "n8n-nodes-base.set",
      "position": [
        -140,
        260
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "93d9bf21-89b5-4544-80db-ed0cc875cb5c",
              "name": "Sender",
              "type": "string",
              "value": "={{ $json.output.sender }}"
            },
            {
              "id": "933fae65-1251-4995-be9a-03691e30d1c7",
              "name": "Keywords/Prompt/Criteria",
              "type": "string",
              "value": "={{ $json.output.keywords }}"
            },
            {
              "id": "bf4c646a-5b8e-49a3-a1a2-68836c731404",
              "name": "afterDate",
              "type": "string",
              "value": "={{ $json.output.afterDate }}"
            },
            {
              "id": "2a91fd77-77e9-4c3f-8f30-17a97fb9871d",
              "name": "beforeDate",
              "type": "string",
              "value": "={{ $json.output.beforeDate }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "fb2cb2c8-59e9-40e6-abe0-447335e499fb",
      "name": "Gets Requested Email(s)",
      "type": "n8n-nodes-base.gmail",
      "position": [
        540,
        260
      ],
      "parameters": {
        "limit": 10,
        "filters": {
          "q": "={{ $json.output.search }}",
          "sender": "={{ $('Set Parameters From User Request (for fetching emails)').item.json.Sender }}",
          "readStatus": "unread",
          "receivedAfter": "={{ $('Set Parameters From User Request (for fetching emails)').item.json.afterDate }}",
          "receivedBefore": "={{ $('Set Parameters From User Request (for fetching emails)').item.json.beforeDate }}"
        },
        "operation": "getAll"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "4b229ce7-c941-4d6c-a999-26054468490c",
      "name": "Sets Parameters For Response",
      "type": "n8n-nodes-base.set",
      "position": [
        840,
        260
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "7e8b9cac-da96-4568-b520-8f64bed8b35b",
              "name": "Date",
              "type": "string",
              "value": "={{ new Date(Number($json.internalDate)).toISOString() }}"
            },
            {
              "id": "6a422a1c-395c-4759-b642-40b33295b8d1",
              "name": "From",
              "type": "string",
              "value": "={{ $json.From }}"
            },
            {
              "id": "85b67fb6-f4ed-4672-becd-43aa682c5cdf",
              "name": "Subject",
              "type": "string",
              "value": "={{ $json.Subject }}"
            },
            {
              "id": "9b4fa0e0-da5a-4c3a-a049-ec1c2ac9c600",
              "name": "snippet",
              "type": "string",
              "value": "={{ $json.snippet }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "e7501362-37d8-4a05-919a-72ad419ee59a",
      "name": "Sends (requested emails) via Telegram back",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1500,
        260
      ],
      "parameters": {
        "text": "={{ $json.output.text }}",
        "chatId": "={{ $('User Request - Telegram Trigger').item.json.message.chat.id }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Set Parameters From User Request (for fetching emails)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Basic LLM Chain": {
      "main": [
        [
          {
            "node": "Gets Requested Email(s)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Basic LLM Chain1": {
      "main": [
        [
          {
            "node": "Sends (requested emails) via Telegram back",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gets Requested Email(s)": {
      "main": [
        [
          {
            "node": "Sets Parameters For Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Basic LLM Chain1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser2": {
      "ai_outputParser": [
        [
          {
            "node": "Basic LLM Chain1",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Sets Parameters For Response": {
      "main": [
        [
          {
            "node": "Basic LLM Chain1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "User Request - Telegram Trigger": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Parameters From User Request (for fetching emails)": {
      "main": [
        [
          {
            "node": "Basic LLM Chain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}