Skip to content

Commit c411c2f

Browse files
authored
Merge pull request #1007 from dorianjanezic/main
feat: improve Twitter client with action processing
2 parents e4dac88 + 5fd9b40 commit c411c2f

File tree

6 files changed

+635
-16
lines changed

6 files changed

+635
-16
lines changed

.env.example

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ POST_INTERVAL_MIN= # Default: 90
6464
POST_INTERVAL_MAX= # Default: 180
6565
POST_IMMEDIATELY=
6666

67+
# Twitter action processing configuration
68+
ACTION_INTERVAL=300000 # Interval in milliseconds between action processing runs (default: 5 minutes)
69+
ENABLE_ACTION_PROCESSING=false # Set to true to enable the action processing loop
70+
6771
# Feature Flags
6872
IMAGE_GEN= # Set to TRUE to enable image generation
6973
USE_OPENAI_EMBEDDING= # Set to TRUE for OpenAI/1536, leave blank for local

packages/client-twitter/src/base.ts

+40
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,46 @@ export class ClientBase extends EventEmitter {
271271
// });
272272
}
273273

274+
async fetchFeedTimeline(count: number): Promise<string> {
275+
elizaLogger.debug("fetching home timeline");
276+
const homeTimeline = await this.twitterClient.fetchHomeTimeline(count, []);
277+
return homeTimeline
278+
.filter(tweet => tweet.text || tweet.legacy?.full_text)
279+
.sort((a, b) => {
280+
const timestampA = new Date(a.createdAt ?? a.legacy?.created_at).getTime();
281+
const timestampB = new Date(b.createdAt ?? b.legacy?.created_at).getTime();
282+
return timestampB - timestampA;
283+
})
284+
.slice(0, count)
285+
.map(tweet =>
286+
`@${tweet.username || tweet.core?.user_results?.result?.legacy?.screen_name}: ${tweet.text ?? tweet.legacy?.full_text ?? ''}`
287+
)
288+
.join('\n');
289+
}
290+
291+
async fetchTimelineForActions(count: number): Promise<Tweet[]> {
292+
elizaLogger.debug("fetching timeline for actions");
293+
const homeTimeline = await this.twitterClient.fetchHomeTimeline(count, []);
294+
295+
return homeTimeline.map(tweet => ({
296+
id: tweet.rest_id,
297+
name: tweet.core?.user_results?.result?.legacy?.name,
298+
username: tweet.core?.user_results?.result?.legacy?.screen_name,
299+
text: tweet.legacy?.full_text,
300+
inReplyToStatusId: tweet.legacy?.in_reply_to_status_id_str,
301+
timestamp: new Date(tweet.legacy?.created_at).getTime() / 1000,
302+
userId: tweet.legacy?.user_id_str,
303+
conversationId: tweet.legacy?.conversation_id_str,
304+
permanentUrl: `https://twitter.com/${tweet.core?.user_results?.result?.legacy?.screen_name}/status/${tweet.rest_id}`,
305+
hashtags: tweet.legacy?.entities?.hashtags || [],
306+
mentions: tweet.legacy?.entities?.user_mentions || [],
307+
photos: tweet.legacy?.entities?.media?.filter(media => media.type === "photo") || [],
308+
thread: tweet.thread || [],
309+
urls: tweet.legacy?.entities?.urls || [],
310+
videos: tweet.legacy?.entities?.media?.filter(media => media.type === "video") || []
311+
}));
312+
}
313+
274314
async fetchSearchTweets(
275315
query: string,
276316
maxTweets: number,

0 commit comments

Comments
 (0)