Skip to content

实战案例 ​

本文通过 5 个完整案例,覆盖 OpenClaw 从入门到生产环境的典型应用场景。

案例 1:搭建个人 AI 聊天助手(全流程) ​

目标:在 Linux 服务器上部署 OpenClaw,通过 WebChat 和 Telegram 使用 AI 助手。

第一步:环境准备 ​

bash
# 确认 Node.js 版本(需要 22.22.3+)
node --version  # 应输出 v22.x 或 v24.x

# 安装 OpenClaw
curl -fsSL https://openclaw.ai/install.sh | bash

第二步:初始化配置 ​

bash
# 运行 onboarding 向导
openclaw onboard --install-daemon

# 选择 Provider:DeepSeek
# 输入 API Key:sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 安装为系统服务:Yes

第三步:验证运行 ​

bash
openclaw gateway status
# Runtime: running ✓
# Connectivity probe: ok ✓

# 打开 WebChat
openclaw dashboard
# 浏览器访问 http://127.0.0.1:18789

第四步:接入 Telegram ​

json5
// ~/.openclaw/openclaw.json
{
  channels: {
    telegram: {
      enabled: true,
      botToken: "123456:ABC-DEF1234ghijk",
      dmPolicy: "pairing",
    },
  },
}
bash
openclaw gateway restart

# 在 Telegram 找到你的 Bot → 发送 /start → 配对码自动生成
# 批准配对
openclaw pairing list telegram
openclaw pairing approve telegram <CODE>

第五步:配置 workspace ​

bash
# 创建个人偏好文件
cat > ~/.openclaw/workspace/USER.md << 'EOF'
# USER.md
我是全栈开发者,偏好 TypeScript 和 Python。
叫我「你」即可,不用太正式。
EOF

cat > ~/.openclaw/workspace/AGENTS.md << 'EOF'
# AGENTS.md
- 回答技术问题时给出代码示例
- 不确定时标注"建议验证"
- 用 emoji 让回复更友好
EOF

openclaw gateway restart

从 Telegram 发送「你好,介绍一下你自己」,Agent 会按照你设定的风格回复。

案例 2:接入飞书群聊机器人 ​

目标:在飞书群中添加一个 AI 助手,团队通过 @ 提问。

第一步:创建飞书应用 ​

  1. 访问 飞书开放平台
  2. 创建企业自建应用
  3. 添加权限:im:message、im:message.group_at_msg、im:message.p2p_msg
  4. 发布应用(需管理员审批)

第二步:配置 OpenClaw ​

bash
# 交互式配置
openclaw channels login --channel feishu
# 输入 App ID 和 App Secret

或直接编辑配置:

json5
{
  channels: {
    feishu: {
      enabled: true,
      appId: "cli_xxxxxxxxxxxx",
      appSecret: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
      groupPolicy: "allowlist",
      groupAllowFrom: ["oc_xxxxxxxxxxxx"],  // 你的群 ID
      requireMention: true,                  // 只响应 @机器人的消息
    },
  },
}

第三步:获取群 ID ​

  1. 在飞书打开目标群 → 右上角设置 → 群 ID(格式 oc_xxx)
  2. 填入 groupAllowFrom

第四步:重启并验证 ​

bash
openclaw gateway restart
openclaw channels status --probe --channel feishu

在群里 @ 机器人发送「帮我写一个 Python 读取 CSV 的脚本」,Agent 会自动回复。

案例 3:定时推送日报 ​

目标:每个工作日早上 9:00,Agent 自动汇总 GitHub issues 并通过飞书发送。

第一步:配置 Heartbeat ​

在 workspace 中创建 HEARTBEAT.md:

markdown
<!-- ~/.openclaw/workspace/HEARTBEAT.md -->

# 每日心跳任务

1. 检查今天是工作日还是周末
2. 如果是周末,跳过后续步骤
3. 调用 GitHub API 查询组织的 open issues
4. 按优先级排序,取 Top 10
5. 生成 Markdown 日报
6. 通过飞书消息推送到团队群(chat_id: oc_xxx)

