Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,31 @@ on:
pull_request:
workflow_call:

permissions:
contents: read

concurrency:
group: ci-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
test:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 20
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12", "3.13"]
steps:
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Setup uv
uses: astral-sh/setup-uv@v6
with:
enable-cache: true

- name: Setup Python
uses: actions/setup-python@v5
Expand All @@ -26,12 +39,16 @@ jobs:

- name: Install dependencies
run: uv sync --extra dev
timeout-minutes: 10

- name: Lint
run: uv run ruff check .
timeout-minutes: 5

- name: Type check
run: uv run mypy twitter_cli
timeout-minutes: 5

- name: Test
run: uv run pytest -q
timeout-minutes: 10
221 changes: 162 additions & 59 deletions README.md

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions SCHEMA.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,22 @@ error:
- `--yaml` and `--json` both use this envelope
- non-TTY stdout defaults to YAML
- tweet and user lists are returned under `data`
- timeline-style list commands may also return `pagination.nextCursor`
- timeline-style list commands (`feed`, `bookmarks`, `list`, `tweet`) may also return `pagination.nextCursor`
- `article` returns a single tweet object under `data`
- `news` returns a list of Explore News story objects (`rank`, `title`, `name`, `category`, `context`, `query`, `url`, `post_count`, `trend_id`, `is_ai_trend`, `type`) under `data`
- `doctor` returns `data.checks` (metadata only — never emits cookies or tokens)
- `status` returns `data.authenticated` plus `data.user`
- `whoami` returns `data.user`
- write commands also support explicit `--json` / `--yaml`
- write commands (`post`, `reply`, `quote`, `article-publish`, etc.) also support explicit `--json` / `--yaml`

## Tweet & Media Fields

Each tweet object in `data` includes:

- `author`: `id`, `name`, `screenName`, `profileImageUrl`, `verified`, `followers`, `following`, plus optional `viewerFollowing` / `viewerFollowedBy` (when present in upstream payload)
- `possiblySensitive`: boolean flag from the tweet payload
- Thread metadata (included when present): `conversationId`, `inReplyToStatusId`, `inReplyToUserId`, `inReplyToScreenName`
- `media[]`: `type`, `url`, `width`, `height`, `adultContent`, `graphicViolence`, `otherWarning`, plus optional `posterUrl`, `altText`, `durationMs`

## Article Fields

Expand All @@ -54,4 +65,7 @@ Common structured error codes:
- `not_found`
- `invalid_input`
- `rate_limited`
- `network_error`
- `query_id_error`
- `media_upload_error`
- `api_error`
80 changes: 44 additions & 36 deletions SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: twitter-cli
description: Use twitter-cli for ALL Twitter/X operations — reading tweets, posting, replying, quoting, liking, retweeting, following, searching, user lookups. Invoke whenever user requests any Twitter interaction.
author: jackwener
version: "0.8.0"
version: "0.8.6"
tags:
- twitter
- x
Expand All @@ -19,7 +19,7 @@ tags:
## Setup

