Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve X/Twitter login with cookie validation and retry mechanism #856

Merged
Merged
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
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
arslanaybars marked this conversation as resolved.
Show resolved Hide resolved
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
Loading