🚀 终极方案:OpenClaw + Cloudflare Tunnel 企业微信机器人部署指南
版本:Final Edition (2026.02) 方案特点:无需公网IP、零端口开放、Cloudflare 全球加速、配置极简。 适用环境:任何能联网的 Mac/Windows/Linux 设备(即便是光猫后的多层内网)。
🏗️ 架构对比
- 旧时代 (FRP):
企业微信->公网服务器(frps)->路由器端口映射->本机(frpc)->OpenClaw(链路长,配置繁琐,需公网IP) - 新时代 (CF Tunnel):
企业微信->Cloudflare边缘节点->加密隧道->本机(cloudflared)->OpenClaw(链路短,无需公网IP,起飞!)
🛠️ 第一步:OpenClaw 基础环境搭建
(如果你之前已经配好了 OpenClaw,可以跳过安装,直接检查配置文件。)
1. 安装核心
npm install -g openclaw
openclaw plugins install openclaw-plugin-wecom
2. 编写/检查配置文件 (关键!)
文件路径:~/.openclaw/openclaw.json
⚠️ 避坑重点:
- 必须显式启用插件
plugins.entries。 encodingAesKey必须是小驼峰命名(企业微信后台复制过来要把 Key 名改对)。- 在 Tunnel 模式下,OpenClaw 只需要监听本地即可,但也建议放开局域网权限方便调试。
{
"plugins": {
"entries": {
"wecom": { "enabled": true }
}
},
"channels": {
"wecom": {
"enabled": true,
"token": "你的Token", // 从企业微信后台获取
"encodingAesKey": "你的43位Key" // 从企业微信后台获取
}
},
"gateway": {
"bind": "lan", // 建议设为 lan,方便调试;Tunnel 访问 localhost 也没问题
"port": 18789
}
}
启动网关:
openclaw gateway restart
🚇 第二步:部署 Cloudflare Tunnel (重头戏)
前提:你需要有一个域名托管在 Cloudflare 上(比如 shuqi.run)。
1. 安装 cloudflared
Mac 用户直接用 Homebrew:
brew install cloudflared
cloudflared tunnel run --url http://localhost:18789
2. 登录并授权
cloudflared tunnel login
终端会给出一个 URL,在浏览器打开,选择你的域名进行授权。
3. 创建隧道
给隧道起个名字,比如 bot-tunnel:
cloudflared tunnel create bot-tunnel
成功后会返回一个 Tunnel ID,记下来。
4. 绑定域名 (DNS 解析)
把你想要的子域名(例如 bot.shuqi.run)绑定到这个隧道:
# 语法: cloudflared tunnel route dns [隧道名] [你的域名]
cloudflared tunnel route dns bot-tunnel bot.shuqi.run
5. 配置流量转发
创建配置文件 ~/.cloudflared/config.yml:
nano ~/.cloudflared/config.yml
写入以下内容(注意替换 Tunnel ID):
tunnel: <你的-Tunnel-ID-xxxxxxxx>
credentials-file: /Users/你的用户名/.cloudflared/<你的-Tunnel-ID>.json
ingress:
# 将 bot.shuqi.run 的流量转发给本地 OpenClaw 端口
- hostname: bot.shuqi.run
service: http://localhost:18789
# 兜底规则(必须有)
- service: http_status:404
6. 启动隧道
cloudflared tunnel run bot-tunnel
看到类似 Connection ... registered 的日志,说明隧道已打通!此时全球任何地方访问 https://bot.shuqi.run 都会直接连到你 Mac 上的 OpenClaw。
📱 第三步:企业微信后台最终对接
回到 企业微信管理后台,进入 应用管理 -> 自建应用 -> API接收消息。
填写参数
- URL:
https://bot.shuqi.run/wecom- 注意:CF Tunnel 自动提供 HTTPS,这里直接填 HTTPS 即可,完美符合企业微信安全性要求。
- 注意:路径必须是
/wecom。
- Token: 与
openclaw.json一致。 - EncodingAESKey: 与
openclaw.json一致。
点击保存
- Cloudflare Tunnel 速度极快,通常会秒过验证。
- 如果提示校验成功,去企业微信 App 发送“你好”测试。
⚡ 进阶技巧:让隧道后台运行
调试没问题后,肯定不想一直开着终端窗口。可以让 cloudflared 变成后台服务自动运行。
# 安装系统服务
sudo cloudflared service install
# 启动服务
sudo cloudflared service start
这样你的 Mac 只要开机,隧道就自动连通,机器人 24 小时在线。
🏆 总结
使用 Cloudflare Tunnel 后,我们彻底告别了: ❌ 繁琐的路由器端口映射 ❌ 昂贵的公网服务器月租 ❌ 令人头秃的防火墙配置 ❌ Nginx 反向代理配置
这就是目前最优雅、最高效的企业微信机器人部署方案。