@higuchan123/twitter_lib
v1.0.5
Published
Node.js library for interacting with X web endpoints and X Chat flows.
Readme
twitter_lib.js
twitter_lib.js is a JavaScript library for automating X (Twitter) web actions.twitter_lib.js は、X (Twitter) のWeb操作を自動化する JavaScript ライブラリです。
26/03/18 update
- Updated the X-Client-Transaction-ID generation logic to reflect recent changes.
- X-Client-Transaction-idの生成ロジックが変更されたのでそれに伴いアップデート
Latest X Chat/DM traffic is fully encrypted and not easy to reverse engineer, but this library provides a full reverse-engineered implementation with curl-cffi/fetch only, so encrypted DM/Chat can be operated easily from the library.
X の最新 Chat/DM の通信はすべて暗号化されておりリバースエンジニアリングは容易ではありませんが、このライブラリは curl-cffi/fetch のみでフルリバースエンジニアリング実装されており、暗号化された DM/Chat をライブラリから簡単に操作できます。
This is a full-request library built with curl-cffi/fetch only, and it does not use browser automation tools such as Playwright.
これは curl-cffi/fetch only で構成されたフルリクエストライブラリで、Playwright などのブラウザ自動操作ツールは使用していません。
Installation / インストール
npm i @higuchan123/twitter_libInstall from npm with the command above.
上記コマンドで npm からインストールできます。
What This Library Can Do / このライブラリでできること
Encrypted X Chat and DM Operations / 暗号化X Chat・DM操作
All of the following are supported even for encrypted DMs.
以下はすべて、暗号化された DM でも対応可能です。
- Chat recovery key generation / Chat リカバリーキー生成
- Fetch DM inbox list / DM 一覧取得
- Fetch DM conversation content / DM 内容取得
- Send DM text messages / DM テキスト送信
- Send DM image messages / DM 画像メッセージ送信
- Accept DM requests / DM リクエスト承諾
- Create new DM flow / DM 新規作成フロー
Accurate Browser-like X-Client-Transaction-Id / 高精度ブラウザ寄りX-Client-Transaction-Id
- Precise, browser-like
X-Client-Transaction-Idgeneration - 正確でブラウザ挙動に近い
X-Client-Transaction-Idの生成
Core X Actions / 基本的なX操作
- Retrieve all API-compatible query IDs / APIに対応するqueryIdの全取得
- Automatic Bearer token retrieval / Bearer tokenの自動取得
- Automatic cookie retrieval / cookieの自動取得
- Follow users / ユーザーのフォロー
- Fetch user tweets list / ユーザーツイート一覧取得
- Fetch tweet info / ツイート情報取得
- Tweet posting / ツイート投稿
- Like / いいね
- Bookmark / ブックマーク
- Retweet / リツイート
- Reply / リプライ
- Pin tweet / 固定ツイート
- Unpin tweet / 固定ツイート解除
- Delete tweet / ツイート削除
- Account language change / アカウント言語変更
- Proxy Support / Proxy対応
- Edit profile (name, bio, location, icon) / プロフィール編集
Function Call Examples / 関数呼び出し例
Core X Actions additions:
- Pin tweet / 固定ツイート
- Unpin tweet / 固定ツイート解除
- Delete tweet / ツイート削除
const lib = require("@higuchan123/twitter_lib");
const authToken = process.env.AUTH_TOKEN;
const passcode = process.env.XCHAT_PASSCODE;
// 1) Recover encrypted X Chat secret
// 1) 暗号化X Chatシークレットを復元
const xchat = await lib.get_xchat_recovered_secret(authToken, passcode);
// 2) Fetch DM inbox list
// 2) DM一覧取得
const inbox = await lib.fetch_xchat_inbox(xchat.cookies, xchat.bearer_token);
// 3) Fetch and decrypt DM conversation content
// 3) DM内容取得と復号
const conversationId = "1234567890-0987654321";
const page = await lib.fetch_xchat_conversation_page(
xchat.cookies,
xchat.bearer_token,
conversationId,
50
);
const decrypted = lib.decrypt_xchat_conversation_events(
page,
xchat.recovered_secret_hex,
xchat.self_user_id
);
// 4) Send encrypted DM text
// 4) 暗号化DMテキスト送信
await lib.send_xchat_text_message(
xchat.cookies,
xchat.bearer_token,
xchat.recovered_secret_hex,
xchat.token_map,
conversationId,
"Hello from @higuchan123/twitter_lib",
xchat.self_user_id
);
// 5) Send encrypted DM image
// 5) 暗号化DM画像送信
await lib.send_xchat_image_message(
xchat.cookies,
xchat.bearer_token,
xchat.recovered_secret_hex,
xchat.token_map,
conversationId,
"./image.jpg",
xchat.self_user_id
);
// 6) Accept DM requests
// 6) DMリクエスト承諾
const requests = await lib.fetch_dm_requests(authToken, { limit: 20 });
for (const req of requests.requests || []) {
await lib.accept_dm_request(authToken, req.conversation_id);
}
// 7) New DM creation flow (send path for new conversation)
// 7) DM新規作成フロー(新規会話への送信パス)
const recipientUserId = "1111111111";
const newConversationId = `${xchat.self_user_id}-${recipientUserId}`;
await lib.send_xchat_text_message(
xchat.cookies,
xchat.bearer_token,
xchat.recovered_secret_hex,
xchat.token_map,
newConversationId,
"First message in a new DM",
xchat.self_user_id
);
// 8) Browser-like transaction id generation
// 8) ブラウザ寄りTransaction ID生成
const gen = new lib.TransactionIdGenerator(homeHtml, ondemandJsText);
await gen.ready();
const transactionId = await gen.generate(
"GET",
"https://x.com/i/api/graphql/.../HomeTimeline?..."
);
// 9) Core X actions
// 9) 基本的なX操作
await lib.post_tweet(authToken, "hello world");
await lib.like_tweet(authToken, "https://x.com/user/status/123");
await lib.bookmark_tweet(authToken, "https://x.com/user/status/123");
await lib.retweet(authToken, "https://x.com/user/status/123");
await lib.reply_tweet(authToken, "https://x.com/user/status/123", "Nice post!");
await lib.set_account_language(authToken, "ja");
await lib.update_account_profile(authToken, {
name: "new name",
description: "new bio",
location: "Tokyo, Japan",
icon_path: "./icon.jpg",
});Proxy Support / Proxy対応
twitter_lib supports HTTP/HTTPS proxies.twitter_lib は HTTP/HTTPS プロキシに対応しています。
You can pass proxy settings per call with proxy_url (or proxy) in options.
各関数の options に proxy_url(または proxy)を渡して設定できます。
You can also set environment variables: PROXY_URL, HTTPS_PROXY, HTTP_PROXY, ALL_PROXY.
環境変数 PROXY_URL, HTTPS_PROXY, HTTP_PROXY, ALL_PROXY でも設定できます。
const lib = require("@higuchan123/twitter_lib");
const authToken = process.env.AUTH_TOKEN;
const proxyUrl = "http://username:password@host:port";
// Create a shared proxied session
// プロキシ付きセッションを作成
const session = lib.create_session({ proxy_url: proxyUrl });
// Use the session for API calls
// 作成したセッションを各API呼び出しで利用
const bearer = await lib.get_bearer_token(session);
const queryIds = await lib.get_all_query_ids(session);
const timeline = await lib.fetch_home_timeline_pages(authToken, {
session,
bearer_token: bearer,
limit: 20,
});Proxy:http://username:[email protected]:1212
For the complete library usage guide, see:
ライブラリの完全な使用方法は以下を参照してください:
https://github.com/GODFIREWA/twitter-lib/blob/main/twitter_lib.md
