案例研究 3:一个个人自动化小工具
想法
一个纯粹利己的项目:一个脚本,每天早上抓取当天的日历事件和天气,格式化成一行摘要,发送到一个私人的 Telegram 聊天里。重点是别在喝咖啡之前还要打开三个应用。
Spec
A scheduled job that runs at 7am my timezone. It reads today's
events from my calendar, fetches the forecast for my city, and
sends one Telegram message like: "3 events today, first at 9:30.
High 24C, rain after 4pm." If any source fails, still send what
you have and note what's missing.
最后那句话——优雅地降级而不是崩溃——是那种你必须明确提出来要求的行为。AI 不会自动假定它。不说出来,模型会去抓的默认就是"出错就抛异常",而对一个早上 7 点的 cron 任务来说,那意味着一个静悄悄的早晨,以及对原因毫无头绪。
技术栈
一个单独的、定时触发的 serverless 函数(一个由 Cron 触发的 Worker),这样就没有一台需要保持运行的机器。整个东西就是一个文件加一份调度计划。对一个个人小工具来说,这比听上去更要紧:任何有一台需要照看的服务器的东西,你迟早会忘了续费,于是这个项目死于荒废,而不是死于失败。
关键的 prompt
Write a Cloudflare Worker triggered by cron at 7am Asia/Seoul.
It calls a weather API and a calendar API (I'll provide both
tokens as secrets), builds a one-line summary, and POSTs it to
the Telegram Bot API. Wrap each external call in try/catch so one
failure doesn't kill the message. Read every token from env.
当我们想要打磨措辞,又不想等到早上 7 点时:
Add a manual trigger: if the Worker is hit with a GET request and
a ?test=1 query param, run the same logic immediately and return
the message text in the response instead of sending it. Keep this
behind a secret token so only I can trigger it.
这个测试触发器把我们从一个残酷的反馈循环里救了出来——我们可以随时按需看到输出,而不必每天只看一次。这是个值得偷师的通用招式:每当你的代码按计划运行时,在你认命去等时钟之前,先给自己造一个能现在就按需跑它的法子。
障碍
Telegram 消息到了,但时间是错的:事件显示的是 UTC,而不是韩国时间。天气倒是没问题。我们把它隔离了出来:
Calendar times are 9 hours off — showing UTC, not Asia/Seoul.
The weather time ("rain after 4pm") is correct. Here's how I
format both: [pasted]. Fix only the calendar formatting and tell
me why the weather path was already right.
AI 解释说,天气 API 已经返回了本地化的字符串,而日历 API 返回的是我们直接原样打印出来的 UTC 时间戳。它在日历格式化那一步加上了一次时区转换。把修复范围限定在"只改日历路径",阻止了它去重写那段本来就工作正常的天气代码——这是 AI 的修复弄坏本来好端端的东西的一种常见方式。那个 prompt 的后半句——"tell me why the weather path was already right"——一身二任:它给了我们真正的根因而不是一个猜测,并且逼着 AI 把这个差别讲清楚,好让它的修复不至于把两条路径意外"统一"成一条坏的。
上线
这里的"上线"只不过意味着部署 Worker、设置 cron 调度计划,以及把 API token 作为加密密钥加进去。我们点了几次 ?test=1 端点以确认消息读起来不错,然后就放着不管了。从那以后它每天早上都在运行——而那条优雅降级的话,在一个月后天气 API 出故障时证明了它的价值:消息照样到了,只是少了天气预报而已。