每周自动预约深度工作时段:Google Calendar + OpenAI + Slack
输入下周日历事件 → 计算工作日空闲间隙 → 自动预约 Focus Time 并生成 Slack 深度工作计划
- 给谁
- 需要在日程中保护深度工作时间的知识工作者
- 你会得到
- 每周日自动扫描下周日历,预约 2 小时以上空闲时段为 Focus Time,并通过 Slack 收到个性化的深度工作计划或碎片警告
硬约束
需要 n8n 实例;Google Calendar(读写两个日历或同一日历)、OpenAI(Chat Model)、Slack 三类凭证。在 n8n 中导入模板后,调整 ⚙️ My Preferences 节点的工作时间、最小时段长度、每周最大预约数,并在两个 Slack 节点填入目标用户名。激活工作流后每周日 18:00 自动执行。
他们做了什么
作者 kii 在 n8n 模板库发布了一个工作流模板。该模板每周日 18:00 自动执行:读取用户 Google Calendar 的下周日历事件,通过 JavaScript 代码节点计算工作日内满足最小时长的空闲间隙,经条件分支后要么预约 Focus Time 日历事件并通过 OpenAI gpt-4o-mini 生成个性化深度工作计划发送至 Slack,要么发送日历碎片警告。模板包含完整的节点参数、连线与提示词。
效果如何
来源报告该模板可帮助用户在会议侵占前主动预约深度工作时间,并收到个性化的 Slack 计划或碎片警告(author_report)。模板在 n8n 平台显示为已发布状态。
值得知道
1) 本站未实测该工作流(reproduction=not_tested),搭建时需自行验证凭证与日历权限。2) 代码中每个空闲时段硬上限为 3 小时(cursor.getTime() + 3 * 36e5),即使原始间隙更长也会被截断。3) 'Get Next Week's Events' 与 'Book Focus Block' 的日历选择器在模板中为空字符串,导入后必须手动选择有效日历。4) Slack 节点的 username 值在模板中为 'your-slack-handle',必须替换为真实用户名否则消息发送失败。5) 工作日判定依赖 JavaScript Date.getDay(),时区行为与 n8n 实例配置相关,跨时区用户需额外验证。6) 模板未提供费用信息;OpenAI 调用按实际 token 计费。
n8n 工作流模板
结构已检查 · 未实际运行下载后在 n8n 选择“Import from File”,或复制 JSON 后粘贴到编辑器。先配置自己的账号,再用测试数据运行;模板默认未启用。
配置与测试清单
需要绑定的凭证
- 导入后按各需鉴权节点绑定你自己的凭证;本附件不含原作者密钥。
需要替换的配置
- 源模板来自 n8n 公开库 #17074(约 16 个节点)。导入后先保持未启用,核对触发器、表达式与外部 API 端点。
- Sticky Note 中的说明仅供参考;按自己的账号、表结构或频道 ID 替换占位配置。
测试输入与预期结果
- 在测试环境手动执行一次主路径,确认每个需凭证的节点已连接且无鉴权错误。
- 用一条合成/脱敏样例数据跑通,核对输出字段与下游写入目标,再考虑生产启用。
模板处理说明
- 已移除原作者凭证绑定、webhookId 与执行数据,工作流设为未启用;节点、连线与原文提示词保持原样。未在 n8n 中实际运行。
- 未改业务逻辑;运行前请自行核对密钥、配额与隐私合规。
模板获取日期: 2026-10-25
步骤
配置触发与偏好
在 n8n 中导入模板。检查 'Every Sunday 18:00' 触发节点(Schedule Trigger),确认规则为每周日 18:00 执行。打开 '⚙️ My Preferences' 节点,按个人日程修改以下字段:workStartHour(工作开始小时,默认 9)、workEndHour(工作结束小时,默认 18)、minBlockHours(最小时段长度,默认 2)、maxBlocksPerWeek(每周最大预约数,默认 5)。
连接 Google Calendar 并读取下周事件
添加 Google Calendar 凭证。在 'Get Next Week's Events' 节点选择要读取的日历(mode: list),operation 设为 getAll,returnAll 开启。该节点会自动拉取下周全部事件,供后续计算空闲间隙。
运行代码计算工作日空闲间隙
'Find Free Gaps' 节点执行 JavaScript 代码,逻辑如下:读取 ⚙️ My Preferences 的偏好值;过滤出状态非取消且含 start.dateTime 的事件;仅扫描下周一到周五(跳过周末);在每个工作日的 workStartHour 至 workEndHour 之间,计算事件之间的空闲时长;保留大于等于 minBlockHours 的间隙,每个间隙上限 3 小时;输出包含 found、start、end、durationHours、weekday 的对象数组。若无满足条件的间隙,返回 {found: false}。
运行提示词 · 来源原文。'Find Free Gaps' 节点的 Code 节点参数字段
// Find free gaps of at least `minBlockHours` inside working hours, Monday–Friday const prefs = $('⚙️ My Preferences').first().json; const events = $input.all() .map(i => i.json) .filter(e => e.start?.dateTime && e.status !== 'cancelled') .map(e => ({ start: new Date(e.start.dateTime), end: new Date(e.end.dateTime) })) .sort((a, b) => a.start - b.start); const gaps = []; const now = new Date(); for (let d = 1; d <= 6; d++) { const day = new Date(now); day.setDate(now.getDate() + d); const dow = day.getDay(); if (dow === 0 || dow === 6) continue; // weekdays only const dayStart = new Date(day); dayStart.setHours(prefs.workStartHour, 0, 0, 0); const dayEnd = new Date(day); dayEnd.setHours(prefs.workEndHour, 0, 0, 0); const dayEvents = events.filter(e => e.start < dayEnd && e.end > dayStart); let cursor = dayStart; for (const e of [...dayEvents, { start: dayEnd, end: dayEnd }]) { const gapHours = (e.start - cursor) / 36e5; if (gapHours >= prefs.minBlockHours) { gaps.push({ found: true, start: cursor.toISOString(), end: new Date(Math.min(e.start, cursor.getTime() + 3 * 36e5)).toISOString(), durationHours: Number(Math.min(gapHours, 3).toFixed(1)), weekday: day.toLocaleDateString('en-US', { weekday: 'long' }) }); } if (e.end > cursor) cursor = e.end; } } if (gaps.length === 0) { return [{ json: { found: false } }]; } return gaps.map(g => ({ json: g }));条件分支:判断是否存在可用时段
'Any Focus Slots?' 节点(If 节点)检查 $json.found 是否为 true。若存在可用间隙,流程进入 'Cap Blocks Per Week';若无,流程走向 'Warn: Week Too Fragmented' 发送碎片警告。
限制每周预约数量并写入日历
'Cap Blocks Per Week' 节点(Limit 节点)将间隙数量限制为 maxBlocksPerWeek。随后 'Book Focus Block' 节点(Google Calendar)为每个通过的间隙创建事件:summary 设为 '🧘 Focus Time (protected by n8n)',color 设为 9,description 提示自动预约并建议拒绝冲突会议。start 与 end 取自间隙的 ISO 时间。需选择写入的目标日历。
聚合已预约时段并生成深度工作计划
'Bundle Booked Blocks' 节点(Aggregate)将所有预约数据聚合到 blocks 字段。'Write My Focus Plan' 节点(Basic LLM Chain)调用 OpenAI Chat Model(gpt-4o-mini),system message 为 'You are a supportive productivity coach who specializes in deep work.',用户提示词包含已预约时段的 JSON 数据,并要求生成不超过 180 词的 Slack 消息:列出各时段(星期几+时间范围)、建议适合的工作类型(长时段→深度创意工作,较短→审阅/写作)、以及一条防御会议侵占的实用建议。要求使用 Slack 格式并保持个人化。
运行提示词 · 来源原文。'Write My Focus Plan' 节点的 messages.messageValues[0].message(system message)
You are a supportive productivity coach who specializes in deep work.
运行提示词 · 来源原文。'Write My Focus Plan' 节点的 text 参数(用户提示词)
=I just protected these deep-work blocks in my calendar for next week: {{ JSON.stringify($('Cap Blocks Per Week').all().map(i => i.json), null, 2) }} Write me a short, motivating Slack message (max 180 words): 1. List each block (weekday + time range, human-readable) 2. Suggest what type of work fits each slot (long blocks → hard creative work, shorter → reviews/writing) 3. One practical tip to actually defend these blocks from meeting requests Use Slack formatting and keep it personal — this goes to one person, not a team.发送 Slack 深度工作计划或碎片警告
若存在可用时段,'DM My Focus Plan' 节点向指定 Slack 用户发送消息,格式为 '🧘 *Your deep-work plan for next week*\n\n{{ $json.text }}',其中 $json.text 为 LLM 生成的内容。若不存在可用时段,'Warn: Week Too Fragmented' 节点发送警告:'⚠️ *Focus Time Defender*: I couldn't find a single free 2-hour slot in your working hours next week. Your calendar is fully fragmented — consider declining or shortening some meetings, then re-run me.' 两个 Slack 节点的 user.mode 均为 username,value 需替换为实际 Slack 用户名。
运行提示词 · 来源原文。'DM My Focus Plan' 节点的 text 参数
=🧘 *Your deep-work plan for next week* {{ $json.text }}运行提示词 · 来源原文。'Warn: Week Too Fragmented' 节点的 text 参数
⚠️ *Focus Time Defender*: I couldn't find a single free 2-hour slot in your working hours next week. Your calendar is fully fragmented — consider declining or shortening some meetings, then re-run me.
可复制提示词
搭建指导 · 本站建议版本。搭建助手启动词:确认 n8n 实例、三类凭证权限、偏好值与 Slack 用户名
这一段是提示词,页面不会在这里插入广告。
在 n8n 中导入模板 https://n8n.io/workflows/17074/,按上述步骤配置凭证、偏好值与 Slack 用户名后激活。如需调整 LLM 生成风格,可修改 'Write My Focus Plan' 节点的 system message 与用户提示词。
导入模板后先配置凭证和 ⚙️ My Preferences,再激活工作流。首次运行建议手动触发观察日志。
来源
Schedule weekly focus-time blocks with Google Calendar, OpenAI, and Slack · kii · n8n.io · 2026-07-14
留言与反馈
Agent 反馈
以下反馈由 Agent 提交,经用户确认和审核后公开,不代表平台已验证。