经典工作流案例
本章将为您展示多种实际应用场景的n8n工作流案例,帮助您理解如何在实际业务中应用工作流自动化技术。
营销自动化案例
1. 智能邮件营销系统
这个工作流可以根据用户行为自动发送个性化的营销邮件。
业务场景
- 新用户注册时发送欢迎邮件
- 用户购买后发送感谢邮件
- 长期未活跃用户发送唤回邮件
- 节日期间发送促销邮件
工作流设计
核心节点配置
Webhook节点:
{
"httpMethod": "POST",
"path": "marketing-trigger",
"responseMode": "respondWith200"
}
Switch节点 - 事件分发:
// 根据事件类型分发到不同分支
const eventType = $json.event_type;
switch(eventType) {
case 'user_registered':
return [0]; // 用户注册分支
case 'order_completed':
return [1]; // 订单完成分支
case 'inactive_check':
return [2]; // 非活跃用户检查分支
default:
return [];
}
Function节点 - 邮件内容生成:
// 生成个性化邮件内容
const user = $json.user;
const eventType = $json.event_type;
let emailContent = {};
if (eventType === 'user_registered') {
emailContent = {
subject: `欢迎加入我们,${user.firstName}!`,
html: `
<h2>欢迎您,${user.firstName}!</h2>
<p>感谢您注册我们的服务。</p>
<p>作为新用户,您可以享受首次购买8折优惠。</p>
<a href="https://yoursite.com/shop?code=WELCOME20">立即购买</a>
`,
to: user.email
};
} else if (eventType === 'order_completed') {
emailContent = {
subject: `感谢您的购买,${user.firstName}!`,
html: `
<h2>订单确认</h2>
<p>亲爱的${user.firstName},</p>
<p>您的订单 #${$json.order.id} 已确认。</p>
<p>预计发货时间:2-3个工作日</p>
`,
to: user.email
};
}
return { json: emailContent };
关键特性
- 📧 多触发器支持(Webhook、定时器)
- 🎯 基于用户行为的智能分发
- 📝 动态邮件内容生成
- 📊 完整的发送日志记录
- 🔄 失败重试机制
2. 社交媒体自动发布
自动将内容发布到多个社交媒体平台。
工作流组件
- RSS触发器: 监听博客或新 闻源
- 内容处理: 格式化适合不同平台的内容
- 多平台发布: 同时发布到Twitter、LinkedIn、Facebook
- 结果汇总: 统计发布结果和互动数据
实现代码
// 内容格式化函数
function formatForPlatform(content, platform) {
const maxLength = {
'twitter': 280,
'linkedin': 700,
'facebook': 500
};
let formattedContent = content;
if (formattedContent.length > maxLength[platform]) {
formattedContent = formattedContent.substring(0, maxLength[platform] - 3) + '...';
}
// 添加平台特定的标签
if (platform === 'twitter') {
formattedContent += ' #automation #n8n';
} else if (platform === 'linkedin') {
formattedContent += '\n\n#技术分享 #自动化';
}
return formattedContent;
}
// 处理RSS内容
const rssItem = $json;
const platforms = ['twitter', 'linkedin', 'facebook'];
const posts = platforms.map(platform => ({
platform: platform,
content: formatForPlatform(rssItem.contentSnippet, platform),
title: rssItem.title,
url: rssItem.link
}));
return posts.map(post => ({ json: post }));
数据处理案例
1. 电商数据分析流水线
处理电商平台的销售数据,生成分析报告。
数据流程
核心处理逻辑
数据清洗节点:
// 清洗和标准化订单数据
const orders = $input.all().map(item => item.json);
const cleanedOrders = [];
orders.forEach(order => {
// 数据验证和清理
if (order.amount && order.date && order.status) {
cleanedOrders.push({
orderId: order.id,
amount: parseFloat(order.amount),
date: new Date(order.date),
status: order.status.toLowerCase(),
customerType: order.customer?.type || 'unknown',
productCategory: order.items?.[0]?.category || 'other'
});
}
});
return cleanedOrders.map(order => ({ json: order }));
数据聚合节点:
// 生成销售统计数据
const orders = $input.all().map(item => item.json);
// 按日期聚合
const dailySales = {};
const categoryStats = {};
orders.forEach(order => {
const dateKey = order.date.toISOString().split('T')[0];
// 日销售额
dailySales[dateKey] = (dailySales[dateKey] || 0) + order.amount;
// 分类统计
categoryStats[order.productCategory] = {
count: (categoryStats[order.productCategory]?.count || 0) + 1,
revenue: (categoryStats[order.productCategory]?.revenue || 0) + order.amount
};
});
// 计算增长率
const dates = Object.keys(dailySales).sort();
const growthRate = dates.length > 1 ?
((dailySales[dates[dates.length - 1]] - dailySales[dates[0]]) / dailySales[dates[0]] * 100).toFixed(2) : 0;
return {
json: {
totalRevenue: orders.reduce((sum, order) => sum + order.amount, 0),
totalOrders: orders.length,
averageOrderValue: orders.reduce((sum, order) => sum + order.amount, 0) / orders.length,
dailySales,
categoryStats,
growthRate: `${growthRate}%`,
reportDate: new Date().toISOString()
}
};
2. CSV数据处理和转换
批量处理CSV文件,转换格式并导入到数据库。
工作流特点
- 📁 监听文件目录变化
- 📊 解析多种CSV格式
- 🔄 数据格式转换和验证
- 💾 批量导入数据库
- 📧 处理结果通知
文件处理逻辑
// CSV数据转换函数
function transformCSVData(csvData, mappingRules) {
const transformed = csvData.map(row => {
const newRow = {};
Object.keys(mappingRules).forEach(targetField => {
const sourceField = mappingRules[targetField];
if (typeof sourceField === 'string') {
newRow[targetField] = row[sourceField];
} else if (typeof sourceField === 'function') {
newRow[targetField] = sourceField(row);
}
});
return newRow;
});
return transformed;
}
// 使用示例
const csvData = $json.data;
const mappingRules = {
'customer_id': 'CustomerID',
'full_name': row => `${row.FirstName} ${row.LastName}`,
'email': 'Email',
'registration_date': row => new Date(row.RegDate).toISOString(),
'total_spent': row => parseFloat(row.TotalSpent || 0)
};
const transformedData = transformCSVData(csvData, mappingRules);
return transformedData.map(row => ({ json: row }));
客户服务案例
1. 智能客服工单系统
自动处理客户咨询,分配工单并跟踪处理进度。
系统流程
智能分类算法
// 工单优先级分类
function classifyTicket(content, subject) {
const urgentKeywords = ['紧急', '无法登录', '系统崩溃', '数据丢失', '支付失败'];
const mediumKeywords = ['帮助', '问题', '咨询', '建议'];
const lowKeywords = ['信息', '了解', '一般咨询'];
const text = (subject + ' ' + content).toLowerCase();
// 检查紧急关键词
if (urgentKeywords.some(keyword => text.includes(keyword))) {
return {
priority: 'high',
estimatedTime: '2小时内',
department: 'technical_support',
autoResponse: false
};
}
// 检查中等优先级
if (mediumKeywords.some(keyword => text.includes(keyword))) {
return {
priority: 'medium',
estimatedTime: '24小时内',
department: 'customer_service',
autoResponse: false
};
}
// 默认低优先级
return {
priority: 'low',
estimatedTime: '3个工作日',
department: 'general_support',
autoResponse: true
};
}
// 处理工单
const ticket = $json;
const classification = classifyTicket(ticket.content, ticket.subject);
return {
json: {
ticketId: `T${Date.now()}`,
customerId: ticket.customer_id,
subject: ticket.subject,
content: ticket.content,
...classification,
createdAt: new Date().toISOString(),
status: 'open'
}
};
2. 客户反馈收集和分析
自动收集客户反馈,进行情感分析并生成报告。
反馈处理流程
- 📝 多渠道反馈收集(邮件、表单、社交媒体)
- 🧠 AI情感分析
- 📊 趋势分析和报告生成
- 🔄 自动问题升级
- 📧 团 队通知和跟进
IT运维案例
1. 服务器监控和告警
监控服务器状态,异常时自动发送告警并尝试修复。
监控工作流
// 服务器健康检查
async function checkServerHealth(serverConfig) {
const checks = {
ping: false,
cpu: 0,
memory: 0,
disk: 0,
services: []
};
try {
// CPU使用率检查
const cpuUsage = await getCPUUsage(serverConfig.host);
checks.cpu = cpuUsage;
// 内存使用率检查
const memoryUsage = await getMemoryUsage(serverConfig.host);
checks.memory = memoryUsage;
// 磁盘使用率检查
const diskUsage = await getDiskUsage(serverConfig.host);
checks.disk = diskUsage;
checks.ping = true;
} catch (error) {
console.error(`服务器 ${serverConfig.name} 检查失败:`, error);
}
return checks;
}
// 告警判断
function shouldAlert(checks, thresholds) {
const alerts = [];
if (!checks.ping) {
alerts.push({ type: 'critical', message: '服务器无响应' });
}
if (checks.cpu > thresholds.cpu) {
alerts.push({ type: 'warning', message: `CPU使用率过高: ${checks.cpu}%` });
}
if (checks.memory > thresholds.memory) {
alerts.push({ type: 'warning', message: `内存使用率过高: ${checks.memory}%` });
}
if (checks.disk > thresholds.disk) {
alerts.push({ type: 'critical', message: `磁盘使用率过高: ${checks.disk}%` });
}
return alerts;
}
// 执行检查
const serverConfig = $json.server;
const thresholds = $json.thresholds || {
cpu: 80,
memory: 85,
disk: 90
};
const healthChecks = await checkServerHealth(serverConfig);
const alerts = shouldAlert(healthChecks, thresholds);
return {
json: {
serverId: serverConfig.id,
serverName: serverConfig.name,
checkTime: new Date().toISOString(),
healthStatus: healthChecks,
alerts: alerts,
needsAttention: alerts.length > 0
}
};
2. 自动化部署流水线
代码提交后自动触发测试、构建和部署流程。
部署流程设计
- 🔄 Git Webhook触发
- 🧪 自动化测试执行
- 🏗️ 构建和打包
- 🚀 多环境部署
- 📊 部署结果通知
- 🔙 失败自动回滚
文档和内容管理案例
1. 知识库自动更新
监控技术文档变更,自动更新知识库并通知团队。
更新流程
// 文档变更检测
function detectDocumentChanges(oldDoc, newDoc) {
const changes = {
title: oldDoc.title !== newDoc.title,
content: oldDoc.content !== newDoc.content,
tags: JSON.stringify(oldDoc.tags) !== JSON.stringify(newDoc.tags),
lastModified: oldDoc.lastModified !== newDoc.lastModified
};
const changesSummary = Object.keys(changes)
.filter(key => changes[key])
.map(key => {
switch(key) {
case 'title': return '标题更新';
case 'content': return '内容修改';
case 'tags': return '标签变更';
case 'lastModified': return '修改时间更新';
}
});
return {
hasChanges: changesSummary.length > 0,
changes: changesSummary,
changeCount: changesSummary.length
};
}
// 生成更新通知
function generateUpdateNotification(document, changes) {
return {
subject: `知识库文档更新: ${document.title}`,
content: `
文档 "${document.title}" 已更新
变更内容:
${changes.changes.map(change => `• ${change}`).join('\n')}
文档链接: ${document.url}
更新时间: ${new Date().toLocaleString()}
修改者: ${document.author}
`,
priority: changes.changeCount > 2 ? 'high' : 'normal'
};
}
const document = $json.document;
const oldVersion = $json.oldVersion;
const changeDetection = detectDocumentChanges(oldVersion, document);
if (changeDetection.hasChanges) {
const notification = generateUpdateNotification(document, changeDetection);
return { json: { ...document, notification, changes: changeDetection } };
} else {
return { json: { ...document, skipNotification: true } };
}