Skip to content

Commit

Permalink
feat: improve Twitter client with action processing
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianjanezic committed Dec 12, 2024
1 parent 1c9a5a1 commit b50a5bf
Show file tree
Hide file tree
Showing 6 changed files with 642 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ POST_INTERVAL_MIN= # Default: 90
POST_INTERVAL_MAX= # Default: 180
POST_IMMEDIATELY=

# Twitter action processing configuration
ACTION_INTERVAL=300000 # Interval in milliseconds between action processing runs (default: 5 minutes)
ENABLE_ACTION_PROCESSING=false # Set to true to enable the action processing loop

# Feature Flags
IMAGE_GEN= # Set to TRUE to enable image generation
USE_OPENAI_EMBEDDING= # Set to TRUE for OpenAI/1536, leave blank for local
Expand Down
40 changes: 40 additions & 0 deletions packages/client-twitter/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,46 @@ export class ClientBase extends EventEmitter {
// });
}

async fetchFeedTimeline(count: number): Promise<string> {
elizaLogger.debug("fetching home timeline");
const homeTimeline = await this.twitterClient.fetchHomeTimeline(count, []);
return homeTimeline
.filter(tweet => tweet.text || tweet.legacy?.full_text)
.sort((a, b) => {
const timestampA = new Date(a.createdAt ?? a.legacy?.created_at).getTime();
const timestampB = new Date(b.createdAt ?? b.legacy?.created_at).getTime();
return timestampB - timestampA;
})
.slice(0, count)
.map(tweet =>
`@${tweet.username || tweet.core?.user_results?.result?.legacy?.screen_name}: ${tweet.text ?? tweet.legacy?.full_text ?? ''}`
)
.join('\n');
}

async fetchTimelineForActions(count: number): Promise<Tweet[]> {
elizaLogger.debug("fetching timeline for actions");
const homeTimeline = await this.twitterClient.fetchHomeTimeline(count, []);

return homeTimeline.map(tweet => ({
id: tweet.rest_id,
name: tweet.core?.user_results?.result?.legacy?.name,
username: tweet.core?.user_results?.result?.legacy?.screen_name,
text: tweet.legacy?.full_text,
inReplyToStatusId: tweet.legacy?.in_reply_to_status_id_str,
timestamp: new Date(tweet.legacy?.created_at).getTime() / 1000,
userId: tweet.legacy?.user_id_str,
conversationId: tweet.legacy?.conversation_id_str,
permanentUrl: `https://twitter.com/${tweet.core?.user_results?.result?.legacy?.screen_name}/status/${tweet.rest_id}`,
hashtags: tweet.legacy?.entities?.hashtags || [],
mentions: tweet.legacy?.entities?.user_mentions || [],
photos: tweet.legacy?.entities?.media?.filter(media => media.type === "photo") || [],
thread: tweet.thread || [],
urls: tweet.legacy?.entities?.urls || [],
videos: tweet.legacy?.entities?.media?.filter(media => media.type === "video") || []
}));
}

async fetchSearchTweets(
query: string,
maxTweets: number,
Expand Down
Loading

0 comments on commit b50a5bf

Please sign in to comment.