{ "name": "Email answer Approval", "nodes": [ { "parameters": { "pollTimes": { "item": [ { "mode": "everyMinute" } ] }, "filters": {} }, "type": "n8n-nodes-base.gmailTrigger", "typeVersion": 1.3, "position": [ -2896, 736 ], "id": "3f555c47-bc94-4fb8-9097-a0eb81b43fe2", "name": "Gmail Trigger", "credentials": { "gmailOAuth2": { "id": "vCP90becxj3Q6Hcj", "name": "Gmail account 3" } } }, { "parameters": { "promptType": "define", "text": "={{ $json.snippet }}", "messages": { "messageValues": [ { "message": "=You are an experienced email copywriter.\n\nYour task is to generate replies to my incoming emails. \nWrite profesional email answers. \n\n# Output: Only respond with answers for the emails, no additional texts. *And return the output as a JSON format*\n\n" } ] }, "batching": {} }, "type": "@n8n/n8n-nodes-langchain.chainLlm", "typeVersion": 1.7, "position": [ -2672, 736 ], "id": "4d11bb8c-6b04-4996-bc7a-f75224c36520", "name": "EMAIL ANSWER GENERATOR" }, { "parameters": {}, "type": "n8n-nodes-base.limit", "typeVersion": 1, "position": [ -2096, 736 ], "id": "77683a67-c829-42a3-b5e0-2e72eae5c82c", "name": "Limit" }, { "parameters": { "assignments": { "assignments": [ { "id": "d0cfa175-2f68-46e8-a0fc-969058837bd6", "name": "text", "value": "={{ $('Output parser').item.json.body }}", "type": "string" }, { "id": "50438c0f-9cda-4532-9c9e-5fb78c6c5340", "name": "Approval message", "value": "=Please approve this email answer :\n\n{{ $json.body }}", "type": "string" } ] }, "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ -1872, 736 ], "id": "dc966379-db86-4a2d-9824-707b1c016e32", "name": "Approval task" }, { "parameters": { "resume": "webhook", "httpMethod": "POST", "limitWaitTime": true, "resumeAmount": 7, "resumeUnit": "minutes", "options": {} }, "type": "n8n-nodes-base.wait", "typeVersion": 1.1, "position": [ -1424, 624 ], "id": "571adb95-8002-42ab-be7e-b6e604c75468", "name": "Wait", "webhookId": "919926a4-931e-4ea1-8e36-e50351104f37" }, { "parameters": { "model": { "__rl": true, "mode": "list", "value": "gpt-4.1-mini" }, "builtInTools": {}, "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi", "typeVersion": 1.3, "position": [ -912, 1040 ], "id": "7489786a-30df-427b-a028-4df529b5e93c", "name": "OpenAI Chat Model1", "credentials": { "openAiApi": { "id": "k9LlgnkWoOMbNkRw", "name": "OpenAi account 5" } } }, { "parameters": { "promptType": "define", "text": "=Current email reply : {{ $('Limit').item.json.body }}\n\n\nfeedback : {{ $json.body.feedback }}\n\n", "messages": { "messageValues": [ { "message": "=Your task is to implement feedback in the emal reply for the previously written reply by another (AI). \n\n\nOutput only the email reply without explanation " } ] }, "batching": {} }, "type": "@n8n/n8n-nodes-langchain.chainLlm", "typeVersion": 1.7, "position": [ -976, 816 ], "id": "eda2f55e-f6d4-4448-ba58-df50c07f7bd5", "name": "implement feedback" }, { "parameters": { "jsCode": "// Add a \"Code\" node after your EMAIL ANSWER GENERATOR\nconst rawText = $input.item.json.text;\n\n// 1. Find the JSON part inside the markdown backticks\nconst jsonMatch = rawText.match(/```json\\n([\\s\\S]*?)\\n```/);\n\nif (jsonMatch && jsonMatch[1]) {\n // 2. Parse the extracted string into a real JSON object\n const cleanObject = JSON.parse(jsonMatch[1]);\n \n // --- NEW STEP: Remove newlines from all string values ---\n for (const key in cleanObject) {\n if (typeof cleanObject[key] === 'string') {\n // Replaces all newlines with a space (prevents words from sticking together)\n // Use .replace(/\\n/g, \"\") if you want them gone entirely with no space\n cleanObject[key] = cleanObject[key].replace(/\\n/g, \" \").trim();\n }\n }\n // --------------------------------------------------------\n\n // 3. Return the object so n8n can use 'subject' and 'body' as separate fields\n return cleanObject;\n}\n\n// Fallback if no JSON found\nreturn {\n error: \"Could not find JSON in response\",\n original: rawText\n};" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -400, 560 ], "id": "bf390170-8549-46fb-9405-3b1fcbb526b6", "name": "JSON Adjuster" }, { "parameters": { "jsCode": "// Add a \"Code\" node after your EMAIL ANSWER GENERATOR\nconst rawText = $input.item.json.text;\n\n// 1. Find the JSON part inside the markdown backticks\nconst jsonMatch = rawText.match(/```json\\n([\\s\\S]*?)\\n```/);\n\nif (jsonMatch && jsonMatch[1]) {\n // 2. Parse the extracted string into a real JSON object\n const cleanObject = JSON.parse(jsonMatch[1]);\n \n // 3. Return the object so n8n can use 'subject' and 'body' as separate fields\n return cleanObject;\n}\n\n// Fallback if no JSON found\nreturn {\n error: \"Could not find JSON in response\",\n original: rawText\n};" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -2304, 736 ], "id": "4a484c82-b9b7-4772-aba1-7ceaaee70b3b", "name": "Output parser" }, { "parameters": { "model": { "__rl": true, "value": "chatgpt-4o-latest", "mode": "list", "cachedResultName": "chatgpt-4o-latest" }, "builtInTools": {}, "options": {} }, "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi", "typeVersion": 1.3, "position": [ -2608, 960 ], "id": "db6c8103-12d2-44f9-a8be-bccea85184af", "name": "OpenAI Chat Model2", "credentials": { "openAiApi": { "id": "k9LlgnkWoOMbNkRw", "name": "OpenAi account 5" } } }, { "parameters": { "rules": { "values": [ { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "leftValue": "={{ $json.body.approved }}", "rightValue": "false", "operator": { "type": "string", "operation": "equals" }, "id": "61d47cc0-4f89-43a9-bcc8-62f1d5779be7" } ], "combinator": "and" }, "renameOutput": true, "outputKey": "Rejected" }, { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "a82e980c-80ea-4128-90f0-4ca2f13bd85f", "leftValue": "={{ $json.body.approved }}", "rightValue": "true", "operator": { "type": "string", "operation": "equals" } } ], "combinator": "and" }, "renameOutput": true, "outputKey": "Approved" } ] }, "options": {} }, "type": "n8n-nodes-base.switch", "typeVersion": 3.4, "position": [ -912, 464 ], "id": "71650df1-61d7-495c-b96d-5daa1284cd3a", "name": "Switch1" }, { "parameters": { "chatId": "=6205857225", "text": "={{ $json[\"Approval message\"] }}", "replyMarkup": "inlineKeyboard", "inlineKeyboard": { "rows": [ { "row": { "buttons": [ { "text": "Approve✅", "additionalFields": { "callback_data": "={{$execution.id}}, true" } }, { "text": "Reject❌", "additionalFields": { "callback_data": "={{$execution.id}}, false" } } ] } } ] }, "additionalFields": { "appendAttribution": false, "parse_mode": "HTML" } }, "type": "n8n-nodes-base.telegram", "typeVersion": 1.2, "position": [ -1648, 624 ], "id": "2735d013-b9c5-453b-89a4-de9a438ae12d", "name": "Send a text message", "webhookId": "1f5627b4-ca0a-4d1b-be15-29d490056d10", "credentials": { "telegramApi": { "id": "VXVmxxPXgnlOCO0Z", "name": "NOAH" } } }, { "parameters": { "resource": "draft", "subject": "={{ $('Limit').item.json.subject }}", "message": "={{ $json.original }}", "options": {} }, "type": "n8n-nodes-base.gmail", "typeVersion": 2.2, "position": [ -176, 560 ], "id": "010e21c2-0fe4-4eb5-9259-5c41b9c9539b", "name": "Create a draft", "webhookId": "2acc1248-1576-4e6f-bc92-97a03164e282", "credentials": { "gmailOAuth2": { "id": "vCP90becxj3Q6Hcj", "name": "Gmail account 3" } } }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict", "version": 3 }, "conditions": [ { "id": "665069f7-0505-47c4-ae6e-7167599ba4eb", "leftValue": "={{ $json.body.feedback }}", "rightValue": "=", "operator": { "type": "string", "operation": "notEmpty", "singleValue": true } } ], "combinator": "or" }, "options": {} }, "type": "n8n-nodes-base.if", "typeVersion": 2.3, "position": [ -1200, 624 ], "id": "c9e39433-f5f9-4277-8155-412fc8763884", "name": "If" }, { "parameters": { "assignments": { "assignments": [ { "id": "bad1da51-ced8-47e7-8e84-c0aa3989b9c7", "name": "rejected", "value": "={{ $json.body.approved }}", "type": "string" } ] }, "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ -624, 368 ], "id": "c458d7bd-9b7a-4b0d-96dc-70193e368721", "name": "Rejected" }, { "parameters": { "assignments": { "assignments": [ { "id": "38a500a2-052c-4813-9de4-0d36596e29e0", "name": "text", "value": "={{ $('Send a text message').item.json.result.text }}", "type": "string" } ] }, "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ -624, 560 ], "id": "0893076d-43e0-403a-9e16-b4568d8f73a3", "name": "Approved" }, { "parameters": { "jsCode": "// Add a \"Code\" node after your EMAIL ANSWER GENERATOR\nconst rawText = $input.item.json.text;\n\n// 1. Find the JSON part inside the markdown backticks\nconst jsonMatch = rawText.match(/```json\\n([\\s\\S]*?)\\n```/);\n\nif (jsonMatch && jsonMatch[1]) {\n // 2. Parse the extracted string into a real JSON object\n const cleanObject = JSON.parse(jsonMatch[1]);\n \n // 3. Return the object so n8n can use 'subject' and 'body' as separate fields\n return cleanObject;\n}\n\n// Fallback if no JSON found\nreturn {\n error: \"Could not find JSON in response\",\n original: rawText\n};" }, "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ -624, 816 ], "id": "556dc5da-2da0-45fa-b13d-d3321579d0fe", "name": "Output parser2" }, { "parameters": { "assignments": { "assignments": [ { "id": "e5d9cb5e-a17c-4bc1-aab6-9318dbaa605a", "name": "body", "value": "={{ $json.original }}", "type": "string" } ] }, "options": {} }, "type": "n8n-nodes-base.set", "typeVersion": 3.4, "position": [ -400, 928 ], "id": "955e5333-994e-45aa-a38c-4f9d7733e00a", "name": "Edit Fields" } ], "pinData": {}, "connections": { "Gmail Trigger": { "main": [ [ { "node": "EMAIL ANSWER GENERATOR", "type": "main", "index": 0 } ] ] }, "EMAIL ANSWER GENERATOR": { "main": [ [ { "node": "Output parser", "type": "main", "index": 0 } ] ] }, "Limit": { "main": [ [ { "node": "Approval task", "type": "main", "index": 0 } ] ] }, "Approval task": { "main": [ [ { "node": "Send a text message", "type": "main", "index": 0 } ] ] }, "Wait": { "main": [ [ { "node": "If", "type": "main", "index": 0 } ] ] }, "OpenAI Chat Model1": { "ai_languageModel": [ [ { "node": "implement feedback", "type": "ai_languageModel", "index": 0 } ] ] }, "JSON Adjuster": { "main": [ [ { "node": "Create a draft", "type": "main", "index": 0 } ] ] }, "Output parser": { "main": [ [ { "node": "Limit", "type": "main", "index": 0 } ] ] }, "OpenAI Chat Model2": { "ai_languageModel": [ [ { "node": "EMAIL ANSWER GENERATOR", "type": "ai_languageModel", "index": 0 } ] ] }, "Switch1": { "main": [ [ { "node": "Rejected", "type": "main", "index": 0 } ], [ { "node": "Approved", "type": "main", "index": 0 } ] ] }, "Send a text message": { "main": [ [ { "node": "Wait", "type": "main", "index": 0 } ] ] }, "implement feedback": { "main": [ [ { "node": "Output parser2", "type": "main", "index": 0 } ] ] }, "If": { "main": [ [ { "node": "implement feedback", "type": "main", "index": 0 } ], [ { "node": "Switch1", "type": "main", "index": 0 } ] ] }, "Approved": { "main": [ [ { "node": "JSON Adjuster", "type": "main", "index": 0 } ] ] }, "Output parser2": { "main": [ [ { "node": "Edit Fields", "type": "main", "index": 0 } ] ] }, "Edit Fields": { "main": [ [ { "node": "Approval task", "type": "main", "index": 0 } ] ] } }, "active": false, "settings": { "executionOrder": "v1" }, "versionId": "da7781a9-3e34-49da-a907-8a124441b0fb", "meta": { "templateCredsSetupCompleted": true, "instanceId": "23830929e6c018b604abb710d12ca57ce411c63a552f9a21234ab1d153211b0b" }, "id": "yXuNhrrkw9B2dCt1", "tags": [] }