Skip to main content
Glama
leeguooooo
by leeguooooo
email_monitoring_workflow.json11.7 kB
{ "name": "智能邮件监控与通知", "nodes": [ { "parameters": { "rule": { "interval": [ { "field": "cronExpression", "expression": "*/5 * * * *" } ] } }, "id": "cron-trigger", "name": "定时触发器", "type": "n8n-nodes-base.cron", "typeVersion": 1, "position": [ 240, 300 ] }, { "parameters": { "command": "python3", "arguments": "/Users/leo/github.com/mcp-email-service/scripts/email_monitor.py run", "options": { "cwd": "/Users/leo/github.com/mcp-email-service", "timeout": 600000, "output": "json", "continueOnFail": true } }, "id": "email-monitor", "name": "邮件监控", "type": "n8n-nodes-base.executeCommand", "typeVersion": 1, "position": [ 460, 300 ] }, { "parameters": { "jsCode": "// 解析邮件监控结果\nconst input = $input.first().json;\nconst exitCode = input.exitCode || 0;\nconst stderr = input.stderr || '';\nconst stdout = input.stdout || '';\n\n// 首先检查脚本执行状态\nif (exitCode !== 0) {\n throw new Error(`脚本执行失败 (退出码: ${exitCode})\\n错误信息: ${stderr}\\n输出: ${stdout}`);\n}\n\n// 检查是否有输出\nif (!stdout || stdout.trim() === '') {\n throw new Error('脚本没有返回任何输出');\n}\n\n// 解析 JSON 输出\nlet result;\ntry {\n result = JSON.parse(stdout);\n} catch (parseError) {\n throw new Error(`JSON 解析失败: ${parseError.message}\\n原始输出: ${stdout}`);\n}\n\n// 检查业务逻辑是否成功\nif (!result.success) {\n throw new Error(`邮件监控失败: ${result.error || '未知错误'}`);\n}\n\n// 提取统计信息\nconst stats = result.stats || {};\nconst details = result.details || {};\n\n// 准备输出数据\nreturn {\n json: {\n success: result.success,\n message: result.message,\n stats: stats,\n timestamp: new Date().toISOString(),\n // 如果有重要邮件,准备通知数据\n hasImportantEmails: stats.important_emails > 0,\n importantEmailsCount: stats.important_emails,\n fetchedEmailsCount: stats.fetched_emails,\n notificationsSent: stats.notifications_sent,\n // 详细结果用于调试\n details: details,\n // 添加 webhook URL 供后续节点使用\n webhookUrl: $env.FEISHU_WEBHOOK\n }\n};" }, "id": "parse-results", "name": "解析结果", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 680, 300 ] }, { "parameters": { "conditions": { "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" }, "conditions": [ { "id": "has-important-emails", "leftValue": "={{ $json.hasImportantEmails }}", "rightValue": true, "operator": { "type": "boolean", "operation": "equal" } } ], "combinator": "and" }, "options": {} }, "id": "check-important-emails", "name": "检查重要邮件", "type": "n8n-nodes-base.if", "typeVersion": 2, "position": [ 900, 300 ] }, { "parameters": { "jsCode": "// 准备管理员通知内容\nconst inputData = $input.first().json;\nconst stats = inputData.stats || {};\nconst timestamp = inputData.timestamp;\n\n// 构建飞书卡片消息\nconst message = {\n \"msg_type\": \"interactive\",\n \"card\": {\n \"elements\": [\n {\n \"tag\": \"div\",\n \"text\": {\n \"content\": `**📊 监控统计**\\n\\n` +\n `📥 **获取邮件**: ${stats.fetchedEmailsCount || 0} 封\\n` +\n `⚠️ **重要邮件**: ${stats.importantEmailsCount || 0} 封\\n` +\n `📤 **发送通知**: ${stats.notificationsSent || 0} 条\\n\\n` +\n `🕐 **检查时间**: ${new Date(timestamp).toLocaleString('zh-CN')}\\n\\n` +\n `📈 **运行状态**: ${stats.importantEmailsCount > 0 ? '🔴 发现重要邮件' : '🟢 运行正常'}`,\n \"tag\": \"lark_md\"\n }\n }\n ],\n \"header\": {\n \"title\": {\n \"content\": \"📧 邮件监控报告\",\n \"tag\": \"plain_text\"\n },\n \"template\": stats.importantEmailsCount > 0 ? \"red\" : \"green\"\n }\n }\n};\n\n// 添加 webhook URL 到输出\nreturn {\n json: {\n ...message,\n webhookUrl: inputData.webhookUrl || $env.FEISHU_WEBHOOK\n }\n};" }, "id": "prepare-admin-notification", "name": "准备管理员通知", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1120, 200 ] }, { "parameters": { "url": "={{ $json.webhookUrl || $json.destination?.webhook || $env.FEISHU_WEBHOOK }}", "options": { "headers": { "Content-Type": "application/json" }, "allowUnauthorizedCerts": false }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ $json }}" }, "id": "send-admin-notification", "name": "发送管理员通知", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 1340, 200 ] }, { "parameters": { "jsCode": "// 记录监控日志\nconst result = $input.first().json;\nconst timestamp = new Date().toISOString();\n\n// 构建日志条目\nconst logEntry = {\n timestamp: timestamp,\n success: result.success,\n message: result.message,\n stats: result.stats,\n level: result.success ? 'INFO' : 'ERROR'\n};\n\n// 输出到控制台(n8n 会记录)\nconsole.log(`[${logEntry.level}] ${timestamp}: ${result.message}`);\nif (result.stats) {\n console.log(`Stats: ${JSON.stringify(result.stats)}`);\n}\n\nreturn {\n json: logEntry\n};" }, "id": "log-results", "name": "记录日志", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1120, 400 ] }, { "parameters": { "jsCode": "// 错误处理和通知\nconst input = $input.first();\nconst error = input.error;\nconst json = input.json || {};\nconst timestamp = new Date().toISOString();\n\n// 提取错误信息\nlet errorMessage = '未知错误';\nlet errorDetails = '';\n\nif (error) {\n errorMessage = error.message || error.toString();\n // 如果是脚本执行错误,提取更多信息\n if (error.message && error.message.includes('脚本执行失败')) {\n errorDetails = error.message;\n }\n} else if (json.exitCode && json.exitCode !== 0) {\n errorMessage = `脚本退出码: ${json.exitCode}`;\n errorDetails = `stderr: ${json.stderr || 'N/A'}\\nstdout: ${json.stdout || 'N/A'}`;\n} else if (json.error) {\n errorMessage = json.error;\n}\n\n// 构建飞书错误通知 (使用 msg_type)\nconst errorNotification = {\n \"msg_type\": \"text\",\n \"content\": {\n \"text\": `🚨 邮件监控系统错误\\n\\n` +\n `时间: ${new Date(timestamp).toLocaleString('zh-CN')}\\n` +\n `错误: ${errorMessage}\\n` +\n (errorDetails ? `\\n详细信息:\\n${errorDetails}\\n` : '') +\n `\\n请检查系统状态并及时处理。`\n }\n};\n\n// 记录错误到控制台\nconsole.error('邮件监控系统错误:', {\n timestamp,\n error: errorMessage,\n details: errorDetails,\n input: input\n});\n\nreturn {\n json: errorNotification\n};" }, "id": "handle-error", "name": "错误处理", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 680, 500 ], "onError": "continueRegularOutput" }, { "parameters": { "url": "={{ $env.FEISHU_WEBHOOK }}", "options": { "headers": { "Content-Type": "application/json" }, "allowUnauthorizedCerts": false }, "sendBody": true, "specifyBody": "json", "jsonBody": "={{ $json }}" }, "id": "send-error-notification", "name": "发送错误通知", "type": "n8n-nodes-base.httpRequest", "typeVersion": 4.2, "position": [ 900, 500 ] }, { "parameters": { "jsCode": "// 健康检查和统计\nconst result = $input.first().json;\nconst timestamp = new Date();\n\n// 计算成功率(可以基于历史数据)\nconst healthStatus = {\n timestamp: timestamp.toISOString(),\n status: result.success ? 'healthy' : 'unhealthy',\n uptime: process.uptime(),\n stats: result.stats,\n lastCheck: timestamp.toLocaleString('zh-CN')\n};\n\n// 可以将健康状态写入数据库或文件\n// 这里只是输出到日志\nconsole.log(`Health Check: ${JSON.stringify(healthStatus)}`);\n\nreturn {\n json: healthStatus\n};" }, "id": "health-check", "name": "健康检查", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1340, 400 ] } ], "connections": { "定时触发器": { "main": [ [ { "node": "邮件监控", "type": "main", "index": 0 } ] ] }, "邮件监控": { "main": [ [ { "node": "解析结果", "type": "main", "index": 0 } ] ], "error": [ [ { "node": "错误处理", "type": "main", "index": 0 } ] ] }, "解析结果": { "main": [ [ { "node": "检查重要邮件", "type": "main", "index": 0 } ] ] }, "检查重要邮件": { "main": [ [ { "node": "准备管理员通知", "type": "main", "index": 0 }, { "node": "记录日志", "type": "main", "index": 0 } ], [ { "node": "记录日志", "type": "main", "index": 0 } ] ] }, "准备管理员通知": { "main": [ [ { "node": "发送管理员通知", "type": "main", "index": 0 } ] ] }, "发送管理员通知": { "main": [ [ { "node": "健康检查", "type": "main", "index": 0 } ] ] }, "记录日志": { "main": [ [ { "node": "健康检查", "type": "main", "index": 0 } ] ] }, "错误处理": { "main": [ [ { "node": "发送错误通知", "type": "main", "index": 0 } ] ] } }, "pinData": {}, "settings": { "executionOrder": "v1" }, "staticData": null, "tags": [ { "createdAt": "2024-01-15T10:00:00.000Z", "updatedAt": "2024-01-15T10:00:00.000Z", "id": "email-monitoring", "name": "邮件监控" } ], "triggerCount": 1, "updatedAt": "2024-01-15T10:00:00.000Z", "versionId": "1" }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/leeguooooo/email-mcp-service'

If you have feedback or need assistance with the MCP directory API, please join our Discord server