背景
之前博客发布后需要手动复制标题和链接到 X(Twitter),步骤繁琐。折腾过几个方案:
HTTP Shortcuts + Python 服务器(太复杂)快捷指令(iOS 限制多)
最终选择了 手机 APK + ADB 自动化 方案,简单可靠。
架构设计
┌─────────────┐ HTTP ┌─────────────┐
│ Mac Mini │ ──────────────► │ 手机 APK │
│ (OpenClaw) │ │ (博客同步) │
└─────────────┘ └──────┬──────┘
│ │
│ ADB │ 打开 X
▼ ▼
┌─────────────┐ ┌─────────────┐
│ 发帖按钮 │ ◄────────────── │ X App │
│ (自动点击) │ Intent │ 分享页面 │
└─────────────┘ └─────────────┘
核心思路:
- Mac 发送 HTTP 请求到手机 APK
- APK 收到请求后打开 X 分享页面(预填充标题 + 链接)
- Mac 通过 ADB 自动点击"发帖"按钮
- 完成发布
目录结构
skills/blog-x-sync/
├── android-app/ # Android 源码(Gradle 项目)
├── blog-sync.apk # 编译好的 APK(5.1MB)
├── scripts/
│ └── post-to-x.sh # 自动发布脚本
└── SKILL.md # 技能文档
手机 APK
技术栈:
- 语言:Kotlin
- 最低 Android:5.0 (API 21)
- 功能:HTTP 服务器(端口 8080)
核心代码:
// 收到 HTTP 请求后打开 X 分享页面
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse("https://twitter.com/intent/tweet?text=$title $url")
setPackage("com.twitter.android")
}
startActivity(intent)
安装:
adb install skills/blog-x-sync/blog-sync.apk
自动发布脚本
post-to-x.sh 完整流程:
#!/bin/bash
PHONE_IP="10.10.10.211"
# 1. 关闭通知栏(避免遮挡)
adb shell input swipe 540 2200 540 200
# 2. 打开 X 发帖界面
adb shell am start -a android.intent.action.VIEW \
-d "https://twitter.com/intent/tweet?text=$TITLE%20$URL"
# 3. 点击发帖按钮(坐标 952,180)
adb shell input tap 952 180
# 4. 清理残留
adb shell am force-stop com.twitter.android
# 5. 打开 X 主页查看
adb shell monkey -p com.twitter.android \
-c android.intent.category.LAUNCHER 1
按钮坐标适配:
- 手机分辨率:1080x2400
- 发帖按钮位置:右上角
- 点击坐标:
952, 180
使用方法
方式 1:直接调用脚本
skills/blog-x-sync/scripts/post-to-x.sh "博客标题" "https://liuhp.net/post/xxx/"
方式 2:集成到 hugo-blog
cd skills/hugo-blog
node scripts/hugo-blog.mjs publish --x
--x 参数会自动调用 post-to-x.sh。
要求
| 项目 | 要求 |
|---|---|
| 网络 | 手机和 Mac 同一网络(10.10.10.x) |
| ADB | 手机开启 USB 调试 |
| APK | 保持运行(不要强制停止) |
| X App | 已登录账号 |
故障排查
1. curl 超时
# 检查手机 IP 是否正确
ping 10.10.10.211
# 检查 APK 是否运行
adb shell ps | grep blogsync
2. 按钮点不到
- 确认手机分辨率
- 用
adb shell input tap x y测试坐标 - 调整
post-to-x.sh中的坐标值
3. X 打不开
# 检查 X 是否安装
adb shell pm list packages | grep twitter
# 手动测试打开
adb shell am start -n com.twitter.android/.HomeActivity
替代方案对比
| 方案 | 优点 | 缺点 |
|---|---|---|
| APK+ADB | 简单可靠,无需 API | 需要手机常连 |
| Twitter API | 纯云端 | 需要 API Key,限流 |
| 快捷指令 | iOS 原生 | 自动化限制多 |
| Python 服务器 | 灵活 | 复杂,需维护 |
最终选择 APK+ADB 是因为:简单就是稳定。
源码
- 技能目录:
~/.openclaw/workspace/skills/blog-x-sync/ - APK 源码:
skills/blog-x-sync/android-app/ - 发布脚本:
skills/blog-x-sync/scripts/post-to-x.sh
总结
这个技能的核心是 用手机的 Android 系统作为桥梁,绕过 Twitter API 的限制。
优势:
- ✅ 无需 API Key
- ✅ 无发布频率限制
- ✅ 账号安全(官方 App)
- ✅ 代码简单(<200 行)
代价:
- ⚠️ 手机需要常连网络
- ⚠️ 依赖 ADB 调试
- ⚠️ 手机型号变更需调整坐标
但对于个人博客来说,这个 trade-off 完全值得。
2026-03-01 于 四川广元