第二步:配置定时触发 ​

json5
// ~/.openclaw/openclaw.json
{
  agents: {
    defaults: {
      heartbeat: {
        every: "1h",               // 每小时检查
        schedule: "0 9 * * 1-5",  // cron:工作日 9:00
        model: "deepseek/deepseek-v4-flash",  // 用便宜模型执行
      },
    },
  },
}

第三步:配置 Cron Jobs(高级) ​

json5
{
  cron: [
    {
      name: "daily-report",
      schedule: "0 9 * * 1-5",
      agent: "default",
      prompt: "根据 HEARTBEAT.md 执行每日报告任务",
    },
    {
      name: "weekly-summary",
      schedule: "0 10 * * 5",
      agent: "default",
      prompt: "生成本周工作总结,汇总 GitHub issues 关闭数和 PR 合并数",
    },
  ],
}
bash
openclaw gateway restart

案例 4:多模型 Fallback 链配置 ​

目标:构建三级模型兜底链条,确保服务永不中断。

json5
{
  agents: {
    defaults: {
      model: {
        primary: "deepseek/deepseek-v4-pro",       // 主模型:最强推理
        fallbacks: [
          "deepseek/deepseek-v4-flash",             // Fallback 1:同厂商快速版
          "openai/gpt-5.4",                         // Fallback 2:跨厂商备份
          "ollama/qwen3:14b",                       // Fallback 3:本地兜底
        ],
      },
      models: {
        "deepseek/deepseek-v4-pro": {
          alias: "DeepSeek Pro",
          params: { temperature: 0.7, maxTokens: 16384 },
        },
        "deepseek/deepseek-v4-flash": {
          alias: "DeepSeek Flash",
          params: { temperature: 0.7, maxTokens: 8192 },
        },
        "openai/gpt-5.4": {
          alias: "GPT-5.4",
          params: { temperature: 0.7, maxTokens: 8192 },
        },
        "ollama/qwen3:14b": {
          alias: "Local Qwen",
          params: { temperature: 0.7, maxTokens: 4096 },
        },
      },
    },
  },
}

验证 Fallback 链 ​

bash
# 检查所有模型连通性
openclaw models status

# 模拟主模型不可用(临时改错 API Key 观察日志)
openclaw logs --follow
# 发送消息后观察是否有 fallback 切换日志

案例 5:用 Skill 扩展 Agent 能力 ​

目标:为 Agent 编写一个「服务器监控」Skill,支持 CPU/内存/磁盘查询。

创建 Skill ​

bash
mkdir -p ~/.openclaw/workspace/skills/server-monitor
yaml
# ~/.openclaw/workspace/skills/server-monitor/SKILL.md
---
name: server-monitor
description: 服务器资源监控(CPU、内存、磁盘、进程)
version: 1.0.0
---
markdown
# Server Monitor Skill

## 触发条件
- 用户询问服务器状态:CPU/内存/磁盘/进程
- 关键词:「服务器状态」「负载」「磁盘空间」「内存使用」

## 执行流程

### CPU 查询
```bash
top -bn1 | grep "Cpu(s)" | awk '{print "CPU 使用率: " 100-$8 "%"}'

内存查询 ​

bash
free -h | awk '/^Mem:/ {print "总内存: " $2 ", 已用: " $3 ", 可用: " $7}'

磁盘查询 ​

bash
df -h / | awk 'NR==2 {print "磁盘: 总 " $2 ", 已用 " $3 " (" $5 "), 可用 " $4}'

进程 Top 5 ​

bash
ps aux --sort=-%cpu | head -6 | tail -5 | awk '{printf "%-20s CPU: %s%% MEM: %s%%\n", $11, $3, $4}'

输出模板 ​

📊 服务器状态报告

💻 CPU:{cpu_usage}
🧠 内存:{mem_info}
💾 磁盘:{disk_info}

🔝 CPU 占用 Top 5:
{process_list}

🕐 检查时间:{timestamp}

安全提醒 ​

  • 仅执行只读命令(top, free, df, ps)
  • 不执行 kill、reboot、shutdown 等危险操作

```bash
openclaw gateway restart

