OpenClaw 新手指南:AI 机器人搭建实战
为什么选择 OpenClaw?
OpenClaw 是一个开源的自托管 AI 网关,让你可以在自己服务器上运行一个 central hub,连接所有聊天平台到强大的 AI 模型(如 Claude、GPT、Pi、Kimi 等)。
核心优势:
- 数据完全掌控(自托管,隐私安全)
- 多平台统一管理(一个网关服务所有渠道)
OpenClaw 是一款开源自托管 AI 网关,支持连接多种聊天平台与 AI 模型。介绍其核心优势、安装配置、多渠道管理、自动化任务(Cron)、长期记忆、脚本集成及浏览器自动化等功能。包含 20 个实战脚本示例,涵盖监控、备份、日志分析等场景,并提供常见问题解答与故障排除手册,帮助用户快速搭建并维护 AI 机器人服务。
OpenClaw 是一个开源的自托管 AI 网关,让你可以在自己服务器上运行一个 central hub,连接所有聊天平台到强大的 AI 模型(如 Claude、GPT、Pi、Kimi 等)。
核心优势:
适用场景: 第一次使用,5 分钟内启动
# 1. 安装 Node.js 22+
nvm install 22
nvm use 22
# 2. 快速安装
npm install -g openclaw
# 3. 启动向导(首次运行)
openclaw onboard
向导会问:
5 分钟后,你的第一个 AI 机器人就上线了。
适用场景: 同时运营多个社群
配置示例 ~/.openclaw/openclaw.json:
{
"channels": {
"telegram": {
"accounts": {
"default": {"token": "YOUR_TELEGRAM_BOT_TOKEN"},
"alerts": {"token": "YOUR_ALERT_BOT_TOKEN", "name": "Alert Bot"}
}
},
"discord": {
"accounts": {
"work": {"token": "YOUR_DISCORD_BOT_TOKEN"}
}
},
"feishu": {
"accounts": {
"default": {
"app_id": "cli_xxxxx",
"app_secret": "xxxxx",
"encrypt_key": "xxxxx",
"verification_token": "xxxxx"
}
}
}
}
}
实用技巧:
channels list 命令查看所有已配置渠道适用场景: 定时提醒、自动生成报告
# 1. 添加早晨提醒任务
openclaw cron add --name "晨间提醒" --cron "0 9 * * *" --system-event "早安!今天有哪些重要任务?"
# 2. 添加每周总结任务
openclaw cron add --name "每周总结" --cron "0 17 * * 5" --agent-turn "汇总本周任务完成情况,生成报告"
高级选项:
--at 1735689600:绝对时间(Unix 时间戳)--every 1h:每小时执行一次--deliver:通知到指定渠道适用场景: AI 记住用户偏好、项目信息
记忆文件结构:
~/.openclaw/workspace/
├── MEMORY.md # 长期记忆
└── memory/
├── 2026-02-17.md # 每日日志
└── 2026-02-18.md
MEMORY.md 示例:
# 用户信息
**姓名:** 小纯
**时区:** GMT+8
**联系方式:** 飞书
### 偏好
- 喜欢详细的说明和可视化例子
- 注重美学和流畅的动画
- 偏好科技/科幻风格
使用方法:
# 搜索记忆
openclaw memory search "项目进度"
# 重建索引
openclaw memory index
价值: AI 能根据记忆了解用户,提供更个性化的回复。
适用场景: 文件操作、数据抓取自动处理
# 在对话中直接命令 AI:
# 你:帮我监控这个网站的价格变化,如果低于 100 元通知我
# AI:我来设置监控脚本...
# AI 会创建这样的脚本:
# ~workspace/price-monitor.sh
cd ~workspace && cat > price-monitor.sh <<'EOF'
#!/bin/bash
PRICE=$(curl -s https://example.com/product | grep -oP 'price: \K[0-9]+')
if [ $PRICE -lt 100 ]; then
openclaw message send --channel feishu --message "价格已降至 $PRICE!"
fi
EOF
chmod +x price-monitor.sh
# 然后添加到 Cron
openclaw cron add --name "价格监控" --cron "0 */6 * * *" --agent-turn "运行价格监控脚本"
适用场景: 智能家居、车载系统
当前支持:
配置示例:
{
"voice": {
"enabled": true,
"provider": "elevenlabs",
"apiKey": "YOUR_API_KEY",
"voiceId": "your-voice-id"
}
}
唤醒词设置: 在对话中 AI 会等待特定短语:
适用场景: 自动化网页操作、数据抓取
# 启动浏览器(命令行或对话中)
openclaw browser start
# 在对话中:
# 你:帮我打开 GitHub,搜索 OpenClaw,统计 Star 数
# AI:正在操作浏览器... (自动点击、搜索、读取数据)
# 你:GitHub 上 OpenClaw 的 Star 数是 8,732
场景举例:
适用场景: 集成企业内部系统
插件结构:
~workspace/skills/my-plugin/
├── SKILL.md
├── index.ts
└── package.json
SKILL.md 示例:
---
name: "my-company-integration"
description: "公司内部系统集成"
---
# 使用场景
- 自动化 HR 工作流
- 工单系统自动化
# 如何使用
直接在对话中:帮我创建一个 Jira 工单
插件自动激活: 当对话匹配描述时,AI 会自动启用插件功能和对应工具(可调用外部 API 等)。
适用场景: 24/7 部署,确保服务稳定
# 检查网关状态
openclaw status
# 检查渠道健康
openclaw channels status --probe
# 检查日志
openclaw logs --follow
关键指标: ✓ 网关是否运行 ✓ 渠道是否连接 ✓ Token 余额 ✓ 内存/CPU 使用
自动化监控:
openclaw cron add --name "健康检查" --cron "0 */4 * * *" --agent-turn "检查服务健康状态,若有异常通知我"
适用场景: 企业部署,多人共用
{
"channels": {
"discord": {
"accounts": {
"default": {
"token": "YOUR_TOKEN",
"allowFrom": ["user:123", "user:456"],
"groups": {
"work-team": {"allowFrom": ["role:admin"]}
}
}
}
}
}
}
权限类型:
1. 快速配置备份
#!/bin/bash
DATE=$(date +%Y%m%d)
cp ~/.openclaw/openclaw.json ~/.openclaw/openclaw.json.backup.$DATE
echo "配置已备份到 openclaw.json.backup.$DATE"
2. 日志清理
#!/bin/bash
# 清理 7 天前的日志
find ~/.openclaw/logs -name "*.log" -mtime +7 -delete
echo "已清理 7 天前的日志"
3. 快速重启
#!/bin/bash
openclaw gateway stop
sleep 3
openclaw gateway start
echo "网关已重启"
4. 网站价格监控
#!/bin/bash
check_product_price(){
URL=$1
THRESHOLD=$2
PRODUCT_NAME=$3
PRICE=$(curl -s $URL | grep -oP 'price: \K[0-9.]+')
if (( $(awk "BEGIN {print $PRICE <= $THRESHOLD}")); then
openclaw message send --channel feishu --target "@your_alert_group" --message "$PRODUCT_NAME 价格已降至 $PRICE"
fi
}
# 使用:$0 "https://example.com/product" 100 "iPhone 15"
5. 天气监控
#!/bin/bash
CITY=$1
WEATHER=$(curl -s "https://getweather.com/$CITY" | jq '.weather')
openclaw message send --channel feishu --message "$CITY 今日天气:$WEATHER"
6. 股票价格监控
#!/bin/bash
SYMBOL=$1
PRICE=$(curl -s "https://api.stocks.com/$SYMBOL" | jq '.price')
openclaw message send --channel feishu --message "$SYMBOL 的最新价格是 $PRICE"
7. 自动发送日报(邮件或飞书)
#!/bin/bash
TODAY=$(date +%Y-%m-%d)
# 从数据库或文件读取工作记录
REPORT=$(cat ~workspace/tasks.log | grep $TODAY)
openclaw message send --channel feishu --message "📊 今日工作报告\n$REPORT"
8. 服务器资源监控
#!/bin/bash
CPU=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
MEM=$(free | grep Mem | awk '{printf "%.1f%%", $3/$2 * 100.0}')
DISK=$(df -h | awk '$NF=="/{printf "%s", $5}')
if (( $(awk "BEGIN {print $CPU > 80}")); then
openclaw message send --channel feishu --message "⚠️ CPU 使用率过高:$CPU%"
fi
if [[ $DISK == *9?%* ]]; then
openclaw message send --channel feishu --message "⚠️ 磁盘使用率过高:$DISK"
fi
9. 备份脚本(完整备份)
#!/bin/bash
BACKUP_DIR=~/.openclaw/backups
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR
# 备份配置、数据、日志
cp -r ~/.openclaw/openclaw.json $BACKUP_DIR/
cp -r ~/.openclaw/workspace $BACKUP_DIR/workspace_$DATE
# 删除 30 天前的备份
find $BACKUP_DIR -mtime +30 -delete
10. 数据库备份(如果使用数据库)
#!/bin/bash
BACKUP_FILE=~workspace/db_backup_$(date +%Y%m%d).sql
if [ -f "~workspace/database.db" ]; then
sqlite3 ~workspace/database.db .dump > $BACKUP_FILE
fi
11. 网站连通性检查
#!/bin/bash
URL=$1
STATUS=$(curl -s -o /dev/null -w "%{http_code}" $URL)
if [ $STATUS -ne 200 ]; then
openclaw message send --channel feishu --message "❌ 网站异常:$URL (状态码:$STATUS)"
fi
12. 监控 HTTPS 证书过期
#!/bin/bash
DOMAIN=$1
EXPIRY=$(echo | openssl s_client -servername $DOMAIN -connect $DOMAIN:443 2>/dev/null | openssl x509 -noout -dates | grep notAfter | cut -d= -f2)
DAYS=$(echo $(($(date -d "$EXPIRY" +%s) - $(date +%s))) / 86400)
if [ $DAYS -lt 30 ]; then
openclaw message send --channel feishu --message "⚠️ $DOMAIN 证书将在 $DAYS 天后过期!"
fi
13. 扫描指定 IP 端口
#!/bin/bash
IP=$1
PORT=$2
if nc -z -w5 $IP $PORT; then
openclaw message send --channel feishu --message "$IP:$PORT 端口正常"
else
openclaw message send --channel feishu --message "❌ $IP:$PORT 端口不可达"
fi
14. 文件去重
#!/bin/bash
DIR=$1
fdupes -r $DIR | while read -r file; do
if [ "$file" != "$PREV" ]; then
PREV=$file
else
rm "$file"
echo "已删除重复文件:$file"
fi
done
15. 定时清理过期文件
#!/bin/bash
DIR=$1
DAYS=$2
find $DIR -type f -mtime +$DAYS -delete
echo "已删除 $DIR 中 $DAYS 天前的文件"
16. 生成代码摘要
#!/bin/bash
FILE=$1
# 使用 AI 分析代码(需要配置)
openclaw agent --message "请分析这个文件:$(cat $FILE)" --to feishu
17. 自动总结日志
#!/bin/bash
LOG_FILE=$1
openclaw agent --message "总结这个日志文件的核心错误:$(tail -100 $LOG_FILE)" --to feishu
18. 自动翻译文本
#!/bin/bash
TEXT=$1
openclaw agent --message "翻译成英文:$TEXT"
19. 生成测试数据
#!/bin/bash
TYPE=$1
openclaw agent --message "生成 $TYPE 的测试数据(10 个样本)"
20. 代码审查
#!/bin/bash
FILE=$1
openclaw agent --message "审查这段代码的安全性:$(cat $FILE)"
Q1:OpenClaw 支持哪些平台? A:支持:
Q2:最低系统要求是什么? A:
Q3:是否需要显卡? A:不需要,OpenClaw 主要是路由和管理工作流,不进行本地模型推理。
Q4:OpenClaw 是否支持多模型? A:支持!可同时使用多个 AI 模型,并设置优先级和回退。配置示例如下:
{
"agents": {
"defaults": {
"model": {
"primary": "anthropic/claude-sonnet-4-6",
"fallbacks": ["kimi-code", "deepseek-chat"]
}
}
}
}
Q5:如何获取 API Key? A:
Q6:如何让 AI 用专业口吻回复?
A:在 MEMORY.md 中设置:
# 系统提示
请用专业、技术性强的语气回复,使用 emoji 和结构化格式,避免无关的客套话。
Q7:如何禁用某个渠道?
A:在 openclaw.json 中:
{"channels":{"telegram":{"enabled":false}}}
或在 CLI 中执行:
openclaw channels remove --channel telegram --delete
Q8:如何让 AI 只在特定时间回复? A:使用 Cron 设置:
openclaw cron add --name "工作时间" --cron "* 9-18 * * 1-5" --system-event "已进入工作时间"
openclaw cron add --name "下班时间" --cron "0 0 20 * * 1-5" --system-event "已下班,工作消息转入离线模式"
Q9:如何防止 AI 泄露敏感信息? A:
Q10:如何让 AI 记住对话? A:OpenClaw 默认会在会话内记住对话,但跨会话持久记忆需手动构建和写入到记忆文件中。可以:
MEMORY.md 或 memory/YYYY-MM-DD.mdopenclaw memory index 重建索引,之后检索生效Q11:启动失败,提示端口被占用怎么办? A:检查并终止占用进程或更换端口:
# 检查哪个进程占用 18789 端口
lsof -i :18789
# 终止进程
kill -9 <PID>
# 或更换端口启动
openclaw gateway --port 18790
Q12:渠道连接失败怎么办? A:诊断步骤:
# 1. 检查 Token 是否有效
openclaw channels status --probe
# 2. 重新添加渠道
openclaw channels remove --channel telegram
openclaw channels add --channel telegram --token YOUR_NEW_TOKEN
# 3. 查看日志
openclaw logs --lines 100
Q13:Token 余额不足怎么办? A:
openclaw models status --probeQ14:AI 回复很慢怎么办? A:可能原因与解决方案:
openclaw memory index 减少上下文压缩压力agents.defaults.timeoutSecondsQ15:如何查看详细错误日志? A:实时查看错误:
openclaw logs --follow
过滤错误日志:
openclaw logs --follow | grep -i error
Q16:如何使用多个工作空间? A:创建单独的配置文件:
openclaw --profile work gateway start
openclaw --profile personal gateway start
Q17:如何让 AI 执行复杂工作流? A:编写插件(skills)并在对话中使用。插件会自动注册工具,AI 可调用这些工具完成步骤。
Q18:如何备份所有数据? A:
# 完整备份
cp -r ~/.openclaw ~/.openclaw.backup.$(date +%Y%m%d)
# 仅备份配置和记忆
tar -czf backup.tar.gz ~/.openclaw/openclaw.json ~/.openclaw/workspace
Q19:如何迁移到新机器? A:
~/.openclaw/ 目录Q20:如何升级到最新版本? A:执行命令:
openclaw update
# 重启网关
openclaw gateway restart
Q21:为什么 AI 会重复回复? A:可能原因:
解决方案:
# 检查日志
openclaw logs | grep "duplicate"
# 调整重试策略(或禁用不必要的重复检测)
Q22:为什么 AI 无法理解我的意图? A:优化建议:
Q23:为什么在某些群组中 AI 不响应? A:检查群组权限设置(需要 @提及、白名单等):
{"channels":{"discord":{"groups":{"*":{"requireMention":true,"allowedFrom":["server:123"]}}}}}
Q24:如何限制 AI 的回复长度? A:在系统提示中设置限制:
# 系统提示
请将回复控制在 300 字以内,使用结构化格式(列表、分段)提升可读性。
Q25:如何让 AI 自动过滤垃圾信息? A:在系统提示中添加垃圾信号词;或编写插件进行预处理过滤。
Q26:如何监控 OpenClaw 的资源使用? A:查看系统健康状态(不含命令历史):
openclaw status --all
也可查看 Gateway 的运行时指标,或通过 systemctl status openclaw-gateway 查看 GPU 占用(若使用本地模型)。
Q27:为什么 Cron 任务没有执行? A:
openclaw cron statusopenclaw cron list --allopenclaw cron runs --id <job-id> --limit 20Q28:如何优化内存使用? A:
Q29:如何让 AI 持续记住我的偏好? A:写入 MEMORY.md/文件;可启用记忆与索引并设置索引路径:
openclaw memory status
openclaw memory index --verbose
Q30:如何联系技术支持? A:
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
| 无法启动网关 | Node 版本过低 | 升级到 Node 22+ |
| 无法启动网关 | 端口冲突 | 更换端口或终止占用进程 |
| Token 验证失败 | API Key 错误 | 检查 API Key 是否有效 |
| 配置文件解析失败 | JSON 格式错误 | 用 openclaw config get 验证 |
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
| 渠道断连 | 网络问题 / Token 过期 | 检查网络并更新 Token |
| AI 不响应 | 模型服务宕机 | 切换到备用模型 |
| 回复变慢 | 上下文过大 | 减少历史记录或提升环境上限 |
| 内存溢出 | 超出系统资源 | 结束其他进程或升级硬件 |
| 症状 | 可能原因 | 解决方案 |
|---|---|---|
| 插件加载失败 | 依赖缺失 | 进入 plugin 目录执行 npm install |
| Cron 任务失败 | 路径错误 | 检查任务脚本路径和执行权限 |
| 浏览器无法启动 | Chrome 路径错误 | 配置浏览器可执行文件路径 |

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online