首页 教程 分类 Skills下载 关于
ZH EN JA KO
高级用法

OpenClaw定时消息与自动化工作流

· 11 分钟

前言

AI Agent 不应该只被动等待用户发消息——它可以主动出击。OpenClaw 内置了定时任务系统,支持基于 Cron 表达式的调度,让 AI Agent 能够在指定时间自动执行任务、发送消息或触发工作流。从每日天气播报到定期数据汇总,定时消息功能极大地扩展了 Agent 的应用场景。

本文将详细介绍定时消息的配置方法和自动化工作流的构建技巧。

基本定时消息

配置定时任务

openclaw.json5 中为 Agent 添加定时任务:

{
  agents: {
    "my-agent": {
      schedules: [
        {
          // 任务名称
          name: "morning-greeting",
          // Cron 表达式(分 时 日 月 周)
          cron: "0 8 * * *",  // 每天早上8点
          // 时区
          timezone: "Asia/Shanghai",
          // 发送到哪个频道
          target: {
            platform: "telegram",
            chatId: "123456789"
          },
          // 要发送给AI的消息(AI会基于此生成回复)
          prompt: "请生成一段早间问候,包含今天的日期和一句励志格言。"
        }
      ]
    }
  }
}

Cron 表达式速查

表达式 含义
0 8 * * * 每天 08:00
0 8 * * 1-5 工作日 08:00
*/30 * * * * 每 30 分钟
0 9,18 * * * 每天 09:00 和 18:00
0 0 1 * * 每月 1 号 00:00
0 0 * * 0 每周日 00:00
0 */4 * * * 每 4 小时

常见自动化场景

场景一:每日天气播报

{
  schedules: [{
    name: "weather-report",
    cron: "0 7 * * *",
    timezone: "Asia/Shanghai",
    target: {
      platform: "telegram",
      chatId: "group_weather_123"
    },
    prompt: "请使用天气工具查询北京、上海、广州的今日天气,并生成天气播报。",
    // 指定使用哪些工具
    tools: ["weather"],
    // 回复格式
    format: "markdown"
  }]
}

场景二:工作日站会提醒

{
  schedules: [{
    name: "standup-reminder",
    cron: "0 9 * * 1-5",
    timezone: "Asia/Shanghai",
    target: {
      platform: "slack",
      channelId: "#dev-team"
    },
    // 静态消息(不经过AI处理)
    staticMessage: "早上好,团队!站会时间到了。请回复你的:\n1. 昨天完成了什么\n2. 今天计划做什么\n3. 有什么阻塞问题",
    // 提及所有人
    mentionAll: true
  }]
}

场景三:定期数据汇总

{
  schedules: [{
    name: "weekly-summary",
    cron: "0 17 * * 5",  // 每周五下午5点
    timezone: "Asia/Shanghai",
    target: {
      platform: "discord",
      channelId: "analytics-channel"
    },
    prompt: "请查询本周的用户数据,生成一份周报汇总,包含新增用户数、活跃用户数、关键指标变化。",
    tools: ["sqlite", "http-fetch"]
  }]
}

场景四:RSS 内容推送

{
  schedules: [{
    name: "rss-digest",
    cron: "0 12 * * *",  // 每天中午
    target: {
      platform: "telegram",
      chatId: "tech_news_123"
    },
    prompt: "请获取以下RSS源的最新10篇文章,筛选出与AI相关的内容,生成中文摘要推送。",
    tools: ["rss", "http-fetch"],
    // 传递给prompt的参数
    params: {
      feeds: [
        "https://news.ycombinator.com/rss",
        "https://techcrunch.com/feed/"
      ]
    }
  }]
}

工作流链式执行

OpenClaw 支持将多个定时任务串联成工作流,前一个任务的结果可以传递给下一个任务。

配置工作流

