twitter-cli by jackwener/twitter-cli
npx skills add https://github.com/jackwener/twitter-cli --skill twitter-cli二进制文件: twitter 凭证: 浏览器 cookies(自动提取)或环境变量
# 安装(需要 Python 3.8+)
uv tool install twitter-cli
# 或者:pipx install twitter-cli
# 升级到最新版本(建议升级以避免 API 错误)
uv tool upgrade twitter-cli
# 或者:pipx upgrade twitter-cli
对智能体重要提示:在执行任何 twitter-cli 命令之前,你必须先检查凭证是否存在。如果不存在,你必须主动引导用户完成认证过程。不要假设凭证已配置。
关键提示:写操作(发布推文、回复、引用推文)需要完整的浏览器 cookies。仅通过环境变量提供 auth_token 和 ct0 可能会导致 226 错误("看起来像自动化行为")。为了获得最佳效果,请使用浏览器 cookie 提取功能。
twitter status --yaml >/dev/null && echo "AUTH_OK" || echo "AUTH_NEEDED"
如果输出 AUTH_OK,则跳转到命令参考部分。如果输出 ,则继续引导用户:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
AUTH_NEEDED方法 A:浏览器 cookie 提取(推荐)
确保用户已登录 x.com,浏览器可以是:Arc、Chrome、Edge、Firefox、Brave 之一。twitter-cli 会自动提取 cookies。所有 Chrome 配置文件都会被自动扫描。要指定配置文件:TWITTER_CHROME_PROFILE="Profile 2" twitter feed。要优先使用特定浏览器:TWITTER_BROWSER=chrome twitter feed(支持的浏览器:arc、chrome、edge、firefox、brave)。
twitter whoami
方法 B:环境变量
export TWITTER_AUTH_TOKEN="<从浏览器获取的 auth_token>"
export TWITTER_CT0="<从浏览器获取的 ct0>"
twitter whoami
方法 C:完整的 cookie 字符串(适用于云端/远程智能体)
告诉用户:
我需要你的 Twitter 登录凭证。请按以下步骤获取:
- 用 Chrome/Edge/Firefox 打开 https://x.com(确保已登录)
- 按
F12打开开发者工具 → Network 标签- 在页面上刷新,点击任意
x.com请求- 找到 Request Headers → Cookie: 这一行,右键 → 复制值
- 把完整 Cookie 字符串发给我
⚠️ Cookie 包含登录信息,请不要分享给其他人。
然后提取并设置环境变量:
FULL_COOKIE="<用户的 cookie 字符串>"
export TWITTER_AUTH_TOKEN=$(echo "$FULL_COOKIE" | grep -oE 'auth_token=[a-f0-9]+' | cut -d= -f2)
export TWITTER_CT0=$(echo "$FULL_COOKIE" | grep -oE 'ct0=[a-f0-9]+' | cut -d= -f2)
twitter whoami
| 症状 | 智能体操作 |
|---|---|
No Twitter cookies found | 引导用户在浏览器中登录 x.com,或设置环境变量 |
| 读操作正常,写操作返回 226 | 缺少完整的 cookies — 使用浏览器 cookie 提取而不是环境变量 |
Cookie expired (401/403) | 要求用户重新登录 x.com 并重试 |
| 用户更改了密码 | 所有旧 cookies 失效 — 重新提取 |
twitter feed # 漂亮的表格输出
非 TTY 标准输出默认自动使用 YAML 格式。使用 OUTPUT=yaml|json|rich|auto 来覆盖此设置。
twitter feed --yaml
twitter feed --json | jq '.[0].text'
所有机器可读的输出都使用 SCHEMA.md 中记录的封装格式。推文和用户数据现在位于 .data 下。
--full-text 标志(仅限富表格)当用户希望在终端表格中看到完整的帖子正文时,使用 --full-text。它影响富表格列表视图,例如 feed、bookmarks、search、user-posts、likes、list 以及 tweet 中的回复表格。它不会改变 --json、--yaml 或 -c 紧凑模式的输出。
twitter feed --full-text
twitter search "AI agent" --full-text
twitter user-posts elonmusk --max 20 --full-text
twitter tweet 1234567890 --full-text
-c 标志(为 LLM 提供最少的标记)twitter -c feed --max 10 # 最少的字段,非常适合 LLM 上下文
twitter -c search "AI" --max 20 # 比 --json 少约 80% 的标记
紧凑模式字段(每条推文): id、author (@句柄)、text(截断至 140 字符)、likes、rts、time(短格式)
twitter status # 快速认证检查
twitter status --yaml # 结构化的认证状态
twitter whoami # 当前认证用户
twitter whoami --yaml # YAML 输出
twitter whoami --json # JSON 输出
twitter user elonmusk # 用户资料
twitter user elonmusk --json # JSON 输出
twitter feed # 首页时间线(为你推荐)
twitter feed -t following # 关注时间线
twitter feed --max 50 # 限制数量
twitter feed --full-text # 在表格中显示完整的帖子正文
twitter feed --filter # 启用排名过滤器
twitter feed --yaml > tweets.yaml # 导出为 YAML
twitter feed --input tweets.json # 从本地 JSON 文件读取
twitter bookmarks # 已收藏的推文
twitter bookmarks --full-text # 收藏夹表格中的完整文本
twitter bookmarks --max 30 --yaml
twitter search "keyword" # 搜索推文
twitter search "AI agent" -t Latest --max 50
twitter search "AI agent" --full-text # 搜索结果中的完整文本
twitter search "topic" -o results.json # 保存到文件
twitter tweet 1234567890 # 推文详情 + 回复
twitter tweet 1234567890 --full-text # 回复表格中的完整文本
twitter tweet https://x.com/user/status/12345 # 接受 URL
twitter show 2 # 打开上次 feed/search 列表中的第 2 条推文
twitter show 2 --full-text # 回复表格中的完整文本
twitter show 2 --json # 结构化输出
twitter list 1539453138322673664 # 列表时间线
twitter list 1539453138322673664 --full-text
twitter user-posts elonmusk --max 20 # 用户的推文
twitter user-posts elonmusk --full-text
twitter likes elonmusk --max 30 # 用户的点赞(仅限自己的账户,见备注)
twitter likes elonmusk --full-text
twitter followers elonmusk --max 50 # 关注者
twitter following elonmusk --max 50 # 正在关注
twitter post "Hello from twitter-cli!" # 发布推文
twitter post "Hello!" --image photo.jpg # 发布带图片的推文
twitter post "Gallery" -i a.png -i b.jpg # 最多 4 张图片
twitter reply 1234567890 "Great tweet!" # 回复(独立命令)
twitter reply 1234567890 "Nice!" -i pic.png # 带图片的回复
twitter post "reply text" --reply-to 1234567890 # 回复(通过 post 命令)
twitter quote 1234567890 "Interesting take" # 引用推文
twitter quote 1234567890 "Look" -i chart.png # 带图片的引用推文
twitter delete 1234567890 # 删除推文
twitter like 1234567890 # 点赞
twitter unlike 1234567890 # 取消点赞
twitter retweet 1234567890 # 转推
twitter unretweet 1234567890 # 取消转推
twitter bookmark 1234567890 # 收藏
twitter unbookmark 1234567890 # 取消收藏
twitter follow elonmusk # 关注用户
twitter unfollow elonmusk # 取消关注用户
图片上传注意事项:
--image / -i(可重复使用)twitter post "My tweet text" 2>/dev/null
# 输出包含推文 URL:🔗 https://x.com/i/status/<id>
# 单张图片
twitter post "Check this out!" --image /path/to/photo.jpg
# 多张图片
twitter post "Photo gallery" -i img1.png -i img2.jpg -i img3.webp
TWEET_ID=$(twitter user-posts targetuser --max 1 --json | jq -r '.data[0].id')
twitter reply "$TWEET_ID" "Nice post!"
# 发布第一条推文,捕获输出以获取推文 ID
twitter post "Thread 1/3: First point"
# 从输出中记下推文 ID,然后:
twitter reply <first_tweet_id> "2/3: Second point"
twitter reply <second_tweet_id> "3/3: Final point"
TWEET_ID=$(twitter search "interesting topic" --max 1 --json | jq -r '.data[0].id')
twitter quote "$TWEET_ID" "This is a great insight!"
twitter search "interesting topic" --max 5 --json | jq -r '.data[].id' | while read id; do
twitter like "$id"
done
twitter user targethandle --json | jq '.data | {username, followers, bio}'
twitter follow targethandle
twitter user-posts elonmusk --max 20 --json | jq '.data | sort_by(.metrics.likes) | reverse | .[:3] | .[] | {id, text: .text[:80], likes: .metrics.likes}'
MY_NAME=$(twitter whoami --json | jq -r '.data.user.username')
twitter followers "$MY_NAME" --max 200 --json | jq -r '.data[].username' | grep -q "targetuser" && echo "Yes" || echo "No"
# 紧凑模式,用于节省标记的 LLM 上下文
twitter -c feed -t following --max 30
twitter -c bookmarks --max 20
# 富表格,包含完整的帖子正文
twitter feed -t following --max 20 --full-text
twitter search "AI agent" --max 20 --full-text
# 用于分析的完整 JSON
twitter feed -t following --max 30 -o following.json
twitter bookmarks --max 20 -o bookmarks.json
# 点赞数 > 100 的推文
twitter search "AI safety" --max 20 --json | jq '[.data[] | select(.metrics.likes > 100)]'
# 仅提取文本和作者
twitter search "rust lang" --max 10 --json | jq '.data[] | {author: .author.screenName, text: .text[:100]}'
# 互动最多的推文
twitter search "topic" --max 20 --json | jq '.data | sort_by(.metrics.likes) | reverse | .[:5] | .[].id'
过滤功能是可选的。使用 --filter 启用:
twitter feed --filter
twitter bookmarks --filter
| 错误 | 原因 | 解决方法 |
|---|---|---|
No Twitter cookies found | 未认证 | 在浏览器中登录 x.com,或设置环境变量 |
| HTTP 226 | 自动化检测 | 使用浏览器 cookie 提取(而不是环境变量) |
| HTTP 401/403 | Cookie 过期 | 重新登录 x.com 并重试 |
| HTTP 404 | QueryId 轮换 | 重试(内置自动回退机制) |
| HTTP 429 | 速率限制 | 等待 15 分钟以上,然后重试 |
| Error 187 | 重复推文 | 更改文本内容 |
| Error 186 | 推文过长 | 保持在 280 字符以内 |
twitter likes 仅适用于你自己的账户每周安装量
204
代码仓库
GitHub 星标数
2.1K
首次出现
2026年3月9日
安全审计
安装于
opencode192
codex191
gemini-cli190
cursor190
github-copilot190
cline190
通过 LiteLLM 代理让 Claude Code 对接 GitHub Copilot 运行 | 高级变通方案指南
31,600 周安装