Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d7204b0

Browse files
committedNov 3, 2024
twitter-profile
1 parent e580ac6 commit d7204b0

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
 

‎core/src/clients/twitter/base.ts

+48
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ export class ClientBase extends EventEmitter {
208208
await this.setCookiesFromArray(cookiesArray);
209209
} else {
210210
console.log("Cookies file path:", cookiesFilePath);
211+
console.log("username is", this.runtime.getSetting("TWITTER_USERNAME"));
211212
if (fs.existsSync(cookiesFilePath)) {
212213
const cookiesArray = JSON.parse(
213214
fs.readFileSync(cookiesFilePath, "utf-8")
@@ -271,6 +272,23 @@ export class ClientBase extends EventEmitter {
271272
console.log("Twitter user ID:", userId);
272273
this.twitterUserId = userId;
273274

275+
// Initialize Twitter profile
276+
const profile = await this.initializeProfile();
277+
if (profile) {
278+
console.log("Twitter profile initialized:", profile);
279+
280+
// Store profile info for use in responses
281+
this.runtime.character = {
282+
...this.runtime.character,
283+
twitterProfile: {
284+
username: profile.username,
285+
screenName: profile.screenName,
286+
bio: profile.bio,
287+
nicknames: profile.nicknames
288+
}
289+
};
290+
}
291+
274292
await this.populateTimeline();
275293

276294
this.onReady();
@@ -589,4 +607,34 @@ export class ClientBase extends EventEmitter {
589607
});
590608
}
591609
}
610+
611+
async initializeProfile() {
612+
const username = this.runtime.getSetting("TWITTER_USERNAME");
613+
if (!username) {
614+
console.error("Twitter username not configured");
615+
return;
616+
}
617+
618+
try {
619+
const profile = await this.requestQueue.add(async () => {
620+
const profile = await this.twitterClient.getProfile(username);
621+
return {
622+
username,
623+
screenName: profile.name || this.runtime.character.name,
624+
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] : "",
625+
nicknames: this.runtime.character.twitterProfile?.nicknames || []
626+
};
627+
});
628+
629+
return profile;
630+
} catch (error) {
631+
console.error("Error fetching Twitter profile:", error);
632+
return {
633+
username: this.runtime.character.name,
634+
screenName: username,
635+
bio: typeof this.runtime.character.bio === 'string' ? this.runtime.character.bio as string : this.runtime.character.bio.length > 0 ? this.runtime.character.bio[0] : "",
636+
nicknames: this.runtime.character.twitterProfile?.nicknames || []
637+
};
638+
}
639+
}
592640
}

‎core/src/core/types.ts

+12
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ export interface State {
139139
responseData?: Content; // An optional content object representing the agent's response in the current state.
140140
recentInteractionsData?: Memory[]; // An optional array of memory objects representing recent interactions in the conversation.
141141
recentInteractions?: string; // An optional string representation of recent interactions in the conversation.
142+
twitterProfile?: {
143+
username: string;
144+
screenName: string;
145+
bio: string;
146+
nicknames?: string[];
147+
};
142148
[key: string]: unknown; // Allows for additional properties to be included dynamically.
143149
}
144150

@@ -314,6 +320,12 @@ export type Character = {
314320
chat: string[];
315321
post: string[];
316322
};
323+
twitterProfile?: {
324+
username: string;
325+
screenName: string;
326+
bio: string;
327+
nicknames?: string[];
328+
};
317329
};
318330

319331
export interface IDatabaseAdapter {

0 commit comments

Comments
 (0)
Please sign in to comment.