This is a modified version of @the-convocation/twitter-scraper with added functionality for sending tweets and retweets. This package does not require the Twitter API to use and will run in both the browser and server.
Installation
npm install agent-twitter-client
Setup
Configure environment variables for authentication.
TWITTER_USERNAME= # Account username
TWITTER_PASSWORD= # Account password
TWITTER_EMAIL= # Account email
PROXY_URL= # HTTP(s) proxy for requests (necessary for browsers)
# Twitter API v2 credentials for tweet and poll functionality
TWITTER_API_KEY= # Twitter API Key
TWITTER_API_SECRET_KEY= # Twitter API Secret Key
TWITTER_ACCESS_TOKEN= # Access Token for Twitter API v2
TWITTER_ACCESS_TOKEN_SECRET= # Access Token Secret for Twitter API v2
It is important to use Twitter cookies to avoid sending a new login request to Twitter every time you want to perform an action.
In your application, you will likely want to check for existing cookies. If cookies are not available, log in with user authentication credentials and cache the cookies for future use.
const scraper = await getScraper({ authMethod: 'password' });
scraper.getCookies().then((cookies) => {
console.log(cookies);
});
Getting Started
const scraper = new Scraper();
await scraper.login('username', 'password');
await scraper.login(
'username',
'password',
'email',
'appKey',
'appSecret',
'accessToken',
'accessSecret',
);
const tweets = await scraper.getTweets('elonmusk', 10);
const tweetsAndReplies = scraper.getTweetsAndReplies('elonmusk');
const latestTweet = await scraper.getLatestTweet('elonmusk');
const tweet = await scraper.getTweet('1234567890123456789');
await scraper.sendTweet('Hello world!');
await scraper.sendTweetV2(
`What's got you most hyped? Let us know! 🤖💸`,
undefined,
{
poll: {
options: [
{ label: 'AI Innovations 🤖' },
{ label: 'Crypto Craze 💸' },
{ label: 'Both! 🌌' },
{ label: 'Neither for Me 😅' },
],
durationMinutes: 120,
},
},
);
const tweet = await scraper.getTweetV2('1856441982811529619', {
expansions: ['attachments.poll_ids'],
pollFields: ['options', 'end_datetime'],
});
console.log('tweet', tweet);
const tweets = await scraper.getTweetsV2(
['1856441982811529619', '1856429655215260130'],
{
expansions: ['attachments.poll_ids', 'attachments.media_keys'],
pollFields: ['options', 'end_datetime'],
mediaFields: ['url', 'preview_image_url'],
},
);
console.log('tweets', tweets);
API
Authentication
await scraper.login('username', 'password');
await scraper.logout();
const isLoggedIn = await scraper.isLoggedIn();
const cookies = await scraper.getCookies();
await scraper.setCookies(cookies);
await scraper.clearCookies();
Profile
const profile = await scraper.getProfile('TwitterDev');
const userId = await scraper.getUserIdByScreenName('TwitterDev');
Search
import { SearchMode } from 'agent-twitter-client';
const tweets = scraper.searchTweets('#nodejs', 20, SearchMode.Latest);
const profiles = scraper.searchProfiles('John', 10);
const results = await scraper.fetchSearchTweets('#nodejs', 20, SearchMode.Top);
const profileResults = await scraper.fetchSearchProfiles('John', 10);
Relationships
const followers = scraper.getFollowers('12345', 100);
const following = scraper.getFollowing('12345', 100);
const followerResults = await scraper.fetchProfileFollowers('12345', 100);
const followingResults = await scraper.fetchProfileFollowing('12345', 100);
const followUserResults = await scraper.followUser('elonmusk');
Trends
const trends = await scraper.getTrends();
const listTweets = await scraper.fetchListTweets('1234567890', 50);
const tweets = scraper.getTweets('TwitterDev');
const likedTweets = scraper.getLikedTweets('TwitterDev');
const tweetsAndReplies = scraper.getTweetsAndReplies('TwitterDev');
const timeline = scraper.getTweets('TwitterDev', 100);
const retweets = await scraper.getTweetsWhere(
timeline,
(tweet) => tweet.isRetweet,
);
const latestTweet = await scraper.getLatestTweet('TwitterDev');
const tweet = await scraper.getTweet('1234567890123456789');
const sendTweetResults = await scraper.sendTweet('Hello world!');
const sendQuoteTweetResults = await scraper.sendQuoteTweet('Hello world!', '1234567890123456789', ['mediaFile1', 'mediaFile2']);
const retweetResults = await scraper.retweet('1234567890123456789');
const likeTweetResults = await scraper.likeTweet('1234567890123456789');