@elizaos/plugin-twitter 1.0.0-beta.32 → 1.0.0-beta.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -6611,8 +6611,52 @@ import {
6611
6611
  ModelType as ModelType2,
6612
6612
  composePrompt,
6613
6613
  createUniqueUuid as createUniqueUuid2,
6614
- logger as logger3
6614
+ logger as logger3,
6615
+ truncateToCompleteSentence
6615
6616
  } from "@elizaos/core";
6617
+
6618
+ // src/constants.ts
6619
+ var TWITTER_SERVICE_NAME = "twitter";
6620
+ var TWEET_CHAR_LIMIT = 280;
6621
+
6622
+ // src/utils.ts
6623
+ async function handleNoteTweet(client, content, tweetId, mediaData) {
6624
+ try {
6625
+ const noteTweetResult = await client.requestQueue.add(
6626
+ async () => await client.twitterClient.sendNoteTweet(content, tweetId, mediaData)
6627
+ );
6628
+ if (noteTweetResult.errors && noteTweetResult.errors.length > 0) {
6629
+ const truncateContent = truncateToCompleteSentence(content, TWEET_CHAR_LIMIT - 1);
6630
+ return await this.sendStandardTweet(client, truncateContent, tweetId);
6631
+ }
6632
+ return noteTweetResult.data.notetweet_create.tweet_results.result;
6633
+ } catch (error) {
6634
+ throw new Error(`Note Tweet failed: ${error}`);
6635
+ }
6636
+ }
6637
+ async function sendStandardTweet(client, content, tweetId, mediaData) {
6638
+ try {
6639
+ const standardTweetResult = await client.requestQueue.add(
6640
+ async () => await client.twitterClient.sendTweet(content, tweetId, mediaData)
6641
+ );
6642
+ const body = await standardTweetResult.json();
6643
+ if (!body?.data?.create_tweet?.tweet_results?.result) {
6644
+ logger3.error("Error sending tweet; Bad response:", body);
6645
+ return;
6646
+ }
6647
+ return body.data.create_tweet.tweet_results.result;
6648
+ } catch (error) {
6649
+ logger3.error("Error sending standard Tweet:", error);
6650
+ throw error;
6651
+ }
6652
+ }
6653
+ async function sendTweet(client, text, mediaData = [], tweetToReplyTo) {
6654
+ if (text.length > TWEET_CHAR_LIMIT - 1) {
6655
+ return await handleNoteTweet(client, text, tweetToReplyTo, mediaData);
6656
+ } else {
6657
+ return await sendStandardTweet(client, text, tweetToReplyTo, mediaData);
6658
+ }
6659
+ }
6616
6660
  async function generateFiller(runtime, fillerType) {
6617
6661
  try {
6618
6662
  const prompt = composePrompt({
@@ -7825,9 +7869,6 @@ var _ClientBase = class _ClientBase {
7825
7869
  _ClientBase._twitterClients = {};
7826
7870
  var ClientBase = _ClientBase;
7827
7871
 
7828
- // src/constants.ts
7829
- var TWITTER_SERVICE_NAME = "twitter";
7830
-
7831
7872
  // src/interactions.ts
7832
7873
  import {
7833
7874
  ChannelType as ChannelType5,
@@ -8178,14 +8219,7 @@ var TwitterInteractionClient = class {
8178
8219
  return [];
8179
8220
  }
8180
8221
  logger7.info(`Replying to tweet ${tweetToReplyTo}`);
8181
- const replyTweetResult = await this.client.requestQueue.add(
8182
- () => this.client.twitterClient.sendTweet(response.text.substring(0, 280), tweetToReplyTo)
8183
- );
8184
- if (!replyTweetResult) {
8185
- throw new Error("Failed to create tweet response");
8186
- }
8187
- const responseBody = await replyTweetResult.json();
8188
- const tweetResult = responseBody?.data?.create_tweet?.tweet_results?.result;
8222
+ const tweetResult = await sendTweet(this.client, response.text, [], tweetToReplyTo);
8189
8223
  if (!tweetResult) {
8190
8224
  throw new Error("Failed to get tweet result from response");
8191
8225
  }
@@ -8325,8 +8359,7 @@ import {
8325
8359
  EventType as EventType3,
8326
8360
  createUniqueUuid as createUniqueUuid6,
8327
8361
  logger as logger8,
8328
- parseBooleanFromText,
8329
- truncateToCompleteSentence
8362
+ parseBooleanFromText
8330
8363
  } from "@elizaos/core";
8331
8364
  var TwitterPostClient = class {
8332
8365
  /**
@@ -8351,7 +8384,7 @@ var TwitterPostClient = class {
8351
8384
  );
8352
8385
  logger8.log(`- Auto-post: ${this.state.isTwitterEnabled ? "enabled" : "disabled"}`);
8353
8386
  logger8.log(
8354
- `- Post Interval: ${this.state?.TWITTER_POST_INTERVAL_MIN || this.runtime.getSetting("TWITTER_POST_INTERVAL_MIN")}-${this.state?.TWITTER_POST_INTERVAL_MAX || this.runtime.getSetting("TWITTER_POST_INTERVAL_MAX")} minutes`
8387
+ `- Post Interval: ${this.state?.TWITTER_POST_INTERVAL_MIN || this.runtime.getSetting("TWITTER_POST_INTERVAL_MIN") || 90}-${this.state?.TWITTER_POST_INTERVAL_MAX || this.runtime.getSetting("TWITTER_POST_INTERVAL_MAX") || 180} minutes`
8355
8388
  );
8356
8389
  logger8.log(
8357
8390
  `- Post Immediately: ${this.state?.TWITTER_POST_IMMEDIATELY || this.runtime.getSetting("TWITTER_POST_IMMEDIATELY") ? "enabled" : "disabled"}`
@@ -8371,159 +8404,17 @@ var TwitterPostClient = class {
8371
8404
  return;
8372
8405
  }
8373
8406
  const generateNewTweetLoop = async () => {
8374
- const interval = (this.state?.TWITTER_POST_INTERVAL || this.runtime.getSetting("TWITTER_POST_INTERVAL") || 30) * 60 * 1e3;
8375
- this.generateNewTweet();
8407
+ const minPostMinutes = this.state?.TWITTER_POST_INTERVAL_MIN || this.runtime.getSetting("TWITTER_POST_INTERVAL_MIN") || 90;
8408
+ const maxPostMinutes = this.state?.TWITTER_POST_INTERVAL_MAX || this.runtime.getSetting("TWITTER_POST_INTERVAL_MAX") || 180;
8409
+ const randomMinutes = Math.floor(Math.random() * (maxPostMinutes - minPostMinutes + 1)) + minPostMinutes;
8410
+ const interval = randomMinutes * 60 * 1e3;
8411
+ await this.generateNewTweet();
8376
8412
  setTimeout(generateNewTweetLoop, interval);
8377
8413
  };
8378
8414
  setTimeout(generateNewTweetLoop, 60 * 1e3);
8379
- if (this.runtime.getSetting("TWITTER_POST_IMMEDIATELY")) {
8415
+ if (this.state?.TWITTER_POST_IMMEDIATELY || this.runtime.getSetting("TWITTER_POST_IMMEDIATELY")) {
8380
8416
  await new Promise((resolve) => setTimeout(resolve, 1e3));
8381
- this.generateNewTweet();
8382
- }
8383
- }
8384
- /**
8385
- * Creates a Tweet object based on the tweet result, client information, and Twitter username.
8386
- *
8387
- * @param {any} tweetResult - The result object from the Twitter API representing a tweet.
8388
- * @param {any} client - The client object containing profile information.
8389
- * @param {string} twitterUsername - The Twitter username of the user.
8390
- * @returns {Tweet} A Tweet object with specific properties extracted from the tweet result and client information.
8391
- */
8392
- createTweetObject(tweetResult, client, twitterUsername) {
8393
- return {
8394
- id: tweetResult.rest_id,
8395
- name: client.profile.screenName,
8396
- username: client.profile.username,
8397
- text: tweetResult.legacy.full_text,
8398
- conversationId: tweetResult.legacy.conversation_id_str,
8399
- createdAt: tweetResult.legacy.created_at,
8400
- timestamp: new Date(tweetResult.legacy.created_at).getTime(),
8401
- userId: client.profile.id,
8402
- inReplyToStatusId: tweetResult.legacy.in_reply_to_status_id_str,
8403
- permanentUrl: `https://twitter.com/${twitterUsername}/status/${tweetResult.rest_id}`,
8404
- hashtags: [],
8405
- mentions: [],
8406
- photos: [],
8407
- thread: [],
8408
- urls: [],
8409
- videos: []
8410
- };
8411
- }
8412
- /**
8413
- * Processes and caches a tweet.
8414
- *
8415
- * @param {IAgentRuntime} runtime - The agent runtime.
8416
- * @param {ClientBase} client - The client object.
8417
- * @param {Tweet} tweet - The tweet to be processed and cached.
8418
- * @param {UUID} roomId - The ID of the room where the tweet will be stored.
8419
- * @param {string} rawTweetContent - The raw content of the tweet.
8420
- */
8421
- async processAndCacheTweet(runtime, client, tweet, roomId, rawTweetContent) {
8422
- await runtime.setCache(`twitter/${client.profile.username}/lastPost`, {
8423
- id: tweet.id,
8424
- timestamp: Date.now()
8425
- });
8426
- await client.cacheTweet(tweet);
8427
- logger8.log(`Tweet posted:
8428
- ${tweet.permanentUrl}`);
8429
- await runtime.ensureRoomExists({
8430
- id: roomId,
8431
- name: "Twitter Feed",
8432
- source: "twitter",
8433
- type: ChannelType6.FEED
8434
- });
8435
- await runtime.ensureParticipantInRoom(runtime.agentId, roomId);
8436
- await runtime.createMemory(
8437
- {
8438
- id: createUniqueUuid6(this.runtime, tweet.id),
8439
- entityId: runtime.agentId,
8440
- agentId: runtime.agentId,
8441
- content: {
8442
- text: rawTweetContent.trim(),
8443
- url: tweet.permanentUrl,
8444
- source: "twitter"
8445
- },
8446
- roomId,
8447
- createdAt: tweet.timestamp
8448
- },
8449
- "messages"
8450
- );
8451
- }
8452
- /**
8453
- * Handles sending a note tweet with optional media data.
8454
- *
8455
- * @param {ClientBase} client - The client object used for sending the note tweet.
8456
- * @param {string} content - The content of the note tweet.
8457
- * @param {string} [tweetId] - Optional Tweet ID to reply to.
8458
- * @param {MediaData[]} [mediaData] - Optional media data to attach to the note tweet.
8459
- * @returns {Promise<Object>} - The result of the note tweet operation.
8460
- * @throws {Error} - If the note tweet operation fails.
8461
- */
8462
- async handleNoteTweet(client, content, tweetId, mediaData) {
8463
- try {
8464
- const noteTweetResult = await client.requestQueue.add(
8465
- async () => await client.twitterClient.sendNoteTweet(content, tweetId, mediaData)
8466
- );
8467
- if (noteTweetResult.errors && noteTweetResult.errors.length > 0) {
8468
- const truncateContent = truncateToCompleteSentence(content, 280 - 1);
8469
- return await this.sendStandardTweet(client, truncateContent, tweetId);
8470
- }
8471
- return noteTweetResult.data.notetweet_create.tweet_results.result;
8472
- } catch (error) {
8473
- throw new Error(`Note Tweet failed: ${error}`);
8474
- }
8475
- }
8476
- /**
8477
- * Asynchronously sends a standard tweet using the provided Twitter client.
8478
- *
8479
- * @param {ClientBase} client - The client used to make the request.
8480
- * @param {string} content - The content of the tweet.
8481
- * @param {string} [tweetId] - Optional tweet ID to reply to.
8482
- * @param {MediaData[]} [mediaData] - Optional array of media data to attach to the tweet.
8483
- * @returns {Promise<string>} The result of sending the tweet.
8484
- */
8485
- async sendStandardTweet(client, content, tweetId, mediaData) {
8486
- try {
8487
- const standardTweetResult = await client.requestQueue.add(
8488
- async () => await client.twitterClient.sendTweet(content, tweetId, mediaData)
8489
- );
8490
- const body = await standardTweetResult.json();
8491
- if (!body?.data?.create_tweet?.tweet_results?.result) {
8492
- logger8.error("Error sending tweet; Bad response:", body);
8493
- return;
8494
- }
8495
- return body.data.create_tweet.tweet_results.result;
8496
- } catch (error) {
8497
- logger8.error("Error sending standard Tweet:", error);
8498
- throw error;
8499
- }
8500
- }
8501
- /**
8502
- * Posts a new tweet with the provided tweet content and optional media data.
8503
- *
8504
- * @param {IAgentRuntime} runtime - The runtime environment for the agent.
8505
- * @param {ClientBase} client - The Twitter client used to post the tweet.
8506
- * @param {string} tweetTextForPosting - The text content of the tweet.
8507
- * @param {UUID} roomId - The ID of the room where the tweet will be posted.
8508
- * @param {string} rawTweetContent - The raw content of the tweet.
8509
- * @param {string} twitterUsername - The username associated with the Twitter account.
8510
- * @param {MediaData[]} [mediaData] - Optional media data to be included in the tweet.
8511
- * @returns {Promise<void>} - A Promise that resolves when the tweet is successfully posted.
8512
- */
8513
- async postTweet(runtime, client, tweetTextForPosting, roomId, rawTweetContent, twitterUsername, mediaData) {
8514
- try {
8515
- logger8.log("Posting new tweet:\n");
8516
- let result;
8517
- if (tweetTextForPosting.length > 280 - 1) {
8518
- result = await this.handleNoteTweet(client, tweetTextForPosting, void 0, mediaData);
8519
- } else {
8520
- result = await this.sendStandardTweet(client, tweetTextForPosting, void 0, mediaData);
8521
- }
8522
- const tweet = this.createTweetObject(result, client, twitterUsername);
8523
- await this.processAndCacheTweet(runtime, client, tweet, roomId, rawTweetContent);
8524
- } catch (error) {
8525
- logger8.error("Error sending tweet:");
8526
- throw error;
8417
+ await this.generateNewTweet();
8527
8418
  }
8528
8419
  }
8529
8420
  /**
@@ -8623,15 +8514,12 @@ var TwitterPostClient = class {
8623
8514
  }
8624
8515
  }
8625
8516
  }
8626
- const result = await this.client.requestQueue.add(
8627
- () => this.client.twitterClient.sendTweet(text.substring(0, 280))
8628
- );
8629
- const body = await result.json();
8630
- if (!body?.data?.create_tweet?.tweet_results?.result) {
8631
- logger8.error("Error sending tweet; Bad response:", body);
8517
+ const result = await sendTweet(this.client, text, mediaData);
8518
+ if (!result) {
8519
+ logger8.error("Error sending tweet; Bad response:");
8632
8520
  return null;
8633
8521
  }
8634
- return body.data.create_tweet.tweet_results.result;
8522
+ return result;
8635
8523
  } catch (error) {
8636
8524
  logger8.error("Error posting to Twitter:", error);
8637
8525
  throw error;