{
  agents: {
    "workflow-agent": {
      workflows: {
        "daily-ops": {
          // 工作流触发时间
          cron: "0 9 * * 1-5",
          timezone: "Asia/Shanghai",
          // 步骤链
          steps: [
            {
              name: "fetch-data",
              prompt: "从数据库查询昨日运营数据",
              tools: ["sqlite"],
              // 输出保存为变量
              outputVar: "dailyData"
            },
            {
              name: "analyze",
              prompt: "分析以下运营数据,找出异常指标:{{dailyData}}",
              outputVar: "analysis"
            },
            {
              name: "report",
              prompt: "基于以下分析结果生成运营日报:{{analysis}}",
              // 发送到目标频道
              target: {
                platform: "slack",
                channelId: "#ops-daily"
              }
            }
          ],
          // 某个步骤失败时的处理
          onError: {
            notify: {
              platform: "telegram",
              chatId: "admin_123",
              message: "工作流 daily-ops 执行失败:{{error}}"
            }
          }
        }
      }
    }
  }
}

条件分支

工作流步骤支持条件判断:

{
  steps: [
    {
      name: "check-metrics",
      prompt: "检查系统关键指标是否正常",
      outputVar: "status"
    },
    {
      name: "alert",
      // 仅当上一步输出包含"异常"时执行
      condition: "{{status}} contains '异常'",
      prompt: "生成异常告警报告:{{status}}",
      target: {
        platform: "telegram",
        chatId: "oncall_group"
      }
    },
    {
      name: "normal-log",
      condition: "{{status}} not contains '异常'",
      staticMessage: "系统巡检正常。",
      target: {
        platform: "slack",
        channelId: "#monitoring"
      }
    }
  ]
}

任务管理

命令行管理

# 查看所有定时任务
openclaw schedules list

# 手动触发某个任务(不等待Cron时间)
openclaw schedules run morning-greeting

# 暂停某个任务
openclaw schedules pause weekly-summary

# 恢复某个任务
openclaw schedules resume weekly-summary

# 查看任务执行历史
openclaw schedules history morning-greeting --last 10

Dashboard 管理

OpenClaw Web Dashboard 提供了可视化的任务管理界面:

  • 查看所有定时任务的状态和下次执行时间
  • 手动触发任务执行
  • 查看执行日志和输出
  • 暂停/恢复/编辑任务

错误处理与重试

{
  schedules: [{
    name: "critical-report",
    cron: "0 9 * * *",
    // 重试配置
    retry: {
      maxAttempts: 3,
      backoffMs: 5000,  // 重试间隔
      // 所有重试失败后的通知
      onFailure: {
        platform: "telegram",
        chatId: "admin_123",
        message: "定时任务 critical-report 连续3次执行失败"
      }
    },
    // 超时设置(秒)
    timeout: 120,
    // ...其他配置
  }]
}

与外部 Cron 集成

如果你已经有现成的 Cron 系统(如 Linux crontab、Kubernetes CronJob),也可以通过 API 触发 OpenClaw 任务:

# 在系统crontab中配置
0 8 * * * curl -X POST http://localhost:3000/api/v1/schedules/morning-greeting/run \
  -H "Authorization: Bearer sk-openclaw-xxx"

这种方式的好处是可以利用现有的 Cron 基础设施,同时享受 OpenClaw 的任务编排能力。

最佳实践

  1. 时区明确:始终显式指定 timezone,避免服务器时区导致的混乱
  2. 错误通知:为重要任务配置失败通知,避免静默失败
  3. 执行窗口:避免在系统高峰期安排大量定时任务
  4. 幂等设计:任务应该设计为可重复执行的,防止因重试导致重复操作
  5. 日志审查:定期查看任务执行历史,确认任务正常运行

小结

OpenClaw 的定时消息和自动化工作流功能将 AI Agent 从被动响应升级为主动服务。通过 Cron 调度、工作流链式执行、条件分支和错误处理,你可以构建出完整的自动化流程——从简单的定时提醒到复杂的多步骤数据处理工作流,OpenClaw 都能胜任。

OpenClaw 是开源免费的个人AI助手,支持 WhatsApp、Telegram、Discord 等多平台接入