Workflow management
工作流概述
这是一个包含19个节点的复杂工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "48",
"name": "Workflow management",
"nodes": [
{
"name": "On clicking 'execute'",
"type": "n8n-nodes-base.manualTrigger",
"position": [
240,
300
],
"parameters": {},
"typeVersion": 1
},
{
"name": "Function",
"type": "n8n-nodes-base.function",
"position": [
570,
300
],
"parameters": {
"functionCode": "//console.log(items[0].json.data);
let data = items[0].json.data;
items = data.map(i => {
// console.log({json:i});
return {json:i};
});
//console.log(items);
return items;"
},
"typeVersion": 1
},
{
"name": "SplitInBatches",
"type": "n8n-nodes-base.splitInBatches",
"position": [
760,
300
],
"parameters": {
"options": {},
"batchSize": 1
},
"typeVersion": 1
},
{
"name": "IF",
"type": "n8n-nodes-base.if",
"position": [
2090,
570
],
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{$node[\"SplitInBatches\"].context[\"noItemsLeft\"]}}",
"value2": true
}
]
}
},
"typeVersion": 1
},
{
"name": "NoOp",
"type": "n8n-nodes-base.noOp",
"position": [
2270,
550
],
"parameters": {},
"typeVersion": 1
},
{
"name": "Airtable",
"type": "n8n-nodes-base.airtable",
"position": [
1100,
200
],
"parameters": {
"table": "Workflows",
"operation": "list",
"application": "<YOUR_APP_ID>",
"additionalOptions": {
"fields": [],
"filterByFormula": "=workflowId={{$node[\"Get Workflow Details\"].json[\"data\"][\"id\"]}}"
}
},
"credentials": {
"airtableApi": "n8n management demo"
},
"typeVersion": 1,
"alwaysOutputData": true
},
{
"name": "Airtable1",
"type": "n8n-nodes-base.airtable",
"position": [
1750,
130
],
"parameters": {
"id": "={{$node[\"Airtable\"].json[\"id\"]}}",
"table": "Workflows",
"options": {
"typecast": true
},
"operation": "update",
"application": "<YOUR_APP_ID>"
},
"credentials": {
"airtableApi": "n8n management demo"
},
"typeVersion": 1
},
{
"name": "Airtable2",
"type": "n8n-nodes-base.airtable",
"position": [
1750,
320
],
"parameters": {
"table": "Workflows",
"options": {
"typecast": true
},
"operation": "append",
"application": "<YOUR_APP_ID>"
},
"credentials": {
"airtableApi": "n8n management demo"
},
"typeVersion": 1
},
{
"name": "Set",
"type": "n8n-nodes-base.set",
"position": [
1590,
130
],
"parameters": {
"values": {
"string": [
{
"name": "workflowId",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"id\"]}}"
},
{
"name": "name",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"name\"]}}"
},
{
"name": "errorWorkflowId",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"settings\"][\"errorWorkflow\"]}}"
},
{
"name": "createdAt",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"createdAt\"]}}"
},
{
"name": "updatedAt",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"updatedAt\"]}}"
},
{
"name": "nodes",
"value": "={{$node[\"Prepare data\"].json[\"fields\"][\"nodes\"]}}"
},
{
"name": "timezone",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"settings\"][\"timezone\"]}}"
},
{
"name": "CRON_details",
"value": "={{$node[\"Prepare data\"].json[\"fields\"][\"CRON_details\"]}}"
},
{
"name": "rawData",
"value": "={{$node[\"Prepare data\"].json[\"fields\"][\"rawData\"]}}"
}
],
"boolean": [
{
"name": "isActive",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"active\"]}}"
},
{
"name": "isCRON",
"value": "={{$node[\"Prepare data\"].json[\"fields\"][\"isCRON\"]}}"
},
{
"name": "saveManualExecutions",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"settings\"][\"saveManualExecutions\"]}}"
},
{
"name": "isTrigger",
"value": "={{$node[\"Prepare data\"].json[\"fields\"][\"isTrigger\"]}}"
}
]
},
"options": {},
"keepOnlySet": true
},
"typeVersion": 1
},
{
"name": "Set1",
"type": "n8n-nodes-base.set",
"position": [
1590,
320
],
"parameters": {
"values": {
"string": [
{
"name": "workflowId",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"id\"]}}"
},
{
"name": "name",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"name\"]}}"
},
{
"name": "errorWorkflowId",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"settings\"][\"errorWorkflow\"]}}"
},
{
"name": "createdAt",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"createdAt\"]}}"
},
{
"name": "updatedAt",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"updatedAt\"]}}"
},
{
"name": "nodes",
"value": "={{$node[\"Prepare data1\"].json[\"fields\"][\"nodes\"]}}"
},
{
"name": "timezone",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"settings\"][\"timezone\"]}}"
},
{
"name": "CRON_details",
"value": "={{$node[\"Prepare data1\"].json[\"fields\"][\"CRON_details\"]}}"
},
{
"name": "rawData",
"value": "={{$node[\"Prepare data1\"].json[\"fields\"][\"rawData\"]}}"
}
],
"boolean": [
{
"name": "isActive",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"active\"]}}"
},
{
"name": "isCRON",
"value": "={{$node[\"Prepare data1\"].json[\"fields\"][\"isCRON\"]}}"
},
{
"name": "saveManualExecutions",
"value": "={{$node[\"Get Workflow Details\"].json[\"data\"][\"settings\"][\"saveManualExecutions\"]}}"
},
{
"name": "isTrigger",
"value": "={{$node[\"Prepare data1\"].json[\"fields\"][\"isTrigger\"]}}"
}
]
},
"options": {},
"keepOnlySet": true
},
"typeVersion": 1
},
{
"name": "Get All Workflows",
"type": "n8n-nodes-base.httpRequest",
"position": [
410,
300
],
"parameters": {
"url": "http://localhost:5678/rest/workflows",
"options": {
"fullResponse": false
},
"headerParametersUi": {
"parameter": [
{
"name": "Authorization",
"value": "<TOKEN>"
}
]
},
"allowUnauthorizedCerts": true
},
"typeVersion": 1
},
{
"name": "Prepare data",
"type": "n8n-nodes-base.function",
"position": [
1430,
130
],
"parameters": {
"functionCode": "let data = $node[\"Get Workflow Details\"].json[\"data\"];
let file = $node[\"Get file link\"].json[\"link\"];
let nodes = new Set(data[\"nodes\"].map(i => i.type));
let nodes2 = [...nodes];
//console.log(...nodes);
let data2 = data[\"nodes\"].map(i => i.name);
if(nodes2.includes('n8n-nodes-base.cron')){
console.log('Cron found!');
// console.log(data);
let cron_node = data[\"nodes\"].filter(i => i.type == 'n8n-nodes-base.cron');
//console.log(cron_node[0].parameters.triggerTimes.item);
items[0].json[\"fields\"][\"isCRON\"]=true;
items[0].json[\"fields\"][\"nodes\"]=[...nodes];
items[0].json[\"fields\"][\"CRON_details\"]=cron_node[0].parameters.triggerTimes.item;
items[0].json[\"fields\"][\"rawData\"]=[{url:file ,filename: 'workflow_'+data[\"id\"]+'__'+data[\"updatedAt\"]+'.json'}];
} else {
//console.log('Cron not found!');
items[0].json[\"fields\"][\"isCRON\"]=false;
items[0].json[\"fields\"][\"nodes\"]=[...nodes];
items[0].json[\"fields\"][\"rawData\"]=[{url:file ,filename: 'workflow_'+data[\"id\"]+'__'+data[\"updatedAt\"]+'.json'}];
}
if(nodes2.some(i => {
let regExp = new RegExp(/n8n-nodes-base\.[\w]+Trigger/);
if(i=='n8n-nodes-base.webhook'){
return true;
}
if(regExp.test(i)){
return true;
}
return false;
})){
items[0].json[\"fields\"][\"isTrigger\"]=true;
} else {
items[0].json[\"fields\"][\"isTrigger\"]=false;
}
//console.log(items);
return items;
"
},
"typeVersion": 1
},
{
"name": "Prepare data1",
"type": "n8n-nodes-base.function",
"position": [
1430,
320
],
"parameters": {
"functionCode": "let data = $node[\"Get Workflow Details\"].json[\"data\"];
let file = $node[\"Get file link\"].json[\"link\"];
let nodes = new Set(data[\"nodes\"].map(i => i.type));
let nodes2 = [...nodes];
//console.log(data);
let data2 = data[\"nodes\"].map(i => i.name);
if(nodes2.includes('n8n-nodes-base.cron')){
//console.log('Cron found!');
let cron_node = data[\"nodes\"].filter(i => i.type == 'n8n-nodes-base.cron');
items[0].json={
fields:{
isCRON:true,
nodes:[...nodes],
CRON_details:cron_node[0].parameters.triggerTimes.item,
rawData:[{url:file ,filename: 'workflow_'+data[\"id\"]+'__'+data[\"updatedAt\"]+'.json'}]
}
};
} else {
//console.log('Cron not found!');
items[0].json={
fields:{
isCRON:false,
nodes:[...nodes],
rawData:[{url:file ,filename: 'workflow_'+data[\"id\"]+'__'+data[\"updatedAt\"]+'.json'}]
}
};
}
if(nodes2.some(i => {
let regExp = new RegExp(/n8n-nodes-base\.[\w]+Trigger/);
if(i=='n8n-nodes-base.webhook'){
return true;
}
if(regExp.test(i)){
return true;
}
return false;
})){
items[0].json[\"fields\"][\"isTrigger\"]=true;
} else {
items[0].json[\"fields\"][\"isTrigger\"]=false;
}
//console.log(items);
return items;
"
},
"typeVersion": 1
},
{
"name": "Cron",
"type": "n8n-nodes-base.cron",
"position": [
250,
510
],
"parameters": {
"triggerTimes": {
"item": [
{
"mode": "everyHour",
"minute": 15
},
{
"mode": "everyHour",
"minute": 45
}
]
}
},
"typeVersion": 1
},
{
"name": "Move Binary Data",
"type": "n8n-nodes-base.moveBinaryData",
"position": [
1000,
-10
],
"parameters": {
"mode": "jsonToBinary",
"options": {
"keepSource": true
}
},
"typeVersion": 1
},
{
"name": "Dropbox",
"type": "n8n-nodes-base.dropbox",
"position": [
1140,
-10
],
"parameters": {
"path": "=/workflows/workflow_{{$node[\"Get Workflow Details\"].json[\"data\"][\"id\"]}}/workflow_{{$node[\"Get Workflow Details\"].json[\"data\"][\"id\"]}}__{{$node[\"Get Workflow Details\"].json[\"data\"][\"updatedAt\"]}}.json",
"binaryData": true
},
"credentials": {
"dropboxApi": "My n8n backups"
},
"typeVersion": 1
},
{
"name": "Get Workflow Details",
"type": "n8n-nodes-base.httpRequest",
"position": [
840,
-10
],
"parameters": {
"url": "=http://localhost:5678/rest/workflows/{{$node[\"SplitInBatches\"].json[\"id\"]}}",
"options": {},
"headerParametersUi": {
"parameter": [
{
"name": "Authorization",
"value": "<TOKEN>"
}
]
},
"allowUnauthorizedCerts": true
},
"typeVersion": 1
},
{
"name": "Get file link",
"type": "n8n-nodes-base.httpRequest",
"position": [
1290,
-10
],
"parameters": {
"url": "https://api.dropboxapi.com/2/files/get_temporary_link",
"options": {},
"requestMethod": "POST",
"bodyParametersUi": {
"parameter": [
{
"name": "path",
"value": "={{$node[\"Dropbox\"].json[\"path_lower\"]}}"
}
]
},
"headerParametersUi": {
"parameter": [
{
"name": "Authorization",
"value": "<TOKEN>"
}
]
}
},
"typeVersion": 1,
"continueOnFail": true,
"alwaysOutputData": true
},
{
"name": "IF Airtable record exists?",
"type": "n8n-nodes-base.if",
"position": [
1270,
200
],
"parameters": {
"conditions": {
"boolean": [
{
"value1": "={{$node[\"Airtable\"].json[\"id\"] != \"\" && $node[\"Airtable\"].json[\"id\"] != null && $node[\"Airtable\"].json[\"id\"] != undefined}}",
"value2": true
}
]
}
},
"typeVersion": 1
}
],
"active": true,
"settings": {
"errorWorkflow": "5"
},
"connections": {
"IF": {
"main": [
[
{
"node": "NoOp",
"type": "main",
"index": 0
}
],
[
{
"node": "SplitInBatches",
"type": "main",
"index": 0
}
]
]
},
"Set": {
"main": [
[
{
"node": "Airtable1",
"type": "main",
"index": 0
}
]
]
},
"Cron": {
"main": [
[
{
"node": "Get All Workflows",
"type": "main",
"index": 0
}
]
]
},
"Set1": {
"main": [
[
{
"node": "Airtable2",
"type": "main",
"index": 0
}
]
]
},
"Dropbox": {
"main": [
[
{
"node": "Get file link",
"type": "main",
"index": 0
}
]
]
},
"Airtable": {
"main": [
[
{
"node": "IF Airtable record exists?",
"type": "main",
"index": 0
}
]
]
},
"Function": {
"main": [
[
{
"node": "SplitInBatches",
"type": "main",
"index": 0
}
]
]
},
"Airtable1": {
"main": [
[
{
"node": "IF",
"type": "main",
"index": 0
}
]
]
},
"Airtable2": {
"main": [
[
{
"node": "IF",
"type": "main",
"index": 0
}
]
]
},
"Prepare data": {
"main": [
[
{
"node": "Set",
"type": "main",
"index": 0
}
]
]
},
"Get file link": {
"main": [
[
{
"node": "Airtable",
"type": "main",
"index": 0
}
]
]
},
"Prepare data1": {
"main": [
[
{
"node": "Set1",
"type": "main",
"index": 0
}
]
]
},
"SplitInBatches": {
"main": [
[
{
"node": "Get Workflow Details",
"type": "main",
"index": 0
}
]
]
},
"Move Binary Data": {
"main": [
[
{
"node": "Dropbox",
"type": "main",
"index": 0
}
]
]
},
"Get All Workflows": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
},
"Get Workflow Details": {
"main": [
[
{
"node": "Move Binary Data",
"type": "main",
"index": 0
}
]
]
},
"On clicking 'execute'": {
"main": [
[
{
"node": "Get All Workflows",
"type": "main",
"index": 0
}
]
]
},
"IF Airtable record exists?": {
"main": [
[
{
"node": "Prepare data",
"type": "main",
"index": 0
}
],
[
{
"node": "Prepare data1",
"type": "main",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- Manualtrigger
- Function
- Splitinbatches
- If
- Noop
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的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错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作