Skip to content

Commit

Permalink
Merge pull request #856 from arslanaybars/fix/twitter-login-cookie-ch…
Browse files Browse the repository at this point in the history
…eck-#855

feat: improve X/Twitter login with cookie validation and retry mechanism
  • Loading branch information
lalalune authored Dec 14, 2024
2 parents 072e11b + f57b48f commit bc13351
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions packages/client-twitter/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,25 +174,36 @@ export class ClientBase extends EventEmitter {
}

elizaLogger.log("Waiting for Twitter login");
while (true) {
await this.twitterClient.login(
username,
this.runtime.getSetting("TWITTER_PASSWORD"),
this.runtime.getSetting("TWITTER_EMAIL"),
this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined
);

if (await this.twitterClient.isLoggedIn()) {
const cookies = await this.twitterClient.getCookies();
let retries = 5; // Optional: Set a retry limit
while (retries > 0) {
const cookies = await this.twitterClient.getCookies();
if (await this.twitterClient.isLoggedIn() || !!cookies) {
await this.cacheCookies(username, cookies);
elizaLogger.info("Successfully logged in and cookies cached.");
break;
}

elizaLogger.error("Failed to login to Twitter trying again...");
try {
await this.twitterClient.login(
username,
this.runtime.getSetting("TWITTER_PASSWORD"),
this.runtime.getSetting("TWITTER_EMAIL"),
this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined
);
} catch (error) {
elizaLogger.error(`Login attempt failed: ${error.message}`);
}

retries--;
elizaLogger.error(`Failed to login to Twitter. Retrying... (${retries} attempts left)`);

if (retries === 0) {
elizaLogger.error("Max retries reached. Exiting login process.");
throw new Error("Twitter login failed after maximum retries.");
}

await new Promise((resolve) => setTimeout(resolve, 2000));
}

// Initialize Twitter profile
this.profile = await this.fetchProfile(username);

Expand Down

0 comments on commit bc13351

Please sign in to comment.