@@ -136,7 +136,7 @@ export class ClientBase extends EventEmitter {
136
136
) ;
137
137
}
138
138
139
- constructor ( runtime : IAgentRuntime , twitterConfig :TwitterConfig ) {
139
+ constructor ( runtime : IAgentRuntime , twitterConfig : TwitterConfig ) {
140
140
super ( ) ;
141
141
this . runtime = runtime ;
142
142
this . twitterConfig = twitterConfig ;
@@ -159,7 +159,7 @@ export class ClientBase extends EventEmitter {
159
159
const username = this . twitterConfig . TWITTER_USERNAME ;
160
160
const password = this . twitterConfig . TWITTER_PASSWORD ;
161
161
const email = this . twitterConfig . TWITTER_EMAIL ;
162
- let retries = this . twitterConfig . TWITTER_RETRY_LIMIT
162
+ let retries = this . twitterConfig . TWITTER_RETRY_LIMIT ;
163
163
const twitter2faSecret = this . twitterConfig . TWITTER_2FA_SECRET ;
164
164
165
165
if ( ! username ) {
@@ -176,7 +176,8 @@ export class ClientBase extends EventEmitter {
176
176
elizaLogger . log ( "Waiting for Twitter login" ) ;
177
177
while ( retries > 0 ) {
178
178
try {
179
- if ( await this . twitterClient . isLoggedIn ( ) ) { // cookies are valid, no login required
179
+ if ( await this . twitterClient . isLoggedIn ( ) ) {
180
+ // cookies are valid, no login required
180
181
elizaLogger . info ( "Successfully logged in." ) ;
181
182
break ;
182
183
} else {
@@ -186,7 +187,8 @@ export class ClientBase extends EventEmitter {
186
187
email ,
187
188
twitter2faSecret
188
189
) ;
189
- if ( await this . twitterClient . isLoggedIn ( ) ) { // fresh login, store new cookies
190
+ if ( await this . twitterClient . isLoggedIn ( ) ) {
191
+ // fresh login, store new cookies
190
192
elizaLogger . info ( "Successfully logged in." ) ;
191
193
elizaLogger . info ( "Caching cookies" ) ;
192
194
await this . cacheCookies (
@@ -251,7 +253,10 @@ export class ClientBase extends EventEmitter {
251
253
/**
252
254
* Fetch timeline for twitter account, optionally only from followed accounts
253
255
*/
254
- async fetchHomeTimeline ( count : number , following ?: boolean ) : Promise < Tweet [ ] > {
256
+ async fetchHomeTimeline (
257
+ count : number ,
258
+ following ?: boolean
259
+ ) : Promise < Tweet [ ] > {
255
260
elizaLogger . debug ( "fetching home timeline" ) ;
256
261
const homeTimeline = following
257
262
? await this . twitterClient . fetchFollowingTimeline ( count , [ ] )
@@ -288,13 +293,14 @@ export class ClientBase extends EventEmitter {
288
293
hashtags : tweet . hashtags ?? tweet . legacy ?. entities . hashtags ,
289
294
mentions :
290
295
tweet . mentions ?? tweet . legacy ?. entities . user_mentions ,
291
- photos : tweet . legacy ?. entities ?. media ?. filter (
292
- ( media ) => media . type === "photo"
293
- ) . map ( media => ( {
294
- id : media . id_str ,
295
- url : media . media_url_https , // Store media_url_https as url
296
- alt_text : media . alt_text
297
- } ) ) || [ ] ,
296
+ photos :
297
+ tweet . legacy ?. entities ?. media
298
+ ?. filter ( ( media ) => media . type === "photo" )
299
+ . map ( ( media ) => ( {
300
+ id : media . id_str ,
301
+ url : media . media_url_https , // Store media_url_https as url
302
+ alt_text : media . alt_text ,
303
+ } ) ) || [ ] ,
298
304
thread : tweet . thread || [ ] ,
299
305
urls : tweet . urls ?? tweet . legacy ?. entities . urls ,
300
306
videos :
@@ -311,41 +317,44 @@ export class ClientBase extends EventEmitter {
311
317
return processedTimeline ;
312
318
}
313
319
314
- async fetchTimelineForActions ( count : number ) : Promise < Tweet [ ] > {
320
+ async fetchTimelineForActions ( ) : Promise < Tweet [ ] > {
315
321
elizaLogger . debug ( "fetching timeline for actions" ) ;
316
322
317
- const agentUsername = this . twitterConfig . TWITTER_USERNAME
318
- const homeTimeline = await this . twitterClient . fetchHomeTimeline (
319
- count ,
320
- [ ]
321
- ) ;
322
-
323
- return homeTimeline . map ( ( tweet ) => ( {
324
- id : tweet . rest_id ,
325
- name : tweet . core ?. user_results ?. result ?. legacy ?. name ,
326
- username : tweet . core ?. user_results ?. result ?. legacy ?. screen_name ,
327
- text : tweet . legacy ?. full_text ,
328
- inReplyToStatusId : tweet . legacy ?. in_reply_to_status_id_str ,
329
- timestamp : new Date ( tweet . legacy ?. created_at ) . getTime ( ) / 1000 ,
330
- userId : tweet . legacy ?. user_id_str ,
331
- conversationId : tweet . legacy ?. conversation_id_str ,
332
- permanentUrl : `https://twitter.com/${ tweet . core ?. user_results ?. result ?. legacy ?. screen_name } /status/${ tweet . rest_id } ` ,
333
- hashtags : tweet . legacy ?. entities ?. hashtags || [ ] ,
334
- mentions : tweet . legacy ?. entities ?. user_mentions || [ ] ,
335
- photos : tweet . legacy ?. entities ?. media ?. filter (
336
- ( media ) => media . type === "photo"
337
- ) . map ( media => ( {
338
- id : media . id_str ,
339
- url : media . media_url_https , // Store media_url_https as url
340
- alt_text : media . alt_text
341
- } ) ) || [ ] ,
342
- thread : tweet . thread || [ ] ,
343
- urls : tweet . legacy ?. entities ?. urls || [ ] ,
344
- videos :
345
- tweet . legacy ?. entities ?. media ?. filter (
346
- ( media ) => media . type === "video"
347
- ) || [ ] ,
348
- } ) ) . filter ( tweet => tweet . username !== agentUsername ) ; // do not perform action on self-tweets
323
+ const agentUsername = this . twitterConfig . TWITTER_USERNAME ;
324
+ const homeTimeline = await this . twitterClient . fetchHomeTimeline ( 20 , [ ] ) ;
325
+
326
+ const processedTweets = homeTimeline
327
+ . map ( ( tweet ) => ( {
328
+ id : tweet . rest_id ,
329
+ name : tweet . core ?. user_results ?. result ?. legacy ?. name ,
330
+ username : tweet . core ?. user_results ?. result ?. legacy ?. screen_name ,
331
+ text : tweet . legacy ?. full_text ,
332
+ inReplyToStatusId : tweet . legacy ?. in_reply_to_status_id_str ,
333
+ timestamp : new Date ( tweet . legacy ?. created_at ) . getTime ( ) / 1000 ,
334
+ userId : tweet . legacy ?. user_id_str ,
335
+ conversationId : tweet . legacy ?. conversation_id_str ,
336
+ permanentUrl : `https://twitter.com/${ tweet . core ?. user_results ?. result ?. legacy ?. screen_name } /status/${ tweet . rest_id } ` ,
337
+ hashtags : tweet . legacy ?. entities ?. hashtags || [ ] ,
338
+ mentions : tweet . legacy ?. entities ?. user_mentions || [ ] ,
339
+ photos :
340
+ tweet . legacy ?. entities ?. media
341
+ ?. filter ( ( media ) => media . type === "photo" )
342
+ . map ( ( media ) => ( {
343
+ id : media . id_str ,
344
+ url : media . media_url_https , // Store media_url_https as url
345
+ alt_text : media . alt_text ,
346
+ } ) ) || [ ] ,
347
+ thread : tweet . thread || [ ] ,
348
+ urls : tweet . legacy ?. entities ?. urls || [ ] ,
349
+ videos :
350
+ tweet . legacy ?. entities ?. media ?. filter (
351
+ ( media ) => media . type === "video"
352
+ ) || [ ] ,
353
+ } ) )
354
+ . filter ( ( tweet ) => tweet . username !== agentUsername ) ; // do not perform action on self-tweets
355
+
356
+ const shuffledTweets = processedTweets . sort ( ( ) => Math . random ( ) - 0.5 ) ;
357
+ return shuffledTweets ;
349
358
}
350
359
351
360
async fetchSearchTweets (
0 commit comments