Skip to content

Commit

Permalink
remove logs, add reply to on tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Jul 13, 2024
1 parent e992f4c commit 89e936f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scraper",
"crawler"
],
"version": "0.0.5",
"version": "0.0.6",
"main": "dist/default/cjs/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
Expand Down
5 changes: 0 additions & 5 deletions src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ testLogin(
'scraper can log in',
async () => {
const scraper = await getScraper({ authMethod: 'password' });

scraper.getCookies().then((cookies) => {
console.log(cookies);
});

await expect(scraper.isLoggedIn()).resolves.toBeTruthy();
},
15000,
Expand Down
5 changes: 2 additions & 3 deletions src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,9 @@ export class Scraper {
* @returns
*/

async sendTweet(text: string) {
async sendTweet(text: string, tweetId?: string) {
try {
const response = await createCreateTweetRequest(text, this.auth);
console.log('**** response');
const response = await createCreateTweetRequest(text, this.auth, tweetId);
console.log(JSON.stringify(response));
if (response.ok) {
console.log('Draft tweet created successfully:');
Expand Down
7 changes: 7 additions & 0 deletions src/tweets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,11 @@ test('sendTweet successfully sends a tweet', async () => {
// Verify that an error message was logged
// Note: This assumes your implementation of sendTweet logs an error message on failure
expect(success).toBe(true);
// wait 5 seconds for the tweet to be sent

const replySuccess = await scraper.sendTweet(
'Ignore this',
'1430277451452751874',
);
expect(replySuccess).toBe(true);
});
20 changes: 13 additions & 7 deletions src/tweets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ export async function fetchTweetsAndReplies(
export async function createCreateTweetRequest(
text: string,
auth: TwitterAuth,
tweetId?: string,
) {
console.log('*** Auth is:', auth);
const onboardingTaskUrl = 'https://api.twitter.com/1.1/onboarding/task.json';

const cookies = await auth.cookieJar().getCookies(onboardingTaskUrl);
Expand All @@ -186,17 +186,23 @@ export async function createCreateTweetRequest(
'x-csrf-token': xCsrfToken?.value as string,
});

const variables: Record<string, any> = {
tweet_text: text,
dark_request: false,
media: { media_entities: [], possibly_sensitive: false },
semantic_annotation_ids: [],
};

if (tweetId) {
variables.reply = { in_reply_to_tweet_id: tweetId };
}

const response = await fetch(
'https://twitter.com/i/api/graphql/a1p9RWpkYKBjWv_I3WzS-A/CreateTweet',
{
headers,
body: JSON.stringify({
variables: {
tweet_text: text,
dark_request: false,
media: { media_entities: [], possibly_sensitive: false },
semantic_annotation_ids: [],
},
variables,
features: {
interactive_text_enabled: true,
longform_notetweets_inline_media_enabled: false,
Expand Down

0 comments on commit 89e936f

Please sign in to comment.