Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter profile #180

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions core/src/clients/twitter/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 || []
};
}
}
}
6 changes: 6 additions & 0 deletions core/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ export type Character = {
chat: string[];
post: string[];
};
twitterProfile?: {
username: string;
screenName: string;
bio: string;
nicknames?: string[];
};
};

export interface IDatabaseAdapter {
Expand Down