然后对 Agent 说「检查服务器状态」,Agent 会加载此 Skill 并按指引执行监控命令。

经验:生产环境最佳实践 ​

1. Systemd 服务管理 ​

ini
# /etc/systemd/system/openclaw-gateway.service
[Unit]
Description=OpenClaw Gateway
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=openclaw
ExecStart=/usr/local/bin/openclaw gateway
Restart=always
RestartSec=10
Environment=NODE_ENV=production
EnvironmentFile=/home/openclaw/.openclaw/.env

# 安全加固
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=/home/openclaw/.openclaw

[Install]
WantedBy=multi-user.target
bash
sudo systemctl daemon-reload
sudo systemctl enable openclaw-gateway
sudo systemctl start openclaw-gateway

2. 日志轮转 ​

bash
# /etc/logrotate.d/openclaw
/home/openclaw/.openclaw/logs/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    copytruncate
    maxsize 100M
}

3. 备份策略 ​

bash
#!/bin/bash
# /home/openclaw/backup.sh
# 每日备份 workspace 到私有 Git 仓库

cd ~/.openclaw/workspace
git add -A
git commit -m "Auto backup $(date +%Y-%m-%d)"
git push origin main

# 备份配置文件(加密后上传)
tar czf /tmp/openclaw-config-backup.tar.gz ~/.openclaw/openclaw.json ~/.openclaw/.env
gpg --encrypt --recipient your-key /tmp/openclaw-config-backup.tar.gz
# 将加密文件保存到安全位置
bash
# Crontab
0 2 * * * /home/openclaw/backup.sh

4. 监控和告警 ​

bash
# 创建健康检查脚本
cat > /usr/local/bin/openclaw-healthcheck.sh << 'SCRIPT'
#!/bin/bash
STATUS=$(openclaw gateway status 2>&1)
if echo "$STATUS" | grep -q "Runtime: running"; then
    exit 0
else
    echo "OpenClaw Gateway is down!" | \
        mail -s "OpenClaw Alert" admin@example.com
    exit 1
fi
SCRIPT

chmod +x /usr/local/bin/openclaw-healthcheck.sh
bash
# 每 5 分钟检查一次
*/5 * * * * /usr/local/bin/openclaw-healthcheck.sh

5. 安全清单 ​

markdown
## 生产环境安全检查清单

- [ ] `dmPolicy` 不为 `"open"`(除非有 allowFrom 限制)
- [ ] `requireMention: true` 对群聊开启(避免误触发)
- [ ] API Key 存放在 `.env` 文件,不在 `openclaw.json` 中
- [ ] `.env` 文件权限为 600(`chmod 600 ~/.openclaw/.env`)
- [ ] Gateway 默认绑定 127.0.0.1(不对外暴露端口)
- [ ] 远程访问通过 SSH Tunnel 或 Tailscale(不直接暴露公网)
- [ ] `exec` 工具配置了 `exec.allowlist` 或沙箱限制
- [ ] workspace 有定期备份
- [ ] 日志被定期轮转,不占满磁盘
- [ ] 系统运行专用用户(`openclaw`),不用 root

总结 ​

通过这 5 个案例,你已经覆盖了 OpenClaw 的核心使用场景:

案例涉及知识
个人聊天助手Gateway 安装、Provider 配置、Telegram 通道、workspace
飞书群聊机器人飞书应用创建、通道配置、群聊策略
定时推送日报HEARTBEAT.md、Cron Jobs、自动化
多模型 Fallback模型链配置、跨 Provider 备份、本地兜底
Skill 扩展SKILL.md 编写、Skill 目录结构、工具调用
生产环境最佳实践systemd、日志轮转、备份、监控、安全加固

建议按照案例 1 → 2 → 5 的顺序动手实操,然后根据实际需求引入案例 3 和 4。


🎉 恭喜完成 OpenClaw 全部模块学习!

加载练习题中...

🎯 本章要点 ​

  • 典型配置用主模型+多个备选链,故障时自动切换确保服务不中断