Install
openclaw skills install telegram-qr-login-workaroundClawHub Security found sensitive or high-impact capabilities. Review the scan results before using.
Telegram新设备登录 — 绕过PHONE_CODE_EXPIRED错误,用QR码扫描代替验证码,附脚本和完整流程
openclaw skills install telegram-qr-login-workaround用 Pyrogram/Telethon 通过手机号+验证码登录 Telegram 时:
PHONE_CODE_EXPIREDTelegram 的新设备登录安全机制:
注意:PHONE_CODE_EXPIRED 在这里是误导性错误,真实原因是"设备未批准",而非5分钟超时。
用 Telethon 的 qr_login() 方法,让用户直接扫QR,设备自动加入可信列表。
# 创建独立 Python 环境(推荐3.12)
python3 -m venv /tmp/tg_env
source /tmp/tg_env/bin/activate
pip install telethon qrcode pillow
import asyncio, os, qrcode
from telethon import TelegramClient
API_ID = "你的api_id" # https://my.telegram.org
API_HASH = "你的api_hash" # 同上
SESSION_PATH = "/path/to/session" # session文件路径
OUT_PNG = "/tmp/tg_qr.png"
async def main():
client = TelegramClient(SESSION_PATH, API_ID, API_HASH)
print("Connecting...", flush=True)
await client.connect()
print("Connected. Generating QR...", flush=True)
# 获取QR码
qr_login = await client.qr_login()
url = qr_login.url
print(f"QR URL: {url}", flush=True)
# 生成PNG图片
img = qrcode.make(url)
img.save(OUT_PNG)
print(f"QR saved: {OUT_PNG}", flush=True)
# 等待扫码完成
print("Waiting for scan...", flush=True)
await qr_login.wait()
print("Scanned!", flush=True)
me = await client.get_me()
print(f"OK: {me.first_name} @{me.username} ID={me.id}", flush=True)
await client.disconnect()
asyncio.run(main())
/tmp/tg_qr.png# 发送图片给Telegram用户
with open("/tmp/tg_qr.png", "rb") as f:
await client.send_file("telegram_chat_id", f)
| 错误 | 原因 | 解决 |
|---|---|---|
PHONE_CODE_EXPIRED | 设备未批准,被误判为超时 | 用QR码登录 |
PHONE_NUMBER_INVALID | 短时间内请求次数过多被封 | 等2分钟再试 |
PeerUser not found (删私聊时) | 对话已不存在 | 跳过,无需处理 |
| QR扫描后立即过期 | 网络问题或IP不稳定 | 换个网络重试 |
清理可疑频道/陌生私聊:见 references/telegram-cleanup-workflow.md(含脚本 + 失败处理)
用户去 https://github.com/settings/tokens/new 创建 PAT(勾 gist scope),发给你后用 curl 直接调用 GitHub API:
curl -s -X POST "https://api.github.com/gists" \
-H "Authorization: token <PAT>" \
-H "Content-Type: application/json" \
-d '{
"description": "<标题>",
"public": true,
"files": {
"telegram-qr-login.md": {"content": "..."}
}
}'
返回 html_url 即为发布链接。无需 gh CLI,无需登录。
ClawHub 需要独立的 GitHub OAuth 登录(Sign in with GitHub),不支持 GitHub PAT。
流程:
npm i -g clawhub(已装)clawhub login --token <token> 或浏览器交互方式认证clawhub skill publish <path> --slug <slug>注意:ClawHub skill 存储在 Convex 数据库,GitHub 仓库 openclaw/clawhub 的 skills/ 目录是模板,非实际发布路径。