8
n8n 中文网amn8n.com

whatsapp-商务支持

中级

这是一个Support Chatbot, AI Chatbot领域的自动化工作流,包含 13 个节点。主要使用 If, Code, DateTime, WhatsApp, GoogleDocs 等节点。 WhatsApp客户支持机器人,使用Gemini AI和Google文档知识库

前置要求
  • Google Sheets API 凭证
  • Google Gemini API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "eOnn0usOtRXAWFnA",
  "meta": {
    "instanceId": "9b335fa52c546d5c6158999e0c6e4bff2bbbb8a64c600fcbaf9588ca5c18ff7d",
    "templateCredsSetupCompleted": true
  },
  "name": "whatsapp-商务支持",
  "tags": [],
  "nodes": [
    {
      "id": "d0cdd573-db7f-4bad-957f-35414a3cb26f",
      "name": "清理回答",
      "type": "n8n-nodes-base.code",
      "position": [
        -240,
        160
      ],
      "parameters": {
        "jsCode": "// cleanAnswer – run once per item\nlet txt = $('AI Agent').first().json.output || '';\n\n// 1. Remove bold / italic / strike markers\ntxt = txt.replace(/[*_~]+/g, '');\n\n// 2. Convert [Texto](https://url) → Texto https://url\ntxt = txt.replace(/\\[([^\\]]+)\\]\\((https?:\\/\\/[^\\s)]+)\\)/g, '$1 $2');\n\n// 3. Collapse 3+ blank lines\ntxt = txt.replace(/\\n{3,}/g, '\\n\\n').trim();\n\n// 4. Remove the unwanted source-reference preamble\ntxt = txt.replace(/^.*?based on the document you provided[,:]?\\s*/i, '');\n\nreturn [{ json: { answer: txt } }];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "71f15de1-8cdf-482b-b239-7594fa5a70ab",
      "name": "24小时窗口检查",
      "type": "n8n-nodes-base.code",
      "position": [
        -680,
        260
      ],
      "parameters": {
        "jsCode": "// within24h?  – run once per item\n// Meta (WhatsApp) timestamp arrives as seconds since epoch\nconst lastTs = Number($('when message received').first().json.messages[0].timestamp) * 1000;   // → ms\nconst withinWindow = Date.now() - lastTs < 24 * 60 * 60 * 1000;\n\nreturn [{ json: { withinWindow, answer: $json.answer, userId: $json.userId } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "82f43fa3-efa2-4c83-98fa-9e16e315f35f",
      "name": "条件判断",
      "type": "n8n-nodes-base.if",
      "position": [
        -460,
        260
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "d33e218e-a49a-49ed-9c6b-55b9ea0b0dbb",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.withinWindow }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "ade2b1ca-8fb0-4622-b156-07518a0c25c0",
      "name": "发送 AI 代理回答",
      "type": "n8n-nodes-base.whatsApp",
      "position": [
        -20,
        160
      ],
      "webhookId": "8da6eeda-31ec-448a-9d75-2d5dbc5f10f9",
      "parameters": {
        "textBody": "={{ $json.answer }}",
        "operation": "send",
        "phoneNumberId": "687083431157048",
        "additionalFields": {},
        "recipientPhoneNumber": "={{ $('when message received').item.json.contacts[0].wa_id }}"
      },
      "credentials": {
        "whatsAppApi": {
          "id": "L6Ezo9L9L9iO6fnt",
          "name": "WhatsApp account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2e28f789-9e3c-4ceb-aac3-b4da028b3bec",
      "name": "Google Gemini 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -1460,
        480
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "id": "yUhCnIuDvIzXVadJ",
          "name": "Google Gemini(PaLM) Api account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2904f065-fc81-4d24-9ba5-490927c2e0b4",
      "name": "当收到消息时",
      "type": "n8n-nodes-base.whatsAppTrigger",
      "position": [
        -2140,
        260
      ],
      "webhookId": "6b645b70-61db-4d4d-af47-2f39295cf196",
      "parameters": {
        "options": {},
        "updates": [
          "messages"
        ]
      },
      "credentials": {
        "whatsAppTriggerApi": {
          "id": "o05Jk36W2gtL9pNU",
          "name": "WhatsApp OAuth account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "2631cd36-f4b8-42f3-85ac-1aa044ae584e",
      "name": "日期与时间",
      "type": "n8n-nodes-base.dateTime",
      "position": [
        -1120,
        260
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "664c3660-7a71-41eb-a269-cf6eee3ed27c",
      "name": "公司知识库",
      "type": "n8n-nodes-base.googleDocs",
      "position": [
        -1920,
        260
      ],
      "parameters": {
        "operation": "get",
        "documentURL": "=1dwioGWqvY3qTDzb8olUsc1EaJJkn7_ySeRC0AaE4NZQ"
      },
      "credentials": {
        "googleDocsOAuth2Api": {
          "id": "ymt0N6m9vFBPthUy",
          "name": "Google Docs account"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "7a9bb146-c1d8-4ec4-96fd-a234f700c6eb",
      "name": "简单记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -1340,
        480
      ],
      "parameters": {
        "sessionKey": "={{ $('when message received').item.json.contacts[0].wa_id }}",
        "sessionIdType": "customKey"
      },
      "typeVersion": 1.3
    },
    {
      "id": "e3710403-d218-4861-9514-91c68d696986",
      "name": "在表格中追加或更新行",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -900,
        260
      ],
      "parameters": {
        "columns": {
          "value": {
            "User": "={{ $('when message received').item.json.messages[0].from }}",
            "Message": "={{ $('when message received').item.json.messages[0].text.body }}",
            "Response": "={{ $('AI Agent').item.json.output }}",
            "Timestamp": "={{ $json.currentDate }}"
          },
          "schema": [
            {
              "id": "Timestamp",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "User",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "User",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Message",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Message",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Response",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Response",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "Timestamp"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "appendOrUpdate",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1Q-i3MNfTBLU8pf4Vhg1zR3PHWCiMDDc6buo35N708x8/edit#gid=0",
          "cachedResultName": "Sayfa1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1Q-i3MNfTBLU8pf4Vhg1zR3PHWCiMDDc6buo35N708x8",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1Q-i3MNfTBLU8pf4Vhg1zR3PHWCiMDDc6buo35N708x8/edit?usp=drivesdk",
          "cachedResultName": "history-whatsapp"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "0A2HzCF9xOmwKi6u",
          "name": "Google Sheets account"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "2466e7ce-b5ea-4598-a0d4-deee18cc1ff9",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "maxTries": 5,
      "position": [
        -1480,
        260
      ],
      "parameters": {
        "text": "={{ $json.finalPrompt }}\n",
        "options": {
          "systemMessage": "=Sen TechFlow Yazılım Şirketi için müşteri asistanısın. \nKuralların: \nİnsan gibi davranmanı istiyorum. \n\n\n"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "retryOnFail": false,
      "typeVersion": 1.7,
      "alwaysOutputData": true,
      "waitBetweenTries": null
    },
    {
      "id": "7d61f55c-fe0c-4197-8951-955fb9e12222",
      "name": "准备提示词",
      "type": "n8n-nodes-base.aiTransform",
      "position": [
        -1700,
        260
      ],
      "parameters": {
        "jsCode": "const googleDocs = $input.all().map((item) => item.json);\nconst whatsappMessages = $(\"when message received\")\n  .all()\n  .map((item) => item.json);\n\nconst date = new Date();\nconst formattedDate = `${date.getMonth() + 1} ${date.getDate()}, ${date.getFullYear()}`;\n\nconst docText = googleDocs[0].content.split(\"\\n\").join(\" \");\n\nconst body = whatsappMessages[0].messages[0].text.body;\n\nconst finalPrompt = `Today's date is: ${formattedDate}\\n\\n${docText}\\n\\nUser's question:\\n${body}`;\n\nreturn { finalPrompt };\n",
        "instructions": "Write code to:\n• Get today’s date formatted “Month Day, Year”\n• Extract the Google Doc’s plain text by joining its body.content textRuns\n• Extract the WhatsApp message from messages[0].text.body\n• Build a field finalPrompt exactly as:\n\nvbnet\nCopy\nEdit\nToday's date is: [date]\n\n[doc text]\n\nUser's question:\n[body]\n• Return finalPrompt only.",
        "codeGeneratedForPrompt": "Write code to:\n• Get today’s date formatted “Month Day, Year”\n• Extract the Google Doc’s plain text by joining its body.content textRuns\n• Extract the WhatsApp message from messages[0].text.body\n• Build a field finalPrompt exactly as:\n\nvbnet\nCopy\nEdit\nToday's date is: [date]\n\n[doc text]\n\nUser's question:\n[body]\n• Return finalPrompt only."
      },
      "typeVersion": 1
    },
    {
      "id": "dc461011-4125-4d46-83d0-195105d4bcde",
      "name": "发送消息",
      "type": "n8n-nodes-base.whatsApp",
      "position": [
        -260,
        380
      ],
      "webhookId": "ac3e8306-8e32-4d7e-ba8a-46763999eb89",
      "parameters": {
        "textBody": "Mesajımızı 24 saat cevaplayamadığınız için süresi dolmuştur. Lütfen tekrar sohbet başlatın.",
        "operation": "send",
        "phoneNumberId": "687083431157048",
        "additionalFields": {},
        "recipientPhoneNumber": "={{ $('when message received').item.json.contacts[0].wa_id }}"
      },
      "credentials": {
        "whatsAppApi": {
          "id": "L6Ezo9L9L9iO6fnt",
          "name": "WhatsApp account"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2928be66-1df6-4ba5-90b2-86ab8859bd70",
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "cleanAnswer",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Append or update row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "cleanAnswer": {
      "main": [
        [
          {
            "node": "Send AI Agent's Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Prompt": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "company's knowledge": {
      "main": [
        [
          {
            "node": "Prepare Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "24-hour window check": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "when message received": {
      "main": [
        [
          {
            "node": "company's knowledge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Append or update row in sheet": {
      "main": [
        [
          {
            "node": "24-hour window check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 客服机器人, AI 聊天机器人

需要付费吗?

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

工作流信息
难度等级
中级
节点数量13
分类2
节点类型11
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Caner Bayrak

Caner Bayrak

@bayrak

Over the last three months, I've been intensively learning and applying skills in database management and WhatsApp automation. My goal is to create efficient systems that bridge communication and data needs

外部链接
在 n8n.io 查看

分享此工作流