From d7204b005519a2cd805e1885292269e791dd2031 Mon Sep 17 00:00:00 2001 From: alextitonis Date: Sun, 3 Nov 2024 16:42:20 +0300 Subject: [PATCH 1/2] twitter-profile --- core/src/clients/twitter/base.ts | 48 ++++++++++++++++++++++++++++++++ core/src/core/types.ts | 12 ++++++++ 2 files changed, 60 insertions(+) diff --git a/core/src/clients/twitter/base.ts b/core/src/clients/twitter/base.ts index 2bc06e43..acb89e90 100644 --- a/core/src/clients/twitter/base.ts +++ b/core/src/clients/twitter/base.ts @@ -208,6 +208,7 @@ export class ClientBase extends EventEmitter { await this.setCookiesFromArray(cookiesArray); } else { console.log("Cookies file path:", cookiesFilePath); + console.log("username is", this.runtime.getSetting("TWITTER_USERNAME")); if (fs.existsSync(cookiesFilePath)) { const cookiesArray = JSON.parse( fs.readFileSync(cookiesFilePath, "utf-8") @@ -271,6 +272,23 @@ export class ClientBase extends EventEmitter { console.log("Twitter user ID:", userId); this.twitterUserId = userId; + // Initialize Twitter profile + const profile = await this.initializeProfile(); + if (profile) { + console.log("Twitter profile initialized:", profile); + + // Store profile info for use in responses + this.runtime.character = { + ...this.runtime.character, + twitterProfile: { + username: profile.username, + screenName: profile.screenName, + bio: profile.bio, + nicknames: profile.nicknames + } + }; + } + await this.populateTimeline(); this.onReady(); @@ -589,4 +607,34 @@ export class ClientBase extends EventEmitter { }); } } + + async initializeProfile() { + const username = this.runtime.getSetting("TWITTER_USERNAME"); + if (!username) { + console.error("Twitter username not configured"); + return; + } + + try { + const profile = await this.requestQueue.add(async () => { + const profile = await this.twitterClient.getProfile(username); + return { + username, + screenName: profile.name || this.runtime.character.name, + bio: profile.biography || typeof this.runtime.character.bio === 'string' ? this.runtime.character.bio as string : this.runtime.character.bio.length > 0 ? this.runtime.character.bio[0] : "", + nicknames: this.runtime.character.twitterProfile?.nicknames || [] + }; + }); + + return profile; + } catch (error) { + console.error("Error fetching Twitter profile:", error); + return { + username: this.runtime.character.name, + screenName: username, + bio: typeof this.runtime.character.bio === 'string' ? this.runtime.character.bio as string : this.runtime.character.bio.length > 0 ? this.runtime.character.bio[0] : "", + nicknames: this.runtime.character.twitterProfile?.nicknames || [] + }; + } + } } diff --git a/core/src/core/types.ts b/core/src/core/types.ts index e088e446..66b4a46d 100644 --- a/core/src/core/types.ts +++ b/core/src/core/types.ts @@ -139,6 +139,12 @@ export interface State { responseData?: Content; // An optional content object representing the agent's response in the current state. recentInteractionsData?: Memory[]; // An optional array of memory objects representing recent interactions in the conversation. recentInteractions?: string; // An optional string representation of recent interactions in the conversation. + twitterProfile?: { + username: string; + screenName: string; + bio: string; + nicknames?: string[]; + }; [key: string]: unknown; // Allows for additional properties to be included dynamically. } @@ -314,6 +320,12 @@ export type Character = { chat: string[]; post: string[]; }; + twitterProfile?: { + username: string; + screenName: string; + bio: string; + nicknames?: string[]; + }; }; export interface IDatabaseAdapter { From 066e12904592314eafc912c9cd19e312c9a2e8e4 Mon Sep 17 00:00:00 2001 From: alextitonis Date: Sun, 3 Nov 2024 16:42:40 +0300 Subject: [PATCH 2/2] Update types.ts --- core/src/core/types.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/src/core/types.ts b/core/src/core/types.ts index 66b4a46d..d76576a9 100644 --- a/core/src/core/types.ts +++ b/core/src/core/types.ts @@ -139,12 +139,6 @@ export interface State { responseData?: Content; // An optional content object representing the agent's response in the current state. recentInteractionsData?: Memory[]; // An optional array of memory objects representing recent interactions in the conversation. recentInteractions?: string; // An optional string representation of recent interactions in the conversation. - twitterProfile?: { - username: string; - screenName: string; - bio: string; - nicknames?: string[]; - }; [key: string]: unknown; // Allows for additional properties to be included dynamically. }