OpenClaw Discord 和微信接入配置指南
本文详细介绍如何配置 OpenClaw 的 Discord 和微信接入,包括企业微信和个人微信两种方案。
一、Discord 接入配置
1.1 为什么选择 Discord?
Discord 优势对比表:
| 特性 | Discord | Telegram | 微信 | |
|---|---|---|---|---|
| 国内可用性 | ✅ 可用 | ⚠️ 需换区 | ✅ 可用 | ✅ 可用 |
| 费用 | 🆓 免费 | 🆓 免费 | 💰 收费 | 🆓 免费 |
| Bot 创建难度 | ⭐ 简单 | ⭐⭐ 中等 | ⭐⭐⭐ 复杂 | ⭐⭐⭐⭐ 复杂 |
| 功能完整度 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ |
| 推荐度 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐⭐ |
1.2 创建 Discord Bot
步骤 1:访问开发者门户
https://discord.com/developers/applications
步骤 2:创建应用
登录 Discord,点击 New Application,输入 Bot 名称,点击 Create。
步骤 3:创建 Bot
| 步骤 | 操作 | 说明 |
|---|---|---|
| 1 | 点击左侧 Bot | 进入 Bot 配置页面 |
| 2 | 点击 Add Bot | 创建 Bot |
| 3 | 点击 Reset Token | 生成新 Token |
| 4 | 复制 Token | ⚠️ 保存好,只显示一次 |
步骤 4:配置 Bot 权限
# 必需权限清单
✅ Send Messages # 发送消息
✅ Read Message History # 读取历史消息
✅ Use Slash Commands # 使用斜杠命令
✅ Embed Links # 嵌入链接
✅ Attach Files # 附加文件
步骤 5:生成邀请链接
https://discord.com/api/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=2147483648&scope=bot
替换 YOUR_CLIENT_ID 为应用页面的 Application ID。
1.3 邀请 Bot 到服务器
打开邀请链接,请求授权,选择服务器,点击授权添加 Bot。
1.4 在 OpenClaw 中配置
# 运行配置向导
openclaw onboard
# 选择消息渠道
? Select messaging platform: Discord
# 输入 Discord Bot Token
? Enter Discord Bot Token: MTIzNDU2Nzg5MDEyMzQ1Njc4OTAuGh_xyz.abcdefghijklmnopqrstuvwxyz1234567890
# 配置完成
✅ Discord integration configured successfully!
1.5 启动并测试
# 启动 OpenClaw Gateway
openclaw gateway start
# 或指定端口和绑定地址
openclaw gateway --bind loopback --port 18789 --verbose
# 终端输出
🦞 OpenClaw 2026.3.2 — Gateway started
Listening on http://127.0.0.1:18789
Discord Bot connected: YourBotName#1234
二、微信接入配置
2.1 微信接入方案对比
| 方案 | 类型 | 难度 | 费用 | 推荐度 | 说明 |
|---|---|---|---|---|---|
| 个人微信(Wechaty) | 非官方 | ⭐⭐⭐ | 🆓 免费 | ⭐⭐⭐⭐ | 适合个人使用 |
| 企业微信 | 官方 | ⭐⭐⭐⭐ | 🆓 免费 | ⭐⭐⭐⭐⭐ | 适合企业 |
| 微信公众号 | 官方 | ⭐⭐⭐⭐ | 🆓 免费 | ⭐⭐⭐ | 适合服务号 |
| 微信小程序 | 官方 | ⭐⭐⭐⭐⭐ | 🆓 免费 | ⭐⭐ | 开发复杂 |
2.2 方案一:个人微信(Wechaty)
2.2.1 什么是 Wechaty?
Wechaty 是一个开源的微信个人号接口,可以让你的个人微信号变成 Bot。
2.2.2 安装 Wechaty
# 1. 创建 Wechaty 项目目录
mkdir -p ~/.openclaw/wechaty
cd ~/.openclaw/wechaty
# 2. 初始化 npm 项目
npm init -y
# 3. 安装 Wechaty 依赖(仅需 wechaty,使用默认 puppet)
npm install wechaty
# 4. 创建 bot.js(见下方「完整 bot.js 代码」)
# 5. 启动 Wechaty Bot
node bot.js
完整 bot.js 代码
将以下内容保存为 ~/.openclaw/wechaty/bot.js。
/**
* OpenClaw 微信桥接 Bot (Wechaty)
*
* 功能:个人微信收到消息后,转发给 OpenClaw,并把回复发回微信。
* 依赖:npm install wechaty
* 运行:node bot.js
*
* 注意:Wechaty v1.x 使用 WechatyBuilder.build() 创建实例,不要用 new Wechaty()。
*/
const { WechatyBuilder } = require('wechaty')
// ========== 配置 ==========
const OPENCLAW_URL = process.env.OPENCLAW_URL || 'http://127.0.0.1:18789'
const BOT_NAME = process.env.BOT_NAME || 'openclaw-bot'
const OPENCLAW_API = `${OPENCLAW_URL.replace(/\/$/, '')}/api/message`
// ========== 创建 Bot ==========
const bot = WechatyBuilder.build({ name: BOT_NAME })
// ========== 扫码登录 ==========
bot.on('scan', (qrcode, status) => {
const url = `https://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`
console.log(`[扫码] 状态:${status}`)
console.log(`[扫码] 请用微信扫描:${url}`)
})
bot.on('login', (user) => {
console.log()
})
bot.(, {
.()
})
bot.(, (message) => {
(message.())
text = message.()?.()
(!text)
talker = message.()
room = message.()
= room ? : talker.()
.()
{
response = (, {
: ,
: { : },
: .({
: ,
: text,
: talker.,
: talker.(),
: room?.,
: room?.(),
}),
})
(!response.) {
()
}
result = response.()
reply = result?. ?? result?. ?? result?.
(reply) {
message.((reply))
} {
message.()
}
} (error) {
.(, error.)
message.()
}
})
bot.(, {
.(, error)
})
.()
bot.().( .()).( {
.(, err)
process.()
})
配置说明:
| 配置项 | 说明 | 默认值 |
|---|---|---|
OPENCLAW_URL | OpenClaw Gateway 地址 | http://127.0.0.1:18789 |
BOT_NAME | Bot 名称(日志用) | openclaw-bot |
OPENCLAW_API | 消息转发 API 路径 | {OPENCLAW_URL}/api/message |
常见问题排查:
| 问题 | 原因 | 解决方案 |
|---|---|---|
Wechaty is not a constructor | Wechaty v1.x 已改为 Builder 模式 | 使用 const { WechatyBuilder } = require('wechaty') |
PuppetWechat is not a constructor | 不要用 wechaty-puppet-wechat | 只装 npm install wechaty,不指定 puppet |
| 频繁掉线 | 免费协议限制 | 考虑企业微信方案或付费 puppet |
2.2.4 配置 OpenClaw
在 OpenClaw 配置文件 ~/.openclaw/openclaw.json 中增加以下内容。
方式一:命令行(推荐)
# 启用 wechat 渠道
openclaw config set channels.wechat.enabled true
# 配置 WeChat 消息(对接 bot.js 转发)
openclaw config set messaging.wechat.enabled true
openclaw config set messaging.wechat.endpoint "http://127.0.0.1:18789/api/message"
openclaw config set messaging.wechat.autoReply true
方式二:直接编辑配置文件
编辑 ~/.openclaw/openclaw.json,加入:
"channels": { "wechat": { "enabled": true } },
"messaging": { "wechat": { "enabled": true, "endpoint": "http://127.0.0.1:18789/api/message", "autoReply": true } }
2.3 方案二:企业微信(推荐)
2.3.1 为什么选择企业微信?
| 特性 | 个人微信(Wechaty) | 企业微信 |
|---|---|---|
| 官方支持 | ❌ 非官方 | ✅ 官方 |
| 稳定性 | ⭐⭐⭐ 可能被封 | ⭐⭐⭐⭐⭐ 稳定 |
| API 文档 | ⭐⭐⭐ 社区维护 | ⭐⭐⭐⭐⭐ 官方文档 |
2.3.2 注册企业微信
访问企业微信官网,注册企业,填写企业信息,管理员认证。
2.3.3 创建企业微信应用
- 登录企业微信管理后台 https://work.weixin.qq.com/
- 点击「应用管理」->「创建应用」
- 填写应用信息,设置可见范围
- 获取 AgentId 和 Secret
2.3.4 获取企业信息
- CorpId(企业 ID)
- AgentId(应用 ID)
- Secret(应用密钥)
- Token & AESKey(用于验证签名)
2.3.5 配置 OpenClaw
# 1. 安装企业微信插件
npm install -g @openclaw/plugin-wework
# 2. 配置企业微信
openclaw config set wework.corpId "your_corp_id"
openclaw config set wework.agentId "your_agent_id"
openclaw config set wework.secret "your_secret"
openclaw config set wework.token "your_token"
openclaw config set wework.encodingAESKey "your_aes_key"
# 3. 设置回调 URL
openclaw config set wework.callbackUrl "https://your-domain.com/wework/callback"
# 4. 启动服务
openclaw gateway start --enable-wework
2.3.6 配置回调服务器
方案 A:使用公网服务器
server {
listen 80;
server_name your-domain.com;
location /wework/callback {
proxy_pass http://127.0.0.1:18789;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
方案 B:使用内网穿透(开发测试)
# 使用 ngrok
npm install -g ngrok
ngrok http 18789
2.3.7 验证配置
# 1. 启动 OpenClaw
openclaw gateway start --verbose
# 2. 在企业微信应用中点击「验证」
# 3. 查看日志
openclaw logs --tail 50
三、其他消息渠道配置
3.1 Telegram 配置
# 1. 在 Telegram 中搜索 @BotFather
# 2. 发送 /newbot
# 3. 按提示设置 Bot 名称和用户名
# 4. 获取 Bot Token
# 配置 Telegram
openclaw config set telegram.token "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
openclaw gateway start --enable-telegram
3.2 飞书配置
# 配置飞书
openclaw config set feishu.appId "cli_xxxxxxxx"
openclaw config set feishu.appSecret "xxxxxxxxxxxxxxxx"
openclaw config set feishu.verificationToken "xxxxxxxx"
openclaw config set feishu.encryptKey "xxxxxxxx"
openclaw gateway start --enable-feishu
3.3 钉钉配置
# 配置钉钉
openclaw config set dingtalk.webhook "https://oapi.dingtalk.com/robot/send?access_token=xxxxx"
openclaw config set dingtalk.secret "SECxxxxxxxxxx"
openclaw gateway start --enable-dingtalk
3.4 Slack 配置
# 配置 Slack
openclaw config set slack.token "xoxb-xxxxxxxxxxxxx"
openclaw config set slack.signingSecret "xxxxxxxxxxxxxxxx"
openclaw gateway start --enable-slack
3.5 消息渠道全面对比表
| 渠道 | 国内可用 | 费用 | 配置难度 | 功能完整度 | 推荐度 |
|---|---|---|---|---|---|
| Discord | ✅ 可用 | 🆓 免费 | ⭐ 简单 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 企业微信 | ✅ 可用 | 🆓 免费 | ⭐⭐⭐ 中等 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| 个人微信 | ✅ 可用 | 🆓 免费 | ⭐⭐⭐ 中等 | ⭐⭐⭐ | ⭐⭐⭐⭐ |
| 飞书 | ✅ 可用 | 🆓 免费 | ⭐⭐⭐ 中等 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| 钉钉 | ✅ 可用 | 🆓 免费 | ⭐⭐ 简单 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Telegram | ⚠️ 需换区 | 🆓 免费 | ⭐⭐ 简单 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
| Slack | ✅ 可用 | 💰 部分收费 | ⭐⭐ 简单 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ |
四、高级配置
4.1 多模型配置
# 配置 Claude
openclaw config set ai.claude.apiKey "sk-ant-xxxxx"
openclaw config set ai.claude.model "claude-3-5-sonnet-20241022"
# 配置 GPT
openclaw config set ai.openai.apiKey "sk-xxxxx"
openclaw config set ai.openai.model "gpt-4"
# 配置 Qwen
openclaw config set ai.qwen.apiKey "sk-xxxxx"
openclaw config set ai.qwen.model "qwen-max"
# 设置默认模型
openclaw config set ai.default "claude"
4.2 自定义提示词(Prompt)
# 编辑系统提示词
openclaw config edit prompts
4.3 权限管理
# 设置管理员
openclaw config set auth.admins "user_id_1,user_id_2"
# 设置白名单
openclaw config set auth.whitelist "user_id_3,user_id_4"
# 启用认证
openclaw config set auth.enabled true
4.4 Docker 部署
FROM node:22-alpine
RUN npm install -g openclaw@latest
WORKDIR /app
COPY config.json /root/.openclaw/config.json
EXPOSE 18789
CMD ["openclaw", "gateway", "start", "--bind", "0.0.0.0", "--port", "18789"]
五、常见问题
5.1 安装问题
问题 1:Node.js 版本不符合要求
# 症状
Error: OpenClaw requires Node.js >=22.0.0
# 解决方案
nvm install 22
nvm use 22
node -v
5.2 配置问题
问题 1:Discord Bot 无法连接
| 检查项 | 命令 | 预期结果 |
|---|---|---|
| Token 是否正确 | openclaw config get discord.token | 显示 Token |
| Bot 是否在线 | 查看 Discord 开发者门户 | 显示在线 |
| 权限是否足够 | 检查 Bot 权限 | 有发送消息权限 |
问题 2:微信回调验证失败
- 检查 Token 是否正确
- 检查 AESKey 是否正确
- 测试 URL 可访问性
- 检查防火墙端口
5.3 运行问题
问题 1:端口被占用
# 解决方案 1:使用其他端口
openclaw gateway start --port 18790
# 解决方案 2:杀死占用端口的进程
lsof -ti:18789 | xargs kill -9
问题 2:内存不足
export NODE_OPTIONS="--max-old-space-size=4096"
openclaw gateway start
六、实战案例
6.1 自动化代码审查
# 配置代码审查提示词
openclaw config set prompts.codeReview "请审查以下代码,重点关注:1. 安全漏洞 2. 性能问题 3. 代码规范 4. 最佳实践"
6.2 企业微信日报提醒
// 创建定时任务脚本
const schedule = require('node-schedule');
const axios = require('axios');
schedule.scheduleJob('0 17 * * *', async () => {
const message = {
msgtype: 'text',
text: { content: '📝 提醒:请在下班前提交今日工作日报!' }
};
await axios.post('http://127.0.0.1:18789/api/wework/send', message);
});
6.3 多语言翻译助手
# 配置翻译功能
openclaw config set features.translation.enabled true
openclaw config set features.translation.languages "en,zh,ja,ko"
6.4 服务器监控告警
// 创建监控脚本
const os = require('os');
const axios = require('axios');
setInterval(async () => {
const cpuUsage = os.loadavg()[0] / os.cpus().length * 100;
const memUsage = (1 - os.freemem() / os.totalmem()) * 100;
if (cpuUsage > 80 || memUsage > 85) {
const alert = {
platform: 'discord',
message: `🚨 服务器告警!CPU 使用率:${cpuUsage.toFixed(2)}%`
};
await axios.post('http://127.0.0.1:18789/api/alert', alert);
}
}, 60000);
七、最佳实践
7.1 配置管理
使用环境变量管理敏感信息:
# .env 文件
NODE_ENV=production
OPENCLAW_PORT=18789
DISCORD_TOKEN=MTIzNDU2...
WEWORK_CORP_ID=wwxxxx
7.2 安全实践
- ✅ 启用 HTTPS
- ✅ 设置强密码认证
- ✅ 定期更换密钥
- ✅ 开启日志审计
7.3 备份恢复
# 自动备份脚本
#!/bin/bash
BACKUP_DIR=~/.openclaw/backups
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR
cp ~/.openclaw/config.json $BACKUP_DIR/config_$DATE.json
八、故障排查
8.1 诊断工具
# 健康检查
openclaw health check
# 详细诊断
openclaw diagnose --verbose
# 网络测试
openclaw test connection --platform discord
8.2 日志分析
# 查看实时日志
openclaw logs --follow
# 过滤错误日志
openclaw logs --level error
8.3 常见错误码
| 错误码 | 说明 | 解决方案 |
|---|---|---|
| E001 | 配置文件错误 | 检查配置文件语法 |
| E002 | API Key 无效 | 重新获取 API Key |
| E003 | 网络连接失败 | 检查网络和防火墙 |
| E004 | 权限不足 | 检查权限配置 |
| E005 | 资源不足 | 增加资源或清理 |
总结
OpenClaw 支持多种消息渠道接入,包括 Discord、企业微信、个人微信等。部署时需注意 Node.js 版本(>=22),合理配置环境变量,并关注安全性与性能优化。通过灵活的高级配置,可实现代码审查、定时提醒、监控告警等多种自动化场景。


