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

fix: handle when tweet_results is empty better #620

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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