Skip to content

Commit 4111f3f

Browse files
committed
fix: client twitter login and auth handler
1 parent 65ba827 commit 4111f3f

File tree

1 file changed

+77
-54
lines changed

1 file changed

+77
-54
lines changed

packages/client-twitter/src/base.ts

+77-54
Original file line numberDiff line numberDiff line change
@@ -157,19 +157,22 @@ export class ClientBase extends EventEmitter {
157157
const username = this.runtime.getSetting("TWITTER_USERNAME");
158158
const password = this.runtime.getSetting("TWITTER_PASSWORD");
159159
const email = this.runtime.getSetting("TWITTER_EMAIL");
160-
const twitter2faSecret = this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined;
160+
const twitter2faSecret =
161+
this.runtime.getSetting("TWITTER_2FA_SECRET") || undefined;
161162
const cookies = this.runtime.getSetting("TWITTER_COOKIES");
162163

163-
164164
if (!username) {
165165
throw new Error("Twitter username not configured");
166166
}
167167
// Check for Twitter cookies
168168
if (cookies) {
169+
elizaLogger.debug("Using cookies from settings");
169170
const cookiesArray = JSON.parse(cookies);
170171

171172
await this.setCookiesFromArray(cookiesArray);
172173
} else {
174+
elizaLogger.debug("No cookies found in settings");
175+
elizaLogger.debug("Checking for cached cookies");
173176
const cachedCookies = await this.getCachedCookies(username);
174177
if (cachedCookies) {
175178
await this.setCookiesFromArray(cachedCookies);
@@ -180,7 +183,8 @@ export class ClientBase extends EventEmitter {
180183
let retries = 5; // Optional: Set a retry limit
181184
while (retries > 0) {
182185
const cookies = await this.twitterClient.getCookies();
183-
if (await this.twitterClient.isLoggedIn() || !!cookies) {
186+
if ((await this.twitterClient.isLoggedIn()) && !!cookies) {
187+
elizaLogger.info("Already logged in.");
184188
await this.cacheCookies(username, cookies);
185189
elizaLogger.info("Successfully logged in and cookies cached.");
186190
break;
@@ -198,10 +202,14 @@ export class ClientBase extends EventEmitter {
198202
}
199203

200204
retries--;
201-
elizaLogger.error(`Failed to login to Twitter. Retrying... (${retries} attempts left)`);
205+
elizaLogger.error(
206+
`Failed to login to Twitter. Retrying... (${retries} attempts left)`
207+
);
202208

203209
if (retries === 0) {
204-
elizaLogger.error("Max retries reached. Exiting login process.");
210+
elizaLogger.error(
211+
"Max retries reached. Exiting login process."
212+
);
205213
throw new Error("Twitter login failed after maximum retries.");
206214
}
207215

@@ -243,63 +251,72 @@ export class ClientBase extends EventEmitter {
243251

244252
async fetchHomeTimeline(count: number): Promise<Tweet[]> {
245253
elizaLogger.debug("fetching home timeline");
246-
const homeTimeline = await this.twitterClient.fetchHomeTimeline(count, []);
254+
const homeTimeline = await this.twitterClient.fetchHomeTimeline(
255+
count,
256+
[]
257+
);
247258

248259
elizaLogger.debug(homeTimeline, { depth: Infinity });
249260
const processedTimeline = homeTimeline
250-
.filter((t) => t.__typename !== "TweetWithVisibilityResults") // what's this about?
251-
.map((tweet) => {
252-
//console.log("tweet is", tweet);
253-
const obj = {
254-
id: tweet.id,
255-
name:
256-
tweet.name ??
257-
tweet?.user_results?.result?.legacy.name,
258-
username:
259-
tweet.username ??
260-
tweet.core?.user_results?.result?.legacy.screen_name,
261-
text: tweet.text ?? tweet.legacy?.full_text,
262-
inReplyToStatusId:
263-
tweet.inReplyToStatusId ??
264-
tweet.legacy?.in_reply_to_status_id_str ??
265-
null,
266-
timestamp: new Date(tweet.legacy?.created_at).getTime() / 1000,
267-
createdAt: tweet.createdAt ?? tweet.legacy?.created_at ?? tweet.core?.user_results?.result?.legacy.created_at,
268-
userId: tweet.userId ?? tweet.legacy?.user_id_str,
269-
conversationId:
270-
tweet.conversationId ??
271-
tweet.legacy?.conversation_id_str,
272-
permanentUrl: `https://x.com/${tweet.core?.user_results?.result?.legacy?.screen_name}/status/${tweet.rest_id}`,
273-
hashtags: tweet.hashtags ?? tweet.legacy?.entities.hashtags,
274-
mentions:
275-
tweet.mentions ?? tweet.legacy?.entities.user_mentions,
276-
photos:
277-
tweet.photos ??
278-
tweet.legacy?.entities.media?.filter(
279-
(media) => media.type === "photo"
280-
) ??
281-
[],
282-
thread: tweet.thread || [],
283-
urls: tweet.urls ?? tweet.legacy?.entities.urls,
284-
videos:
285-
tweet.videos ??
286-
tweet.legacy?.entities.media?.filter(
287-
(media) => media.type === "video"
288-
) ??
289-
[],
290-
};
291-
//console.log("obj is", obj);
292-
return obj;
293-
});
261+
.filter((t) => t.__typename !== "TweetWithVisibilityResults") // what's this about?
262+
.map((tweet) => {
263+
//console.log("tweet is", tweet);
264+
const obj = {
265+
id: tweet.id,
266+
name:
267+
tweet.name ?? tweet?.user_results?.result?.legacy.name,
268+
username:
269+
tweet.username ??
270+
tweet.core?.user_results?.result?.legacy.screen_name,
271+
text: tweet.text ?? tweet.legacy?.full_text,
272+
inReplyToStatusId:
273+
tweet.inReplyToStatusId ??
274+
tweet.legacy?.in_reply_to_status_id_str ??
275+
null,
276+
timestamp:
277+
new Date(tweet.legacy?.created_at).getTime() / 1000,
278+
createdAt:
279+
tweet.createdAt ??
280+
tweet.legacy?.created_at ??
281+
tweet.core?.user_results?.result?.legacy.created_at,
282+
userId: tweet.userId ?? tweet.legacy?.user_id_str,
283+
conversationId:
284+
tweet.conversationId ??
285+
tweet.legacy?.conversation_id_str,
286+
permanentUrl: `https://x.com/${tweet.core?.user_results?.result?.legacy?.screen_name}/status/${tweet.rest_id}`,
287+
hashtags: tweet.hashtags ?? tweet.legacy?.entities.hashtags,
288+
mentions:
289+
tweet.mentions ?? tweet.legacy?.entities.user_mentions,
290+
photos:
291+
tweet.photos ??
292+
tweet.legacy?.entities.media?.filter(
293+
(media) => media.type === "photo"
294+
) ??
295+
[],
296+
thread: tweet.thread || [],
297+
urls: tweet.urls ?? tweet.legacy?.entities.urls,
298+
videos:
299+
tweet.videos ??
300+
tweet.legacy?.entities.media?.filter(
301+
(media) => media.type === "video"
302+
) ??
303+
[],
304+
};
305+
//console.log("obj is", obj);
306+
return obj;
307+
});
294308
//elizaLogger.debug("process homeTimeline", processedTimeline);
295309
return processedTimeline;
296310
}
297311

298312
async fetchTimelineForActions(count: number): Promise<Tweet[]> {
299313
elizaLogger.debug("fetching timeline for actions");
300-
const homeTimeline = await this.twitterClient.fetchHomeTimeline(count, []);
314+
const homeTimeline = await this.twitterClient.fetchHomeTimeline(
315+
count,
316+
[]
317+
);
301318

302-
return homeTimeline.map(tweet => ({
319+
return homeTimeline.map((tweet) => ({
303320
id: tweet.rest_id,
304321
name: tweet.core?.user_results?.result?.legacy?.name,
305322
username: tweet.core?.user_results?.result?.legacy?.screen_name,
@@ -311,10 +328,16 @@ export class ClientBase extends EventEmitter {
311328
permanentUrl: `https://twitter.com/${tweet.core?.user_results?.result?.legacy?.screen_name}/status/${tweet.rest_id}`,
312329
hashtags: tweet.legacy?.entities?.hashtags || [],
313330
mentions: tweet.legacy?.entities?.user_mentions || [],
314-
photos: tweet.legacy?.entities?.media?.filter(media => media.type === "photo") || [],
331+
photos:
332+
tweet.legacy?.entities?.media?.filter(
333+
(media) => media.type === "photo"
334+
) || [],
315335
thread: tweet.thread || [],
316336
urls: tweet.legacy?.entities?.urls || [],
317-
videos: tweet.legacy?.entities?.media?.filter(media => media.type === "video") || []
337+
videos:
338+
tweet.legacy?.entities?.media?.filter(
339+
(media) => media.type === "video"
340+
) || [],
318341
}));
319342
}
320343

0 commit comments

Comments
 (0)