Skip to content

Commit

Permalink
prefer this.client.twitterConfig over getSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
odilitime committed Dec 28, 2024
1 parent 09d4978 commit ecaf554
Showing 1 changed file with 18 additions and 42 deletions.
60 changes: 18 additions & 42 deletions packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
IAgentRuntime,
ModelClass,
stringToUuid,
parseBooleanFromText,
UUID,
} from "@elizaos/core";
import { elizaLogger } from "@elizaos/core";
Expand Down Expand Up @@ -106,10 +105,8 @@ export class TwitterPostClient {
constructor(client: ClientBase, runtime: IAgentRuntime) {
this.client = client;
this.runtime = runtime;
this.twitterUsername = runtime.getSetting("TWITTER_USERNAME");
this.isDryRun = parseBooleanFromText(
runtime.getSetting("TWITTER_DRY_RUN") ?? "false"
);
this.twitterUsername = this.client.twitterConfig.TWITTER_USERNAME;
this.isDryRun = this.client.twitterConfig.TWITTER_DRY_RUN

// Log configuration on initialization
elizaLogger.log("Twitter Client Configuration:");
Expand All @@ -118,34 +115,34 @@ export class TwitterPostClient {
`- Dry Run Mode: ${this.isDryRun ? "enabled" : "disabled"}`
);
elizaLogger.log(
`- Post Interval: ${runtime.getSetting("POST_INTERVAL_MIN") || "90"}-${runtime.getSetting("POST_INTERVAL_MAX") || "180"} minutes`
`- Post Interval: ${this.client.twitterConfig.POST_INTERVAL_MIN}-${this.client.twitterConfig.POST_INTERVAL_MAX} minutes`
);
elizaLogger.log(
`- Action Processing: ${parseBooleanFromText(runtime.getSetting("ENABLE_ACTION_PROCESSING") ?? "false") ? "enabled" : "disabled"}`
`- Action Processing: ${this.client.twitterConfig.ENABLE_ACTION_PROCESSING ? "enabled" : "disabled"}`
);
elizaLogger.log(
`- Action Interval: ${(parseInt(runtime.getSetting("ACTION_INTERVAL") ?? "300000") / 1000).toFixed(0)} seconds`
`- Action Interval: ${this.client.twitterConfig.ACTION_INTERVAL} seconds`
);
elizaLogger.log(
`- Post Immediately: ${parseBooleanFromText(runtime.getSetting("POST_IMMEDIATELY") ?? "false") ? "enabled" : "disabled"}`
`- Post Immediately: ${this.client.twitterConfig.POST_IMMEDIATELY ? "enabled" : "disabled"}`
);
elizaLogger.log(
`- Search Enabled: ${parseBooleanFromText(runtime.getSetting("TWITTER_SEARCH_ENABLE") ?? "false") ? "enabled" : "disabled"}`
`- Search Enabled: ${this.client.twitterConfig.TWITTER_SEARCH_ENABLE ? "enabled" : "disabled"}`
);

const targetUsers = runtime.getSetting("TWITTER_TARGET_USERS");
const targetUsers = this.client.twitterConfig.TWITTER_TARGET_USERS;
if (targetUsers) {
elizaLogger.log(`- Target Users: ${targetUsers}`);
}

if (this.isDryRun) {
elizaLogger.log(
"Twitter client initialized in dry run mode - no actual tweets will be posted"
"Twitter client initialized in dry run mode - no actual tweets should be posted"
);
}
}

async start(postImmediately: boolean = false) {
async start() {
if (!this.client.profile) {
await this.client.init();
}
Expand All @@ -156,10 +153,8 @@ export class TwitterPostClient {
}>("twitter/" + this.twitterUsername + "/lastPost");

const lastPostTimestamp = lastPost?.timestamp ?? 0;
const minMinutes =
parseInt(this.runtime.getSetting("POST_INTERVAL_MIN")) || 90;
const maxMinutes =
parseInt(this.runtime.getSetting("POST_INTERVAL_MAX")) || 180;
const minMinutes = this.client.twitterConfig.POST_INTERVAL_MIN;
const maxMinutes = this.client.twitterConfig.POST_INTERVAL_MAX;
const randomMinutes =
Math.floor(Math.random() * (maxMinutes - minMinutes + 1)) +
minMinutes;
Expand All @@ -177,8 +172,7 @@ export class TwitterPostClient {
};

const processActionsLoop = async () => {
const actionInterval =
parseInt(this.runtime.getSetting("ACTION_INTERVAL")) || 300000; // Default to 5 minutes
const actionInterval = this.client.twitterConfig.ACTION_INTERVAL; // Defaults to 5 minutes

while (!this.stopProcessingActions) {
try {
Expand All @@ -190,7 +184,7 @@ export class TwitterPostClient {
);
// Wait for the full interval before next processing
await new Promise((resolve) =>
setTimeout(resolve, actionInterval)
setTimeout(resolve, actionInterval * 60 * 1000) // now in minutes
);
}
} catch (error) {
Expand All @@ -204,16 +198,7 @@ export class TwitterPostClient {
}
};

if (
this.runtime.getSetting("POST_IMMEDIATELY") != null &&
this.runtime.getSetting("POST_IMMEDIATELY") !== ""
) {
// Retrieve setting, default to false if not set or if the value is not "true"
postImmediately =
this.runtime.getSetting("POST_IMMEDIATELY") === "true" || false;
}

if (postImmediately) {
if (this.client.twitterConfig.POST_IMMEDIATELY) {
await this.generateNewTweet();
}

Expand All @@ -225,12 +210,7 @@ export class TwitterPostClient {
elizaLogger.log("Tweet generation loop disabled (dry run mode)");
}

// Add check for ENABLE_ACTION_PROCESSING before starting the loop
const enableActionProcessing = parseBooleanFromText(
this.runtime.getSetting("ENABLE_ACTION_PROCESSING") ?? "false"
);

if (enableActionProcessing && !this.isDryRun) {
if (this.client.twitterConfig.ENABLE_ACTION_PROCESSING && !this.isDryRun) {
processActionsLoop().catch((error) => {
elizaLogger.error(
"Fatal error in process actions loop:",
Expand Down Expand Up @@ -333,8 +313,7 @@ export class TwitterPostClient {
// Note Tweet failed due to authorization. Falling back to standard Tweet.
const truncateContent = truncateToCompleteSentence(
content,
parseInt(runtime.getSetting("MAX_TWEET_LENGTH")) ||
DEFAULT_MAX_TWEET_LENGTH
this.client.twitterConfig.MAX_TWEET_LENGTH
);
return await this.sendStandardTweet(
client,
Expand Down Expand Up @@ -496,10 +475,7 @@ export class TwitterPostClient {
}

// Truncate the content to the maximum tweet length specified in the environment settings, ensuring the truncation respects sentence boundaries.
const maxTweetLength = parseInt(
this.runtime.getSetting("MAX_TWEET_LENGTH"),
10
);
const maxTweetLength = this.client.twitterConfig.MAX_TWEET_LENGTH
if (maxTweetLength) {
cleanedContent = truncateToCompleteSentence(
cleanedContent,
Expand Down

0 comments on commit ecaf554

Please sign in to comment.