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

chore: Twitter search switch #1003

Merged
merged 8 commits into from
Dec 12, 2024
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ TWITTER_EMAIL= # Account email
TWITTER_2FA_SECRET=
TWITTER_COOKIES= # Account cookies
TWITTER_POLL_INTERVAL=120 # How often (in seconds) the bot should check for interactions
TWITTER_SEARCH_ENABLE=FALSE # Enable timeline search, WARNING this greatly increases your chance of getting banned
TWITTER_TARGET_USERS= # Comma separated list of Twitter user names to interact with
X_SERVER_URL=
XAI_API_KEY=
Expand Down
17 changes: 17 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ export async function initializeClients(
}

if (clientTypes.includes("twitter")) {
TwitterClientInterface.enableSearch = !isFalsish(getSecret(character, "TWITTER_SEARCH_ENABLE"));
const twitterClients = await TwitterClientInterface.start(runtime);
clients.push(twitterClients);
}
Expand All @@ -358,6 +359,22 @@ export async function initializeClients(
return clients;
}

function isFalsish(input: any): boolean {
// If the input is exactly NaN, return true
if (Number.isNaN(input)) {
return true;
}

// Convert input to a string if it's not null or undefined
const value = input == null ? '' : String(input);

// List of common falsish string representations
const falsishValues = ['false', '0', 'no', 'n', 'off', 'null', 'undefined', ''];

// Check if the value (trimmed and lowercased) is in the falsish list
return falsishValues.includes(value.trim().toLowerCase());
}

function getSecret(character: Character, secret: string) {
return character.settings.secrets?.[secret] || process.env[secret];
}
Expand Down
22 changes: 16 additions & 6 deletions packages/client-twitter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,34 @@ class TwitterManager {
post: TwitterPostClient;
search: TwitterSearchClient;
interaction: TwitterInteractionClient;
constructor(runtime: IAgentRuntime) {
constructor(runtime: IAgentRuntime, enableSearch:boolean) {
this.client = new ClientBase(runtime);
this.post = new TwitterPostClient(this.client, runtime);
//this.search = new TwitterSearchClient(this.client, runtime); // don't start the search client by default
// this searches topics from character file, but kind of violates consent of random users
// burns your rate limit and can get your account banned
// use at your own risk

if (enableSearch) {
// this searches topics from character file
elizaLogger.warn('Twitter/X client running in a mode that:')
elizaLogger.warn('1. violates consent of random users')
elizaLogger.warn('2. burns your rate limit')
elizaLogger.warn('3. can get your account banned')
elizaLogger.warn('use at your own risk')
this.search = new TwitterSearchClient(this.client, runtime); // don't start the search client by default
}
this.interaction = new TwitterInteractionClient(this.client, runtime);
}
}

export const TwitterClientInterface: Client = {

async start(runtime: IAgentRuntime) {
await validateTwitterConfig(runtime);

elizaLogger.log("Twitter client started");

const manager = new TwitterManager(runtime);
// enableSearch is just set previous to this call
// so enableSearch can change over time
// and changing it won't stop the SearchClient in the existing instance
const manager = new TwitterManager(runtime, this.enableSearch);

await manager.client.init();

Expand Down
Loading