```bash
# Install (requires Python 3.8+)
# Install (requires Python 3.10+)
uv tool install twitter-cli
# Or: pipx install twitter-cli

Expand All @@ -32,7 +32,7 @@ uv tool upgrade twitter-cli

**IMPORTANT FOR AGENTS**: Before executing ANY twitter-cli command, you MUST first check if credentials exist. If not, you MUST proactively guide the user through the authentication process. Do NOT assume credentials are configured.

**CRITICAL**: Write operations (posting tweets, replying, quoting) REQUIRE full browser cookies. Only providing `auth_token` + `ct0` via env vars may result in **226 error** ("looks like automated behavior"). For best results, use browser cookie extraction.
**CRITICAL**: Write operations (posting tweets, replying, quoting) REQUIRE full browser cookies. Only providing `auth_token` + `ct0` via env vars may result in **226 error** ("looks like automated behavior"). For best results, use browser cookie extraction or `TWITTER_COOKIE_STRING` (full `Cookie:` header).

### Step 0: Check if already authenticated

Expand All @@ -47,9 +47,10 @@ If `AUTH_NEEDED`, proceed to guide the user:

**Method A: Browser cookie extraction (recommended)**

Ensure user is logged into x.com in one of: Arc, Chrome, Edge, Firefox, Brave. twitter-cli auto-extracts cookies.
Ensure user is logged into x.com in one of: Arc, Chrome, Edge, Firefox, Brave, Vivaldi, Zen. twitter-cli auto-extracts cookies.
All Chrome profiles are scanned automatically. To specify a profile: `TWITTER_CHROME_PROFILE="Profile 2" twitter feed`.
To prioritize a specific browser: `TWITTER_BROWSER=chrome twitter feed` (supported: arc, chrome, edge, firefox, brave).
To point to a custom Chromium user data directory (e.g., Chromium, Opera, portable Chrome): `TWITTER_CHROMIUM_USER_DATA_DIR="/path/to/User Data" twitter feed`.
To prioritize a specific browser: `TWITTER_BROWSER=chrome twitter feed` (supported: arc, chrome, edge, firefox, brave, vivaldi, zen).

```bash
twitter whoami
Expand All @@ -63,35 +64,31 @@ export TWITTER_CT0="<ct0 from browser>"
twitter whoami
```

**Method C: Full cookie string (for cloud/remote agents)**
**Method C: Full cookie string (recommended for cloud/remote agents & Windows Chrome >=127)**

Tell the user:
Pass the full browser `Cookie:` header via `TWITTER_COOKIE_STRING` (preserves all session/anti-bot cookies and auto-extracts `auth_token` + `ct0`):

```bash
export TWITTER_COOKIE_STRING="auth_token=...; ct0=...; twid=..."
twitter whoami
```

If asking the user to copy it:

> 我需要你的 Twitter 登录凭证。请按以下步骤获取:
>
> 1. 用 Chrome/Edge/Firefox 打开 https://x.com(确保已登录)
> 2. 按 `F12` 打开开发者工具 → **Network** 标签
> 3. 在页面上刷新,点击任意 `x.com` 请求
> 4. 找到 **Request Headers** → **Cookie:** 这一行,右键 → 复制值
> 5. 把完整 Cookie 字符串发给我
>
> ⚠️ Cookie 包含登录信息,请不要分享给其他人。

Then extract and set env vars:

```bash
FULL_COOKIE="<user's cookie string>"
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
```
> 5. 把完整 Cookie 字符串设置为 `TWITTER_COOKIE_STRING` 环境变量(⚠️ Cookie 包含登录信息,请不要分享给其他人)。

### Step 2: Handle common auth issues

| Symptom | Agent action |
|---------|-------------|
| `No Twitter cookies found` | Guide user to login to x.com in browser, or set env vars |
| Read works, write returns 226 | Full cookies missing — use browser cookie extraction instead of env vars |
| Read works, write returns 226 | Full cookies missing — use browser cookie extraction or `TWITTER_COOKIE_STRING` instead of minimal `auth_token`/`ct0` env vars |
| `Cookie expired (401/403)` | Ask user to re-login to x.com and retry |
| User changed password | All old cookies invalidated — re-extract |

Expand All @@ -109,7 +106,7 @@ Non-TTY stdout defaults to YAML automatically. Use `OUTPUT=yaml|json|rich|auto`

```bash
twitter feed --yaml
twitter feed --json | jq '.[0].text'
twitter feed --json | jq '.data[0].text'
```

All machine-readable output uses the envelope documented in [SCHEMA.md](./SCHEMA.md).
Expand Down Expand Up @@ -144,31 +141,38 @@ twitter -c search "AI" --max 20 # ~80% fewer tokens than --json
```bash
twitter status # Quick auth check
twitter status --yaml # Structured auth status
twitter doctor # Diagnostics (deps, config, auth presence, caches)
twitter doctor --refresh # Refresh cached GraphQL query IDs
twitter whoami # Current authenticated user
twitter whoami --yaml # YAML output
twitter whoami --json # JSON output
twitter user elonmusk # User profile
twitter user elonmusk --json # JSON output
twitter user elonmusk # User profile (screen name or numeric rest_id)
twitter user 44196397 --json # Lookup by numeric user ID
twitter feed # Home timeline (For You)
twitter feed -t following # Following timeline
twitter feed --max 50 # Limit count
twitter feed --full-text # Show full post body in table
twitter --time iso feed # Timestamp display: relative | absolute | iso
twitter feed --filter # Enable ranking filter
twitter feed --yaml > tweets.yaml # Export as YAML
twitter feed --input tweets.json # Read from local JSON file
twitter bookmarks # Bookmarked tweets
twitter bookmarks --cursor "<cursor>" # Paginate bookmarks
twitter bookmarks --full-text # Full text in bookmarks table
twitter bookmarks --max 30 --yaml
twitter news --max 20 # Explore > News personalized stories
twitter search "keyword" # Search tweets
twitter search "AI agent" -t Latest --max 50
twitter search "AI agent" --full-text # Full text in search results
twitter search "topic" -o results.json # Save to file
twitter tweet 1234567890 # Tweet detail + replies
twitter tweet 1234567890 --cursor "<cursor>" # Paginate replies
twitter tweet 1234567890 --full-text # Full text in reply table
twitter tweet https://x.com/user/status/12345 # Accepts URL
twitter show 2 # Open tweet #2 from last feed/search list
twitter show 2 --full-text # Full text in reply table
twitter show 2 --json # Structured output
twitter article 1234567890 --markdown # Fetch Twitter Article as Markdown
twitter list 1539453138322673664 # List timeline
twitter list 1539453138322673664 --cursor "<next-cursor>"
twitter list 1539453138322673664 --full-text
Expand All @@ -178,19 +182,24 @@ twitter likes elonmusk --max 30 # User's likes (own only, see note)
twitter likes elonmusk --full-text
twitter followers elonmusk --max 50 # Followers
twitter following elonmusk --max 50 # Following
twitter completion zsh # Print shell completion (bash/zsh/fish)
```

### Write Operations

```bash
twitter post "Hello from twitter-cli!" # Post tweet
twitter post "Hello from twitter-cli!" # Post tweet (auto-uses CreateNoteTweet for >280 chars on Premium)
twitter post "Hello!" --image photo.jpg # Post with image
twitter post "Gallery" -i a.png -i b.jpg # Up to 4 images
twitter post "Demo clip" --video clip.mp4 # Post with video (mp4/mov/webm up to 512 MB)
twitter reply 1234567890 "Great tweet!" # Reply (standalone)
twitter reply 1234567890 "Nice!" -i pic.png # Reply with image
twitter reply 1234567890 "Clip" -V clip.mp4 # Reply with video
twitter post "reply text" --reply-to 1234567890 # Reply (via post)
twitter quote 1234567890 "Interesting take" # Quote-tweet
twitter quote 1234567890 "Look" -i chart.png # Quote with image
twitter quote 1234567890 "Clip" -V clip.mp4 # Quote with video
twitter article-publish post.md --title "My Post" # Publish X Article from Markdown (--draft for draft)
twitter delete 1234567890 # Delete tweet
twitter like 1234567890 # Like
twitter unlike 1234567890 # Unlike
Expand All @@ -202,11 +211,9 @@ twitter follow elonmusk # Follow user
twitter unfollow elonmusk # Unfollow user
```

**Image upload notes:**
- Supported formats: JPEG, PNG, GIF, WebP
- Max file size: 5 MB per image
- Max 4 images per tweet
- Use `--image` / `-i` (repeatable)
**Media upload notes:**
- Images: JPEG, PNG, GIF, WebP — max 5 MB per image, up to 4 images per tweet (`--image` / `-i`)
- Video: MP4, MOV, WebM — max 512 MB, 1 video per tweet (`--video` / `-V`, chunked upload + async processing poll); cannot mix images and video in the same tweet

## Agent Workflows

Expand Down Expand Up @@ -262,7 +269,7 @@ done
### Get user info then follow

```bash
twitter user targethandle --json | jq '.data | {username, followers, bio}'
twitter user targethandle --json | jq '.data | {screenName, followers, bio}'
twitter follow targethandle
```

Expand All @@ -275,8 +282,8 @@ twitter user-posts elonmusk --max 20 --json | jq '.data | sort_by(.metrics.likes
### Check follower relationship

```bash
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"
MY_NAME=$(twitter whoami --json | jq -r '.data.user.screenName')
twitter followers "$MY_NAME" --max 200 --json | jq -r '.data[].screenName' | grep -q "targetuser" && echo "Yes" || echo "No"
```

### Daily reading workflow
Expand Down Expand Up @@ -322,25 +329,26 @@ twitter bookmarks --filter
| Error | Cause | Fix |
|-------|-------|-----|
| `No Twitter cookies found` | Not authenticated | Login to x.com in browser, or set env vars |
| HTTP 226 | Automated detection | Use browser cookie extraction (not env vars) |
| HTTP 226 | Automated detection | Use browser cookie extraction or full `TWITTER_COOKIE_STRING` |
| HTTP 401/403 | Cookie expired | Re-login to x.com and retry |
| HTTP 404 | QueryId rotation | Retry (auto-fallback built in) |
| HTTP 404 | QueryId rotation | Retry (auto-fallback built in) or run `twitter doctor --refresh` |
| HTTP 429 | Rate limited | Wait 15+ minutes, then retry |
| Error 187 | Duplicate tweet | Change text content |
| Error 186 | Tweet too long | Keep under 280 chars |
| Error 186 | Tweet too long | Keep under 280 weighted chars (or use X Premium, which auto-routes via `CreateNoteTweet`) |

## Limitations

- **Images only** — video/GIF animation upload not yet supported (image upload supports JPEG/PNG/GIF/WebP)
- **No DMs** — no direct messaging
- **No notifications** — can't read notifications
- **No polls** — can't create polls
- **Single account** — one set of credentials at a time
- **Likes are private** — Twitter/X made all likes private since June 2024. `twitter likes` only works for your own account
- **Long-form posts & Articles** — posts > 280 weighted chars automatically use `CreateNoteTweet` and `article-publish` publishes X Articles (both require an X Premium account)

## Safety Notes

- Write operations have built-in random delays (1.5–4s) to avoid rate limits.
- GraphQL write (`POST`) requests fail closed if `x-client-transaction-id` cannot be generated, preventing unsigned-write 226 flags (override only for debugging with `TWITTER_ALLOW_UNSIGNED=1`).
- TLS fingerprint and User-Agent are automatically matched to the Chrome version used.
- Do not ask users to share raw cookie values in chat logs.
- Prefer local browser cookie extraction over manual secret copy/paste.
Expand Down
5 changes: 4 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
fetch:
count: 50

display:
time: relative # relative | absolute | iso (overridable with --time)

filter:
mode: "topN"
topN: 20
Expand All @@ -18,4 +21,4 @@ rateLimit:
requestDelay: 1.5 # seconds between paginated requests
maxRetries: 3 # retry count on 429 / rate-limit errors
retryBaseDelay: 5.0 # base delay for exponential backoff (seconds)
maxCount: 200 # hard cap for single fetch
maxCount: 200 # default cap for a single fetch; explicit --max on user lists may exceed it
13 changes: 8 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ dependencies = [
"click>=8.0",
"rich>=13.0",
"PyYAML>=6.0",
"curl_cffi>=0.7",
"xclienttransaction>=1.0.1",
# >=0.15.0 is required: earlier releases are affected by
# GHSA-qw2m-4pqf-rmpp (CVE-2026-33752), a redirect-based SSRF.
"curl_cffi>=0.15.0",
# >=1.0.3 is required for X's numeric webpack module map for ondemand.s.
"xclienttransaction>=1.0.3",
"beautifulsoup4>=4.12",
]

Expand All @@ -38,9 +41,9 @@ dev = [
]

[project.urls]
Homepage = "https://github.com/jackwener/twitter-cli"
Repository = "https://github.com/jackwener/twitter-cli"
Issues = "https://github.com/jackwener/twitter-cli/issues"
Homepage = "https://github.com/public-clis/twitter-cli"
Repository = "https://github.com/public-clis/twitter-cli"
Issues = "https://github.com/public-clis/twitter-cli/issues"

[project.scripts]
twitter = "twitter_cli.cli:cli"
Expand Down
27 changes: 27 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@
os.environ.setdefault("OUTPUT", "rich")


@pytest.fixture(autouse=True)
def _isolate_env_and_caches(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
"""Prevent tests from reading/writing host ~/.twitter-cli caches or host TWITTER_* env vars."""
from twitter_cli import cache as cache_mod
from twitter_cli import client as client_mod
from twitter_cli import graphql as graphql_mod

for key in list(os.environ):
if key.startswith("TWITTER_") or key == "XDG_CONFIG_HOME":
monkeypatch.delenv(key, raising=False)

cache_dir = tmp_path / ".twitter-cli-test-cache"
monkeypatch.setattr(cache_mod, "_CACHE_DIR", cache_dir)
monkeypatch.setattr(cache_mod, "_CACHE_FILE", cache_dir / "last_results.json")
monkeypatch.setattr(graphql_mod, "_qid_cache_path", lambda: cache_dir / "query_ids_cache.json")
monkeypatch.setattr(
client_mod.TwitterClient,
"_ct_cache_path",
staticmethod(lambda: str(cache_dir / "transaction_cache.json")),
)


@pytest.fixture()
def tweet_factory():
def _make_tweet(tweet_id: str = "1", **overrides: Any) -> Tweet:
Expand Down Expand Up @@ -40,6 +62,11 @@ def _make_tweet(tweet_id: str = "1", **overrides: Any) -> Tweet:
article_text=overrides.pop("article_text", None),
is_subscriber_only=overrides.pop("is_subscriber_only", False),
is_promoted=overrides.pop("is_promoted", False),
possibly_sensitive=overrides.pop("possibly_sensitive", False),
conversation_id=overrides.pop("conversation_id", None),
in_reply_to_status_id=overrides.pop("in_reply_to_status_id", None),
in_reply_to_user_id=overrides.pop("in_reply_to_user_id", None),
in_reply_to_screen_name=overrides.pop("in_reply_to_screen_name", None),
)

return _make_tweet
Expand Down
Loading
Loading