From b9b495461c94dc3c90ded62f1051e58223d6b985 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 11 Nov 2024 21:39:21 +0300 Subject: [PATCH] twitter-profile-remake --- packages/client-twitter/src/base.ts | 48 +++++++++++++++++++++++++++++ packages/core/src/types.ts | 6 ++++ 2 files changed, 54 insertions(+) diff --git a/packages/client-twitter/src/base.ts b/packages/client-twitter/src/base.ts index fd2e887f02e..a4d1df0b9ff 100644 --- a/packages/client-twitter/src/base.ts +++ b/packages/client-twitter/src/base.ts @@ -272,6 +272,24 @@ 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(); @@ -601,4 +619,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/packages/core/src/types.ts b/packages/core/src/types.ts index 7d61a241f54..bfe28d4ef74 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -357,6 +357,12 @@ export type Character = { chat: string[]; post: string[]; }; + twitterProfile?: { + username: string; + screenName: string; + bio: string; + nicknames?: string[]; + }; }; export interface IDatabaseAdapter {