8
n8n 中文网amn8n.com

Gmail智能自动回复器,含GPT-4o和Google Drive上下文记忆

高级

这是一个Personal Productivity, AI Chatbot领域的自动化工作流,包含 18 个节点。主要使用 If, Set, Code, Gmail, GoogleDrive 等节点。 Gmail智能自动回复器,含GPT-4o和Google Drive上下文记忆

前置要求
  • Google 账号和 Gmail API 凭证
  • Google Drive API 凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "074f90e2bb5206c5f405a8aac6551497c72005283a5405fb08207b1b3a78c2b8",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "gmail-trigger-001",
      "name": "Gmail 触发器",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        560,
        400
      ],
      "parameters": {
        "simple": false,
        "filters": {
          "q": "-from:me"
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CRED_ID"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "drive-download-001",
      "name": "Google Drive - 下载配置文件",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        760,
        260
      ],
      "parameters": {
        "fileId": "YOUR_PROFILE_DOC_FILE_ID",
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_GOOGLE_DRIVE_CRED_ID"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "gmail-thread-001",
      "name": "Gmail - 获取线程",
      "type": "n8n-nodes-base.gmail",
      "position": [
        760,
        540
      ],
      "parameters": {
        "threadId": "={{ $json.threadId }}",
        "operation": "getAll"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CRED_ID"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "profile-loader-001",
      "name": "配置文件文档加载器",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        980,
        260
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "type",
                "value": "user_profile"
              }
            ]
          }
        },
        "jsonData": "={{ $json.content }}",
        "jsonMode": "expressionData"
      },
      "typeVersion": 1
    },
    {
      "id": "thread-loader-001",
      "name": "线程文档加载器",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        980,
        540
      ],
      "parameters": {
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "type",
                "value": "email_thread"
              }
            ]
          }
        },
        "jsonData": "={{ JSON.stringify($json.messages) }}",
        "jsonMode": "expressionData"
      },
      "typeVersion": 1
    },
    {
      "id": "profile-splitter-001",
      "name": "配置文件文本分割器",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        1200,
        260
      ],
      "parameters": {
        "chunkSize": 1000,
        "chunkOverlap": 100
      },
      "typeVersion": 1
    },
    {
      "id": "thread-splitter-001",
      "name": "线程文本分割器",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        1200,
        540
      ],
      "parameters": {
        "chunkSize": 2000,
        "chunkOverlap": 200
      },
      "typeVersion": 1
    },
    {
      "id": "openai-embeddings-001",
      "name": "OpenAI Embeddings",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "position": [
        1420,
        400
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "YOUR_OPENAI_CRED_ID"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "vector-store-001",
      "name": "简单向量存储",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "position": [
        1640,
        400
      ],
      "parameters": {
        "mode": "insert",
        "memoryKey": "email_context_{{ $('Gmail Trigger').item.json.threadId }}"
      },
      "typeVersion": 1
    },
    {
      "id": "prepare-context-001",
      "name": "准备 Agent 上下文",
      "type": "n8n-nodes-base.set",
      "position": [
        1860,
        400
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "context-text",
              "name": "text",
              "type": "string",
              "value": "=Please analyze this incoming email and generate an appropriate reply based on the user profile and email thread context.\n\nIncoming Email Subject: {{ $('Gmail Trigger').item.json.subject }}\nIncoming Email From: {{ $('Gmail Trigger').item.json.headers.from }}\nIncoming Email Body: {{ $('Gmail Trigger').item.json.textAsHtml }}\n\nInstructions:\n1. If this appears to be a newsletter, automated notification, or spam, respond with exactly 'NO_REPLY_NEEDED'\n2. If this requires a response, generate a professional and contextual reply\n3. Use the knowledge from the vector store to inform your response\n4. Keep responses concise but helpful"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "openai-chat-001",
      "name": "OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        2080,
        580
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {
          "temperature": 0.3
        }
      },
      "credentials": {
        "openAiApi": {
          "id": "YOUR_OPENAI_CRED_ID"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "simple-memory-001",
      "name": "简单记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        2180,
        580
      ],
      "parameters": {
        "sessionKey": "=email_{{ $('Gmail Trigger').item.json.threadId }}",
        "maxTokenLimit": 4000,
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "vector-retriever-001",
      "name": "Vector Store Retriever",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreInMemory",
      "position": [
        2280,
        580
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "limit": 5,
        "toolName": "email_context",
        "memoryKey": "email_context_{{ $('Gmail Trigger').item.json.threadId }}",
        "toolDescription": "Retrieve context about the user profile and email thread history"
      },
      "typeVersion": 1
    },
    {
      "id": "email-agent-001",
      "name": "邮件回复 Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        2080,
        400
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {
          "systemMessage": "You are an intelligent email assistant. Your job is to analyze incoming emails and generate appropriate responses based on:\n\n1. User profile information (from vector store)\n2. Email thread context (from vector store)\n3. The specific incoming email content\n\nGuidelines:\n- For newsletters, promotions, automated messages: respond with exactly 'NO_REPLY_NEEDED'\n- For legitimate emails requiring responses: generate professional, contextual replies\n- Keep responses concise but helpful\n- Use the user's communication style from their profile\n- Reference relevant context from previous emails when appropriate\n- Always be polite and professional"
        },
        "promptType": "define"
      },
      "typeVersion": 1.9
    },
    {
      "id": "check-response-001",
      "name": "检查是否需要回复",
      "type": "n8n-nodes-base.if",
      "position": [
        2400,
        400
      ],
      "parameters": {
        "conditions": {
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "notContainsText"
              },
              "leftValue": "={{ $json.output }}",
              "rightValue": "NO_REPLY_NEEDED"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "create-draft-001",
      "name": "Gmail - 创建草稿",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2680,
        400
      ],
      "parameters": {
        "message": "={{ $json.output.replace(/\\n/g, \"<br />\\n\") }}",
        "options": {
          "sendTo": "={{ $('Gmail Trigger').item.json.headers.from }}",
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}"
        },
        "subject": "=Re: {{ $('Gmail Trigger').item.json.subject }}",
        "resource": "draft",
        "emailType": "html"
      },
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CRED_ID"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "error-handler-001",
      "name": "错误处理器",
      "type": "n8n-nodes-base.code",
      "position": [
        2400,
        600
      ],
      "parameters": {
        "jsCode": "// Log error details for debugging\nconst errorData = {\n  timestamp: new Date().toISOString(),\n  error: $input.all(),\n  workflow: 'Gmail Auto Reply',\n  threadId: $('Gmail Trigger').item?.json?.threadId || 'unknown'\n};\n\nconsole.log('Workflow Error:', JSON.stringify(errorData, null, 2));\n\nreturn [{ json: errorData }];"
      },
      "typeVersion": 2
    },
    {
      "id": "sticky-note-001",
      "name": "工作流说明",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        200,
        200
      ],
      "parameters": {
        "color": 5,
        "width": 300,
        "height": 400,
        "content": "# Gmail 自动回复与 AI"
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "3",
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Google Drive - Download Profile",
            "type": "main",
            "index": 0
          },
          {
            "node": "Gmail - Fetch Thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "Email Reply Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Email Reply Agent": {
      "main": [
        [
          {
            "node": "Check If Reply Needed",
            "type": "main",
            "index": 0
          }
        ]
      ],
      "error": [
        [
          {
            "node": "Error Handler",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Email Reply Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Embeddings": {
      "ai_embedding": [
        [
          {
            "node": "Simple Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Simple Vector Store": {
      "main": [
        [
          {
            "node": "Prepare Agent Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail - Create Draft": {
      "error": [
        [
          {
            "node": "Error Handler",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail - Fetch Thread": {
      "main": [
        [
          {
            "node": "Thread Document Loader",
            "type": "main",
            "index": 0
          }
        ]
      ],
      "error": [
        [
          {
            "node": "Error Handler",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Thread Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Thread Document Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Check If Reply Needed": {
      "main": [
        [
          {
            "node": "Gmail - Create Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Agent Context": {
      "main": [
        [
          {
            "node": "Email Reply Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Profile Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Profile Document Loader",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Thread Document Loader": {
      "ai_document": [
        [
          {
            "node": "Simple Vector Store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Vector Store Retriever": {
      "ai_tool": [
        [
          {
            "node": "Email Reply Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Profile Document Loader": {
      "ai_document": [
        [
          {
            "node": "Simple Vector Store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive - Download Profile": {
      "main": [
        [
          {
            "node": "Profile Document Loader",
            "type": "main",
            "index": 0
          }
        ]
      ],
      "error": [
        [
          {
            "node": "Error Handler",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 个人效率, AI 聊天机器人

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量18
分类2
节点类型14
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

外部链接
在 n8n.io 查看

分享此工作流