Skip to content

Commit e4dac88

Browse files
authored
Merge pull request #1003 from odilitime/twitter-search-switch
2 parents 3bb4350 + 193860b commit e4dac88

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ TWITTER_EMAIL= # Account email
5353
TWITTER_2FA_SECRET=
5454
TWITTER_COOKIES= # Account cookies
5555
TWITTER_POLL_INTERVAL=120 # How often (in seconds) the bot should check for interactions
56+
TWITTER_SEARCH_ENABLE=FALSE # Enable timeline search, WARNING this greatly increases your chance of getting banned
5657
TWITTER_TARGET_USERS= # Comma separated list of Twitter user names to interact with
5758
X_SERVER_URL=
5859
XAI_API_KEY=

agent/src/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ export async function initializeClients(
335335
}
336336

337337
if (clientTypes.includes("twitter")) {
338+
TwitterClientInterface.enableSearch = !isFalsish(getSecret(character, "TWITTER_SEARCH_ENABLE"));
338339
const twitterClients = await TwitterClientInterface.start(runtime);
339340
clients.push(twitterClients);
340341
}
@@ -358,6 +359,22 @@ export async function initializeClients(
358359
return clients;
359360
}
360361

362+
function isFalsish(input: any): boolean {
363+
// If the input is exactly NaN, return true
364+
if (Number.isNaN(input)) {
365+
return true;
366+
}
367+
368+
// Convert input to a string if it's not null or undefined
369+
const value = input == null ? '' : String(input);
370+
371+
// List of common falsish string representations
372+
const falsishValues = ['false', '0', 'no', 'n', 'off', 'null', 'undefined', ''];
373+
374+
// Check if the value (trimmed and lowercased) is in the falsish list
375+
return falsishValues.includes(value.trim().toLowerCase());
376+
}
377+
361378
function getSecret(character: Character, secret: string) {
362379
return character.settings.secrets?.[secret] || process.env[secret];
363380
}

packages/client-twitter/src/index.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,34 @@ class TwitterManager {
1010
post: TwitterPostClient;
1111
search: TwitterSearchClient;
1212
interaction: TwitterInteractionClient;
13-
constructor(runtime: IAgentRuntime) {
13+
constructor(runtime: IAgentRuntime, enableSearch:boolean) {
1414
this.client = new ClientBase(runtime);
1515
this.post = new TwitterPostClient(this.client, runtime);
16-
//this.search = new TwitterSearchClient(this.client, runtime); // don't start the search client by default
17-
// this searches topics from character file, but kind of violates consent of random users
18-
// burns your rate limit and can get your account banned
19-
// use at your own risk
16+
17+
if (enableSearch) {
18+
// this searches topics from character file
19+
elizaLogger.warn('Twitter/X client running in a mode that:')
20+
elizaLogger.warn('1. violates consent of random users')
21+
elizaLogger.warn('2. burns your rate limit')
22+
elizaLogger.warn('3. can get your account banned')
23+
elizaLogger.warn('use at your own risk')
24+
this.search = new TwitterSearchClient(this.client, runtime); // don't start the search client by default
25+
}
2026
this.interaction = new TwitterInteractionClient(this.client, runtime);
2127
}
2228
}
2329

2430
export const TwitterClientInterface: Client = {
31+
2532
async start(runtime: IAgentRuntime) {
2633
await validateTwitterConfig(runtime);
2734

2835
elizaLogger.log("Twitter client started");
2936

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

3242
await manager.client.init();
3343

0 commit comments

Comments
 (0)