Skip to content

Commit

Permalink
Merge pull request #620 from odilitime/fix-twitter
Browse files Browse the repository at this point in the history
fix: handle when tweet_results is empty better
  • Loading branch information
lalalune authored Nov 27, 2024
2 parents c144a13 + 254cce5 commit b2cb0b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
5 changes: 4 additions & 1 deletion packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,12 @@ export class TwitterPostClient {
await this.client.twitterClient.sendTweet(content)
);
const body = await result.json();
if (!body?.data?.create_tweet?.tweet_results?.result) {
console.error("Error sending tweet; Bad response:", body);
return;
}
const tweetResult = body.data.create_tweet.tweet_results.result;

// console.dir({ tweetResult }, { depth: Infinity });
const tweet = {
id: tweetResult.rest_id,
name: this.client.profile.screenName,
Expand Down
48 changes: 26 additions & 22 deletions packages/client-twitter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,29 +182,33 @@ export async function sendTweet(
previousTweetId
)
);
// Parse the response
const body = await result.json();
const tweetResult = body.data.create_tweet.tweet_results.result;

const finalTweet: Tweet = {
id: tweetResult.rest_id,
text: tweetResult.legacy.full_text,
conversationId: tweetResult.legacy.conversation_id_str,
//createdAt:
timestamp: tweetResult.timestamp * 1000,
userId: tweetResult.legacy.user_id_str,
inReplyToStatusId: tweetResult.legacy.in_reply_to_status_id_str,
permanentUrl: `https://twitter.com/${twitterUsername}/status/${tweetResult.rest_id}`,
hashtags: [],
mentions: [],
photos: [],
thread: [],
urls: [],
videos: [],
};

sentTweets.push(finalTweet);
previousTweetId = finalTweet.id;

// if we have a response
if (body?.data?.create_tweet?.tweet_results?.result) {
// Parse the response
const tweetResult = body.data.create_tweet.tweet_results.result;
const finalTweet: Tweet = {
id: tweetResult.rest_id,
text: tweetResult.legacy.full_text,
conversationId: tweetResult.legacy.conversation_id_str,
//createdAt:
timestamp: tweetResult.timestamp * 1000,
userId: tweetResult.legacy.user_id_str,
inReplyToStatusId: tweetResult.legacy.in_reply_to_status_id_str,
permanentUrl: `https://twitter.com/${twitterUsername}/status/${tweetResult.rest_id}`,
hashtags: [],
mentions: [],
photos: [],
thread: [],
urls: [],
videos: [],
};
sentTweets.push(finalTweet);
previousTweetId = finalTweet.id;
} else {
console.error("Error sending chunk", chunk, "repsonse:", body);
}

// Wait a bit between tweets to avoid rate limiting issues
await wait(1000, 2000);
Expand Down

0 comments on commit b2cb0b8

Please sign in to comment.