Clawdbot/Moltbot:对接Telegram配置实录
从安装到 Telegram 集成,一个折腾了数小时的真实经历
前言
Clawdbot(现已更名为 Moltbot)是一个开源的本地 AI 助手,可以通过 Telegram、WhatsApp、Discord 等消息应用与 Claude 模型交互。本文记录了在特殊网络环境下配置 Clawdbot 的完整过程,包括遇到的各种问题和解决方案。
环境信息:
- 操作系统:macOS 14.5
- Node.js:v22.22.0
- Clawdbot 版本:2026.1.24-3
- 网络环境:需要魔法上网访问境外服务
一、安装 Clawdbot
1.1 前置条件
确保 Node.js 版本不低于 22.0.0:
# 使用 nvm 安装
nvm install 22
nvm use 22
nvm alias default 22
# 验证版本
node --version # 应显示 v22.x.x
1.2 全局安装
npm install -g clawdbot
1.3 初始化配置
clawdbot onboard
配置向导会引导你完成基本设置:
🦞 Clawdbot 2026.1.24-3
│
◇ 安全提示 ───────────────────────────────────────────────────╮
│ Clawdbot 可以运行命令、读写文件,请确保了解相关风险。 │
├──────────────────────────────────────────────────────────────╯
│
◇ 我已了解风险,继续?
│ 是
│
◇ 选择模型/认证提供商
│ Anthropic
│
◇ 选择认证方式
│ Anthropic API key
二、API 代理配置(推荐方案)
对于需要魔法上网的用户,使用第三方 API 代理服务是最稳定的方案。
2.1 配置文件结构
~/.clawdbot/
├── clawdbot.json # 主配置文件
└── agents/
└── main/
└── agent/
└── auth-profiles.json # 认证配置文件
2.2 主配置文件
文件位置: ~/.clawdbot/clawdbot.json
{
"models": {
"providers": {
"anthropic": {
"baseUrl": "https://你的代理地址.com",
"apiKey": "你的API密钥",
"api": "anthropic-messages",
"models": []
}
}
},
"agents": {
"defaults": {
"model": {
"primary": "anthropic/claude-sonnet-4-5-20250929"
},
"workspace": "/Users/你的用户名/clawd"
}
}
}
2.3 关键配置项说明
| 字段 | 说明 | 必需 |
|---|---|---|
baseUrl | API 代理地址 | ✅ |
apiKey | 代理服务的 API 密钥 | ✅ |
api | 必须设置为 anthropic-messages | ✅ |
models | 必须包含此字段,可以为空数组 [] | ✅ |
⚠️ 踩坑点: 如果缺少
api或models字段,会导致 HTTP 403 错误!这是我花了很长时间才发现的问题。
2.4 认证配置文件
文件位置: ~/.clawdbot/agents/main/agent/auth-profiles.json
{
"version": 1,
"profiles": {
"anthropic:default": {
"type": "api_key",
"provider": "anthropic",
"apiKey": "你的API密钥"
}
}
}
2.5 验证 API 配置
配置完成后,用 curl 测试 API 是否可用:
# ❌ 这里注意请求的model是自己令牌支持的!
curl -H "x-api-key: 你的API密钥" \
-H "content-type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{"model":"claude-sonnet-4-5-20250929","max_tokens":50,"messages":[{"role":"user","content":"你好"}]}' \
https://你的代理地址.com/v1/messages
预期返回类似:
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [{"type": "text", "text": "你好!有什么我可以帮助你的吗?"}]
}
三、代理配置(核心难点)
由于 Telegram API 需要魔法访问,这是配置中最复杂的部分。
3.1 问题:Node.js 不读取系统代理
设置环境变量对 Node.js 22 的网络请求无效:
# ❌ 这样做是无效的!
export HTTP_PROXY=http://127.0.0.1:1087
export HTTPS_PROXY=http://127.0.0.1:1087
clawdbot gateway # Telegram 连接仍会失败
会看到如下错误:
[telegram] telegram setMyCommands failed: HttpError: Network request failed!
[clawdbot] Unhandled promise rejection: TypeError: fetch failed
3.2 解决方案:使用 undici 代理
安装前请先有自己的梯子,笔者用的shadowsocks且代理端口1087
步骤一:安装 undici
npm install -g undici
步骤二:创建代理配置脚本
cat > ~/proxy-setup.js << 'EOF'
const { setGlobalDispatcher, ProxyAgent } = require('undici');
setGlobalDispatcher(new ProxyAgent('http://127.0.0.1:1087'));
console.log('[代理] 已设置全局代理: http://127.0.0.1:1087');
EOF
注意:请将
1087替换为你实际的代理端口
步骤三:创建启动脚本
cat > ~/start-clawdbot.sh << 'EOF'
#!/bin/bash
NODE_PATH=$(npm root -g) node --require ~/proxy-setup.js $(which clawdbot) gateway --port 18789
EOF
chmod +x ~/start-clawdbot.sh
步骤四:使用启动脚本运行
~/start-clawdbot.sh
成功启动后会看到:
[代理] 已设置全局代理: http://127.0.0.1:1087
🦞 Clawdbot 2026.1.24-3
[gateway] 正在监听 ws://127.0.0.1:18789
[telegram] [default] 正在启动...
[telegram] [default] 启动成功 (@你的机器人名)
有这个红框表示telegram也成功连接
3.3 验证代理是否生效
测试 Telegram API 连接:
curl -x http://127.0.0.1:1087 https://api.telegram.org/bot你的TOKEN/getMe
如果返回机器人信息,说明代理配置正确。
四、Telegram 机器人配置
4.1 创建 Telegram 机器人
- 在 Telegram 搜索 @BotFather
- 发送
/newbot - 按提示设置机器人名称和用户名
- 获取机器人 Token(格式类似
123456789:ABCdefGHIjklMNOpqrsTUVwxyz)
4.2 配置 Clawdbot
在 ~/.clawdbot/clawdbot.json 中添加 Telegram 配置:
{
"channels": {
"telegram": {
"enabled": true,
"dmPolicy": "pairing",
"botToken": "你的机器人Token",
"groupPolicy": "allowlist",
"streamMode": "partial"
}
}
}
4.3 配对你的 Telegram 账号
- 使用代理启动 Gateway:
~/start-clawdbot.sh
-
在 Telegram 给你的机器人发送任意消息
-
机器人会回复配对码:
Clawdbot: 访问未配置
你的 Telegram 用户 ID: 123456789
配对码: ABC12345
请机器人管理员执行以下命令批准:
clawdbot pairing approve telegram ABC12345
- 在终端执行批准命令:
clawdbot pairing approve telegram ABC12345
- 配对成功后即可正常对话
五、Web 控制面板
Clawdbot 提供了一个功能丰富的 Web 控制面板。
5.1 访问地址
Gateway 启动后,访问:
http://127.0.0.1:18789/?token=你的gateway_token
或者使用命令自动打开:
clawdbot dashboard
5.2 主要功能
- 💬 对话:直接与 AI 对话
- 📊 概览:查看系统状态
- 🔌 频道:管理 Telegram/WhatsApp 等连接
- 📁 会话:查看和管理对话历史
- ⚙️ 配置:修改各项设置
笔者让他帮忙编写这篇文档
六、完整配置文件参考
6.1 主配置文件 ~/.clawdbot/clawdbot.json
{
"meta": {
"lastTouchedVersion": "2026.1.24-3"
},
"models": {
"providers": {
"anthropic": {
"baseUrl": "https://你的代理地址.com",
"apiKey": "你的API密钥",
"api": "anthropic-messages",
"models": []
}
}
},
"agents": {
"defaults": {
"workspace": "/Users/你的用户名/clawd",
"model": {
"primary": "anthropic/claude-sonnet-4-5-20250929"
},
"compaction": {
"mode": "safeguard"
},
"maxConcurrent": 4
}
},
"channels": {
"telegram": {
"enabled": true,
"dmPolicy": "pairing",
"botToken": "你的机器人Token",
"groupPolicy": "allowlist",
"streamMode": "partial"
}
},
"gateway": {
"port": 18789,
"mode": "local",
"bind": "loopback",
"auth": {
"mode": "token",
"token": "你的gateway_token"
}
}
}
6.2 认证配置文件
~/.clawdbot/agents/main/agent/auth-profiles.json
{
"version": 1,
"profiles": {
"anthropic:default": {
"type": "api_key",
"provider": "anthropic",
"apiKey": "你的API密钥"
}
}
}
七、常见问题排查
7.1 HTTP 403 错误
可能原因:
- 缺少
api: "anthropic-messages"配置 - 缺少
models: []字段 - API 密钥无效或余额不足
解决方案: 检查配置文件格式,确保包含所有必需字段。
7.2 Telegram 连接失败
错误信息:
TypeError: fetch failed
可能原因:
- 代理未正确配置
- Node.js 的网络请求没有走代理
解决方案: 使用 undici 代理方案(见第三节)。
7.3 Gateway 启动但无响应
检查步骤:
# 查看状态
clawdbot status
# 查看日志
tail -f /tmp/clawdbot/clawdbot-$(date +%Y-%m-%d).log
7.4 配对码不显示
可能原因:
- Telegram 机器人 Token 无效
- 代理连接不稳定
解决方案:
- 验证 Token:
curl -x http://127.0.0.1:1087 https://api.telegram.org/bot你的TOKEN/getMe - 检查代理连接是否正常
八、常用命令速查
| 命令 | 说明 |
|---|---|
clawdbot status | 查看状态 |
clawdbot status --deep | 查看详细状态 |
clawdbot models status | 查看模型状态 |
clawdbot logs --follow | 实时查看日志 |
clawdbot gateway restart | 重启网关 |
clawdbot configure | 配置向导 |
clawdbot doctor | 诊断问题 |
clawdbot dashboard | 打开控制面板 |
clawdbot pairing approve telegram <码> | 批准配对 |
九、总结
在特殊网络环境下配置 Clawdbot 的主要挑战:
- 代理配置复杂:Node.js 22 的网络请求不读取环境变量代理,需要使用 undici 方案
- 配置格式严格:
api和models字段缺一不可,否则会报 403 错误 - 多个配置文件:主配置和认证配置需要同时正确设置
推荐方案:
- 使用 API 代理服务获取稳定的 API 访问
- 使用 undici + 启动脚本配置全局代理
- 保留配置备份,方便出问题时恢复
参考资源
文件结构速查
~/.clawdbot/
├── clawdbot.json # 主配置
├── logs/ # 日志目录
│ ├── gateway.log
│ └── gateway.err.log
└── agents/
└── main/
├── agent/
│ └── auth-profiles.json # 认证配置
└── sessions/ # 会话存储
~/
├── proxy-setup.js # 代理配置脚本
└── start-clawdbot.sh # 启动脚本
附录:LLM摘要
以下是本文的结构化摘要,方便喂给其他 AI 模型快速理解,节省 token 消耗。
yaml
# Clawdbot/Moltbot 配置知识卡片
# 生成时间: 2026-01
# 用途: 供AI模型快速理解本文核心内容
meta:
project: Clawdbot (已更名 Moltbot)
repo: https://github.com/moltbot/moltbot
docs: https://docs.molt.bot/
功能: 本地AI助手,支持Telegram/WhatsApp/Discord接入Claude模型
环境要求:
os: macOS/Linux
node: ">=22.0.0"
安装: npm install -g clawdbot
初始化: clawdbot onboard
配置文件:
主配置: ~/.clawdbot/clawdbot.json
认证配置: ~/.clawdbot/agents/main/agent/auth-profiles.json
API代理配置:
位置: clawdbot.json -> models.providers.anthropic
必需字段:
baseUrl: "API代理地址"
apiKey: "密钥"
api: "anthropic-messages" # 关键!缺少报403
models: [] # 关键!必须存在,可为空数组
示例: |
{"models":{"providers":{"anthropic":{
"baseUrl":"https://proxy.example.com",
"apiKey":"sk-xxx",
"api":"anthropic-messages",
"models":[]
}}}}
认证配置:
位置: auth-profiles.json
格式: |
{"version":1,"profiles":{"anthropic:default":{
"type":"api_key","provider":"anthropic","apiKey":"sk-xxx"
}}}
网络代理问题:
症状: Telegram连接失败,fetch failed
原因: Node.js 22 的 fetch 不读取 HTTP_PROXY 环境变量
错误方式: export HTTP_PROXY=... 无效
正确方案:
1_安装: npm install -g undici
2_创建脚本: |
# ~/proxy-setup.js
const {setGlobalDispatcher,ProxyAgent}=require('undici');
setGlobalDispatcher(new ProxyAgent('http://127.0.0.1:端口'));
3_启动命令: |
NODE_PATH=$(npm root -g) node --require ~/proxy-setup.js $(which clawdbot) gateway --port 18789
Telegram配置:
配置位置: clawdbot.json -> channels.telegram
必需字段: {enabled:true, botToken:"xxx", dmPolicy:"pairing"}
配对流程:
1: 通过@BotFather创建机器人获取token
2: 配置clawdbot.json
3: 启动gateway
4: Telegram发消息给bot
5: 获取配对码
6: 执行 clawdbot pairing approve telegram <配对码>
Web控制台:
地址: http://127.0.0.1:18789/?token=<gateway_token>
命令: clawdbot dashboard
常见错误:
HTTP_403:
原因: [缺少api字段, 缺少models字段, apiKey无效]
解决: 检查配置文件格式
fetch_failed:
原因: 代理未生效
解决: 使用undici方案
配对码不显示:
原因: [botToken无效, 代理不稳定]
解决: curl测试Telegram API
命令速查:
状态: clawdbot status [--deep]
日志: clawdbot logs --follow
重启: clawdbot gateway restart
诊断: clawdbot doctor
配对: clawdbot pairing approve telegram <码>
面板: clawdbot dashboard
文件结构:
~/.clawdbot/clawdbot.json: 主配置
~/.clawdbot/agents/main/agent/auth-profiles.json: 认证
~/proxy-setup.js: 代理脚本
~/start-clawdbot.sh: 启动脚本
使用方法: 复制上方 YAML 代码块,直接粘贴给 AI 助手,即可快速获得 Clawdbot 配置相关问题的解答。
本文基于 2026 年 1 月的实际配置经历撰写, 由claude-opus-4.5 + clawdbot整理编写。