WhatsApp business bot
工作流概述
这是一个包含15个节点的复杂工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "NzoLNV2FbS4eurJ7",
"meta": {
"instanceId": "fb924c73af8f703905bc09c9ee8076f48c17b596ed05b18c0ff86915ef8a7c4a",
"templateCredsSetupCompleted": true
},
"name": "WhatsApp business bot",
"tags": [],
"nodes": [
{
"id": "4ca5e25a-f67b-4874-af20-680d1a6ac731",
"name": "Sent notification template",
"type": "n8n-nodes-base.whatsApp",
"position": [
1140,
320
],
"parameters": {
"template": "test_appointment_button|en_US",
"components": {
"component": [
{
"type": "header",
"headerParameters": {
"parameter": [
{
"text": "📅"
}
]
}
},
{
"bodyParameters": {
"parameter": [
{
"text": "={{ $json[\"Your name\"] }}"
},
{
"text": "={{ DateTime.fromFormat($json[\"Please pick a day and time of your appointment\"], \"M/d/yyyy HH:mm:ss\").toLocaleString(DateTime.DATE_HUGE); }}"
},
{
"text": "={{ $json[\"Please pick a day and time of your appointment\"].split(' ')[1] }}"
}
]
}
}
]
},
"phoneNumberId": "=244242975437240",
"requestOptions": {},
"recipientPhoneNumber": "={{ $json[\"Your mobile number\"] }}"
},
"credentials": {
"whatsAppApi": {
"id": "mm0r1xKc6N8XktAD",
"name": "WhatsApp account 2"
}
},
"typeVersion": 1
},
{
"id": "877c62c5-9869-48fc-bd74-35897dbd2276",
"name": "WhatsApp Trigger",
"type": "n8n-nodes-base.whatsAppTrigger",
"position": [
700,
800
],
"webhookId": "b06b387a-481e-43f1-9035-01a87123ad88",
"parameters": {
"updates": [
"messages"
]
},
"credentials": {
"whatsAppTriggerApi": {
"id": "bWqGRWeDXvGTdSq5",
"name": "WhatsApp Trigger"
}
},
"typeVersion": 1
},
{
"id": "6c0edf48-20af-42fb-a436-aee3a9a4f6cc",
"name": "Is message?",
"type": "n8n-nodes-base.if",
"position": [
920,
800
],
"parameters": {
"options": {
"looseTypeValidation": true
},
"conditions": {
"options": {
"leftValue": "",
"caseSensitive": true,
"typeValidation": "loose"
},
"combinator": "and",
"conditions": [
{
"id": "8a765e57-8e39-4547-a99a-0458df2b75f4",
"operator": {
"type": "object",
"operation": "exists",
"singleValue": true
},
"leftValue": "={{ $json.messages[0] }}",
"rightValue": ""
}
]
}
},
"typeVersion": 2
},
{
"id": "00006406-47be-4693-9763-a21d06b13d51",
"name": "Schedule Trigger",
"type": "n8n-nodes-base.scheduleTrigger",
"position": [
680,
1184
],
"parameters": {
"rule": {
"interval": [
{
"field": "minutes"
}
]
}
},
"typeVersion": 1.2
},
{
"id": "b9919c0d-eeb2-4a5e-a91f-3dad11b778f8",
"name": "Loop Over Items",
"type": "n8n-nodes-base.splitInBatches",
"position": [
1120,
1184
],
"parameters": {
"options": {}
},
"typeVersion": 3
},
{
"id": "8f0dc664-715f-4074-b0f7-98d3c7f563a5",
"name": "Get new answers",
"type": "n8n-nodes-base.googleSheets",
"position": [
900,
1184
],
"parameters": {
"options": {},
"filtersUI": {
"values": [
{
"lookupValue": "Ready",
"lookupColumn": "Status"
}
]
},
"sheetName": {
"__rl": true,
"mode": "list",
"value": 1621824221,
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit#gid=1621824221",
"cachedResultName": "WA-messages"
},
"documentId": {
"__rl": true,
"mode": "list",
"value": "1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit?usp=drivesdk",
"cachedResultName": "WhatsApp Appointments (Responses)"
}
},
"credentials": {
"googleSheetsOAuth2Api": {
"id": "RtRiRezoxiWkzZQt",
"name": "Ted's Tech Talks Google account"
}
},
"typeVersion": 4.4
},
{
"id": "a7b07f7e-1287-4e8f-b28a-4c656f386f8a",
"name": "Reply to the user",
"type": "n8n-nodes-base.whatsApp",
"position": [
1340,
1184
],
"parameters": {
"textBody": "={{ $json.ReplyText }}",
"operation": "send",
"phoneNumberId": "244242975437240",
"requestOptions": {},
"additionalFields": {},
"recipientPhoneNumber": "=+{{ $json.UserPhone }}"
},
"credentials": {
"whatsAppApi": {
"id": "mm0r1xKc6N8XktAD",
"name": "WhatsApp account 2"
}
},
"typeVersion": 1
},
{
"id": "30f0a7da-c3ce-448c-ad05-b8b75da3d319",
"name": "Update message status",
"type": "n8n-nodes-base.googleSheets",
"position": [
1520,
1184
],
"parameters": {
"columns": {
"value": {
"Status": "Replied",
"row_number": "={{ $('Loop Over Items').item.json.row_number }}"
},
"schema": [
{
"id": "UserPhone",
"type": "string",
"display": true,
"removed": true,
"required": false,
"displayName": "UserPhone",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "UserName",
"type": "string",
"display": true,
"removed": true,
"required": false,
"displayName": "UserName",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "UserMessage",
"type": "string",
"display": true,
"removed": true,
"required": false,
"displayName": "UserMessage",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "ReplyText",
"type": "string",
"display": true,
"removed": true,
"required": false,
"displayName": "ReplyText",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "Status",
"type": "string",
"display": true,
"required": false,
"displayName": "Status",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "row_number",
"type": "string",
"display": true,
"removed": false,
"readOnly": true,
"required": false,
"displayName": "row_number",
"defaultMatch": false,
"canBeUsedToMatch": true
}
],
"mappingMode": "defineBelow",
"matchingColumns": [
"row_number"
]
},
"options": {},
"operation": "update",
"sheetName": {
"__rl": true,
"mode": "list",
"value": 1621824221,
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit#gid=1621824221",
"cachedResultName": "WA-messages"
},
"documentId": {
"__rl": true,
"mode": "list",
"value": "1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit?usp=drivesdk",
"cachedResultName": "WhatsApp Appointments (Responses)"
}
},
"credentials": {
"googleSheetsOAuth2Api": {
"id": "RtRiRezoxiWkzZQt",
"name": "Ted's Tech Talks Google account"
}
},
"typeVersion": 4.4
},
{
"id": "95486a27-a667-4555-8924-53d46b19de43",
"name": "Wait 1 sec",
"type": "n8n-nodes-base.wait",
"position": [
1700,
1184
],
"webhookId": "df4df4f8-378c-4228-b1e2-326b9d956e7e",
"parameters": {
"amount": 1
},
"typeVersion": 1.1
},
{
"id": "21551e78-428f-4730-a337-48d1a80bf703",
"name": "New message from the user",
"type": "n8n-nodes-base.googleSheets",
"position": [
1140,
800
],
"parameters": {
"columns": {
"value": {
"Status": "New",
"UserName": "={{ $json.contacts[0].profile.name }}",
"UserPhone": "={{ $json.messages[0].from }}",
"UserMessage": "={{ $json.messages[0].text.body }}"
},
"schema": [
{
"id": "UserPhone",
"type": "string",
"display": true,
"required": false,
"displayName": "UserPhone",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "UserName",
"type": "string",
"display": true,
"required": false,
"displayName": "UserName",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "UserMessage",
"type": "string",
"display": true,
"required": false,
"displayName": "UserMessage",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "ReplyText",
"type": "string",
"display": true,
"removed": true,
"required": false,
"displayName": "ReplyText",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "Status",
"type": "string",
"display": true,
"required": false,
"displayName": "Status",
"defaultMatch": false,
"canBeUsedToMatch": true
}
],
"mappingMode": "defineBelow",
"matchingColumns": []
},
"options": {},
"operation": "append",
"sheetName": {
"__rl": true,
"mode": "list",
"value": 1621824221,
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit#gid=1621824221",
"cachedResultName": "WA-messages"
},
"documentId": {
"__rl": true,
"mode": "list",
"value": "1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit?usp=drivesdk",
"cachedResultName": "WhatsApp Appointments (Responses)"
}
},
"credentials": {
"googleSheetsOAuth2Api": {
"id": "RtRiRezoxiWkzZQt",
"name": "Ted's Tech Talks Google account"
}
},
"typeVersion": 4.4
},
{
"id": "e1478757-0094-4bcb-998f-7e3e81958319",
"name": "Get new entries",
"type": "n8n-nodes-base.googleSheetsTrigger",
"position": [
700,
320
],
"parameters": {
"event": "rowAdded",
"options": {},
"pollTimes": {
"item": [
{
"mode": "everyX",
"unit": "minutes",
"value": 5
}
]
},
"sheetName": {
"__rl": true,
"mode": "list",
"value": 470797219,
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit#gid=470797219",
"cachedResultName": "Form Responses 1"
},
"documentId": {
"__rl": true,
"mode": "list",
"value": "1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit?usp=drivesdk",
"cachedResultName": "WhatsApp Appointments (Responses)"
}
},
"credentials": {
"googleSheetsTriggerOAuth2Api": {
"id": "m33qCYf9eEvSgo0x",
"name": "Ted's Tech Talks Google Sheets Trigger"
}
},
"typeVersion": 1
},
{
"id": "14811434-d716-4999-ab53-761fc355ee09",
"name": "User consented for WA messages?",
"type": "n8n-nodes-base.filter",
"position": [
920,
320
],
"parameters": {
"options": {},
"conditions": {
"options": {
"leftValue": "",
"caseSensitive": true,
"typeValidation": "strict"
},
"combinator": "and",
"conditions": [
{
"id": "b9bfdb33-0d9c-4320-b4bc-0bf0a469c8ca",
"operator": {
"name": "filter.operator.equals",
"type": "string",
"operation": "equals"
},
"leftValue": "={{ $json[\"I consent to receive WhatsApp notifications regarding my appointments\"] }}",
"rightValue": "Yes"
}
]
}
},
"typeVersion": 2
},
{
"id": "20bec538-5d04-4382-ba88-a2c15421c8e7",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
660,
83.6407185628741
],
"parameters": {
"width": 744.5356369854154,
"height": 404.8383233532937,
"content": "## Send a WhatsApp (WA) template message
Occurs after a user submitted a new Google form. If the user opted-in for WA notifications during the form submission, a template message will be sent via WhatsApp.
**IMPORTANT!**
1. You will need to create a new WA template message on the [Meta Business portal](https://business.facebook.com/wa/manage/message-templates/)
2. To send outgoing WA messages you'll need an Access Token and a WhatsApp Business Account ID. These can be obtained via Meta Developers Portal after creating an a new App. Please refer to this [n8n blog article on creating WhatsApp bots](https://blog.n8n.io/whatsapp-bot/#step-1-set-up-a-whatsapp-business-account)"
},
"typeVersion": 1
},
{
"id": "ab7bd838-2ed1-4645-b3d9-69617a888090",
"name": "Sticky Note1",
"type": "n8n-nodes-base.stickyNote",
"position": [
660,
526.8263473053893
],
"parameters": {
"width": 752.168692512586,
"height": 437.60479041916165,
"content": "## Store incoming WhatsApp user messages in a Google Sheet
To receive user messages, you need to add a WhatsApp Trigger node. In the credentials section provide an App ID and an App secret. These are obtained from the Meta Developers Portal, Basic App settings screen
After the credentials are added, copy the trigger URL and enter it into the 'Callback URL' field in the WhatsApp configuration window in the Meta Developer portal.
Once the trigger receives a payload from WhatsApp, we check if the incoming data contains a message and add a new row with user data and message text in [Google Sheet](https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit?gid=1621824221#gid=1621824221).
WhatsApp trigger also receives status notifications (i.e. message sent, message read etc.), so we ignore such notifications in this workflow."
},
"typeVersion": 1
},
{
"id": "b0c62bd4-d6bc-425b-b506-b6820b3e6dc5",
"name": "Sticky Note2",
"type": "n8n-nodes-base.stickyNote",
"position": [
660,
1000
],
"parameters": {
"width": 1197.9640718562885,
"height": 369.34131736526945,
"content": "## Reply to the user via WhatsApp
WhatsApp allows sending automatic messages **with custom text** via bots only within the 24h time frame after the last incoming user message.
After the user sends a message to the WhatsApp bot, a row is added to the [Google Sheet](https://docs.google.com/spreadsheets/d/1T-B0yepcrCHxQpn7Sj6QjTa0VqwwVBQhO5ZcIUSxWJE/edit?gid=1621824221#gid=1621824221) with the Status 'New'
Type something in the `ReplyText` column and change the Status to 'Ready'.
In a few minutes, n8n timer will fetch all 'Ready' replies from the Google Sheet and send them one by one to the recipients"
},
"typeVersion": 1
}
],
"active": true,
"pinData": {},
"settings": {
"callerPolicy": "workflowsFromSameOwner",
"executionOrder": "v1",
"saveManualExecutions": true,
"saveDataSuccessExecution": "all"
},
"versionId": "66863e99-c756-48d5-b8e0-af0907623e8a",
"connections": {
"Wait 1 sec": {
"main": [
[
{
"node": "Loop Over Items",
"type": "main",
"index": 0
}
]
]
},
"Is message?": {
"main": [
[
{
"node": "New message from the user",
"type": "main",
"index": 0
}
]
]
},
"Get new answers": {
"main": [
[
{
"node": "Loop Over Items",
"type": "main",
"index": 0
}
]
]
},
"Get new entries": {
"main": [
[
{
"node": "User consented for WA messages?",
"type": "main",
"index": 0
}
]
]
},
"Loop Over Items": {
"main": [
[],
[
{
"node": "Reply to the user",
"type": "main",
"index": 0
}
]
]
},
"Schedule Trigger": {
"main": [
[
{
"node": "Get new answers",
"type": "main",
"index": 0
}
]
]
},
"WhatsApp Trigger": {
"main": [
[
{
"node": "Is message?",
"type": "main",
"index": 0
}
]
]
},
"Reply to the user": {
"main": [
[
{
"node": "Update message status",
"type": "main",
"index": 0
}
]
]
},
"Update message status": {
"main": [
[
{
"node": "Wait 1 sec",
"type": "main",
"index": 0
}
]
]
},
"User consented for WA messages?": {
"main": [
[
{
"node": "Sent notification template",
"type": "main",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- Whatsapptrigger
- If
- Scheduletrigger
- Splitinbatches
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的Gmail账户
- n8n平台访问权限
- Google API凭证
- AI分类服务订阅
配置步骤
- 在n8n中导入工作流JSON文件
- 配置Gmail节点的认证信息
- 设置AI分类器的API密钥
- 自定义分类规则和标签映射
- 测试工作流执行
- 配置定时触发器(可选)
关键参数
| 参数名称 | 默认值 | 说明 |
|---|---|---|
| maxEmails | 50 | 单次处理的最大邮件数量 |
| confidenceThreshold | 0.8 | 分类置信度阈值 |
| autoLabel | true | 是否自动添加标签 |
最佳实践
优化建议
- 定期更新AI分类模型以提高准确性
- 根据邮件量调整处理批次大小
- 设置合理的分类置信度阈值
- 定期清理过期的分类规则
安全注意事项
- 妥善保管API密钥和认证信息
- 限制工作流的访问权限
- 定期审查处理日志
- 启用双因素认证保护Gmail账户
性能优化
- 使用增量处理减少重复工作
- 缓存频繁访问的数据
- 并行处理多个邮件分类任务
- 监控系统资源使用情况
故障排除
常见问题
邮件未被正确分类
检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。
Gmail认证失败
确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。
调试技巧
- 启用详细日志记录查看每个步骤的执行情况
- 使用测试邮件验证分类逻辑
- 检查网络连接和API服务状态
- 逐步执行工作流定位问题节点
错误处理
工作流包含以下错误处理机制:
- 网络超时自动重试(最多3次)
- API错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作