配置
OpenClaw 从 ~/.openclaw/openclaw.json 读取可选的
如果文件缺失,OpenClaw 使用安全的默认值。添加配置的原因:
- 连接频道并控制谁可以向机器人发送消息
- 设置模型、工具、沙盒或自动化(cron、hooks)
- 调整会话、媒体、网络或 UI
请参阅完整参考了解每个可用字段。
最小配置
// ~/.openclaw/openclaw.json
{
agents: { defaults: { workspace: "~/.openclaw/workspace" } },
channels: { whatsapp: { allowFrom: ["+15555550123"] } },
}
编辑配置
严格验证
验证失败时:
- 网关不会启动
- 仅诊断命令有效(
openclaw doctor、openclaw logs、openclaw health、openclaw status) - 运行
openclaw doctor查看确切问题 - 运行
openclaw doctor --fix(或--yes)应用修复
常见任务
- [WhatsApp](/zh/docs/channels/whatsapp) — `channels.whatsapp`
- [Telegram](/zh/docs/channels/telegram) — `channels.telegram`
- [Discord](/zh/docs/channels/discord) — `channels.discord`
- [Slack](/zh/docs/channels/slack) — `channels.slack`
- [Signal](/zh/docs/channels/signal) — `channels.signal`
- [iMessage](/zh/docs/channels/imessage) — `channels.imessage`
- [Google Chat](/zh/docs/channels/googlechat) — `channels.googlechat`
- [Mattermost](/zh/docs/channels/mattermost) — `channels.mattermost`
- [MS Teams](/zh/docs/channels/msteams) — `channels.msteams`
所有频道共享相同的 DM 策略模式:
```json5
{
channels: {
telegram: {
enabled: true,
botToken: "123:abc",
dmPolicy: "pairing", // pairing | allowlist | open | disabled
allowFrom: ["tg:123"], // 仅用于 allowlist/open
},
},
}
```
```json5
{
agents: {
defaults: {
model: {
primary: "anthropic/claude-sonnet-4-5",
fallbacks: ["openai/gpt-5.2"],
},
models: {
"anthropic/claude-sonnet-4-5": { alias: "Sonnet" },
"openai/gpt-5.2": { alias: "GPT" },
},
},
},
}
```
- `agents.defaults.models` 定义模型目录并作为 `/model` 的允许列表。
- 模型引用使用 `provider/model` 格式(例如 `anthropic/claude-opus-4-6`)。
- `agents.defaults.imageMaxDimensionPx` 控制转录/工具图片的缩小(默认 `1200`);较低的值通常在截图密集运行中减少视觉令牌使用。
- 请参阅 [Models CLI](/zh/docs/concepts/models) 以在聊天中切换模型,并参阅 [Model Failover](/zh/docs/concepts/model-failover) 了解身份轮换和后备行为。
- 对于自定义/自托管提供商,请参阅参考文档中的 [Custom providers](/zh/docs/gateway/configuration-reference#custom-providers-and-base-urls)。
- `"pairing"`(默认):未知发送者获得一次性配对代码以进行批准
- `"allowlist"`:仅允许 `allowFrom`(或配对允许存储)中的发送者
- `"open"`:允许所有入站 DM(需要 `allowFrom: ["*"]`)
- `"disabled"`:忽略所有 DM
对于群组,使用 `groupPolicy` + `groupAllowFrom` 或特定频道的允许列表。
请参阅 [完整参考](/zh/docs/gateway/configuration-reference#dm-and-group-access) 了解每个频道的详细信息。
```json5
{
agents: {
list: [
{
id: "main",
groupChat: {
mentionPatterns: ["@openclaw", "openclaw"],
},
},
],
},
channels: {
whatsapp: {
groups: { "*": { requireMention: true } },
},
},
}
```
- **元数据提及**:原生 @-提及(WhatsApp 点击提及、Telegram @bot 等)
- **文本模式**:`mentionPatterns` 中的正则模式
- 请参阅 [完整参考](/zh/docs/gateway/configuration-reference#group-chat-mention-gating) 了解每个频道的覆盖和自我聊天模式。
```json5
{
session: {
dmScope: "per-channel-peer", // 推荐用于多用户
threadBindings: {
enabled: true,
ttlHours: 24,
},
reset: {
mode: "daily",
atHour: 4,
idleMinutes: 120,
},
},
}
```
- `dmScope`:`main`(共享)| `per-peer` | `per-channel-peer` | `per-account-channel-peer`
- `threadBindings`:线程绑定会话路由的全局默认值(Discord 支持 `/focus`、`/unfocus`、`/agents` 和 `/session ttl`)。
- 请参阅 [Session Management](/zh/docs/concepts/session) 了解范围、身份链接和发送策略。
- 请参阅 [完整参考](/zh/docs/gateway/configuration-reference#session) 了解所有字段。
```json5
{
agents: {
defaults: {
sandbox: {
mode: "non-main", // off | non-main | all
scope: "agent", // session | agent | shared
},
},
},
}
```
首先构建镜像:`scripts/sandbox-setup.sh`
请参阅 [Sandboxing](/zh/docs/gateway/sandboxing) 获取完整指南和 [完整参考](/zh/docs/gateway/configuration-reference#sandbox) 了解所有选项。
- `every`:持续时间字符串(`30m`、`2h`)。设置 `0m` 禁用。
- `target`:`last` | `whatsapp` | `telegram` | `discord` | `none`
- `directPolicy`:`allow`(默认)或 `block` 用于 DM 风格的心跳目标
- 请参阅 [Heartbeat](/zh/docs/gateway/heartbeat) 获取完整指南。
- `sessionRetention`:从 `sessions.json` 中修剪已完成的隔离运行会话(默认 `24h`;设置 `false` 禁用)。
- `runLog`:按大小和保留行数修剪 `cron/runs/<jobId>.jsonl`。
- 请参阅 [Cron jobs](/zh/docs/automation/cron-jobs) 了解功能概述和 CLI 示例。
```json5
{
hooks: {
enabled: true,
token: "shared-secret",
path: "/hooks",
defaultSessionKey: "hook:ingress",
allowRequestSessionKey: false,
allowedSessionKeyPrefixes: ["hook:"],
mappings: [
{
match: { path: "gmail" },
action: "agent",
agentId: "main",
deliver: true,
},
],
},
}
```
请参阅 [完整参考](/zh/docs/gateway/configuration-reference#hooks) 了解所有映射选项和 Gmail 集成。
```json5
{
agents: {
list: [
{ id: "home", default: true, workspace: "~/.openclaw/workspace-home" },
{ id: "work", workspace: "~/.openclaw/workspace-work" },
],
},
bindings: [
{ agentId: "home", match: { channel: "whatsapp", accountId: "personal" } },
{ agentId: "work", match: { channel: "whatsapp", accountId: "biz" } },
],
}
```
请参阅 [Multi-Agent](/zh/docs/concepts/multi-agent) 和 [完整参考](/zh/docs/gateway/configuration-reference#multi-agent-routing) 了解绑定规则和每个代理的访问配置文件。
```json5
// ~/.openclaw/openclaw.json
{
gateway: { port: 18789 },
agents: { $include: "./agents.json5" },
broadcast: {
$include: ["./clients/a.json5", "./clients/b.json5"],
},
}
```
- **单个文件**:替换包含的对象
- **文件数组**:按顺序深度合并(后出现的覆盖先前的)
- **兄弟键**:在包含之后合并(覆盖包含的值)
- **嵌套包含**:支持最多 10 层深
- **相对路径**:相对于包含文件解析
- **错误处理**:对缺失文件、解析错误和循环包含提供清晰的错误
配置热重载
网关监视 ~/.openclaw/openclaw.json 并自动应用更改 —— 大多数设置无需手动重启。
重载模式
| 模式 | 行为 |
|---|---|
hybrid(默认) | 立即热应用安全更改。自动重启关键的更改。 |
hot | 仅热应用安全更改。当需要重启时记录警告 —— 您自行处理。 |
restart | 在任何配置更改时重启网关,无论是安全还是不安全。 |
off | 禁用文件监视。更改在下次手动重启时生效。 |
{
gateway: {
reload: { mode: "hybrid", debounceMs: 300 },
},
}
哪些热应用 vs 哪些需要重启
大多数字段热应用而无需停机。在 hybrid 模式下,需要重启的更改会自动处理。
| 类别 | 字段 | 需要重启? |
|---|---|---|
| 频道 | channels.*, web(WhatsApp) — 所有内置和扩展频道 | 否 |
| 代理和模型 | agent, agents, models, routing | 否 |
| 自动化 | hooks, cron, agent.heartbeat | 否 |
| 会话和消息 | session, messages | 否 |
| 工具和媒体 | tools, browser, skills, audio, talk | 否 |
| UI 和杂项 | ui, logging, identity, bindings | 否 |
| 网关服务器 | gateway.*(端口、绑定、身份验证、tailscale、TLS、HTTP) | 是 |
| 基础设施 | discovery, canvasHost, plugins | 是 |
配置 RPC(编程更新)
<Warning>
`config.apply` 替换**整个配置**。对部分更新使用 `config.patch`,或对单个键使用 `openclaw config set`。
</Warning>
参数:
- `raw`(字符串)—— 整个配置的 JSON5 有效负载
- `baseHash`(可选)—— `config.get` 中的配置哈希(当配置存在时必需)
- `sessionKey`(可选)—— 重启后唤醒 ping 的会话密钥
- `note`(可选)—— 重启哨兵的说明
- `restartDelayMs`(可选)—— 重启前的延迟(默认 2000)
重启请求在已有待处理/进行中的请求时合并,并在重启周期之间应用 30 秒的冷却。
```bash
openclaw gateway call config.get --params '{}' # 捕获 payload.hash
openclaw gateway call config.apply --params '{
"raw": "{ agents: { defaults: { workspace: \"~/.openclaw/workspace\" } } }",
"baseHash": "<hash>",
"sessionKey": "agent:main:whatsapp:dm:+15555550123"
}'
```
- 对象递归合并
- `null` 删除一个键
- 数组替换
参数:
- `raw`(字符串)—— 仅包含要更改的键的 JSON5
- `baseHash`(必需)—— `config.get` 中的配置哈希
- `sessionKey`、`note`、`restartDelayMs`—— 与 `config.apply` 相同
重启行为匹配 `config.apply`:合并待处理的重启加上重启周期之间 30 秒的冷却。
```bash
openclaw gateway call config.patch --params '{
"raw": "{ channels: { telegram: { groups: { \"*\": { requireMention: false } } } } }",
"baseHash": "<hash>"
}'
```
环境变量
OpenClaw 从父进程加上以下内容读取环境变量:
.env来自当前工作目录(如果存在)~/.openclaw/.env(全局后备)
这两个文件都不会覆盖现有的环境变量。您还可以在配置中设置内联环境变量:
{
env: {
OPENROUTER_API_KEY: "sk-or-...",
vars: { GROQ_API_KEY: "gsk-..." },
},
}
{
env: {
shellEnv: { enabled: true, timeoutMs: 15000 },
},
}
环境变量等效项:OPENCLAW_LOAD_SHELL_ENV=1
{
gateway: { auth: { token: "${OPENCLAW_GATEWAY_TOKEN}" } },
models: { providers: { custom: { apiKey: "${CUSTOM_API_KEY}" } } },
}
规则:
- 仅匹配大写名称:
[A-Z_][A-Z0-9_]* - 缺失/空的变量在加载时抛出错误
- 用
$${VAR}转义以进行字面输出 - 在
$include文件内工作 - 内联替换:
"${BASE}/v1"→"https://api.example.com/v1"
{
models: {
providers: {
openai: { apiKey: { source: "env", provider: "default", id: "OPENAI_API_KEY" } },
},
},
skills: {
entries: {
"nano-banana-pro": {
apiKey: {
source: "file",
provider: "filemain",
id: "/skills/entries/nano-banana-pro/apiKey",
},
},
},
},
channels: {
googlechat: {
serviceAccountRef: {
source: "exec",
provider: "vault",
id: "channels/googlechat/serviceAccount",
},
},
},
}
SecretRef 详细信息(包括用于 env/file/exec 的 secrets.providers)在 Secrets Management 中。
请参阅 Environment 了解完整的优先级和来源。
完整参考
有关完整的逐字段参考,请参阅 Configuration Reference。
相关:Configuration Examples · Configuration Reference · Doctor