Skip to content

Commit

Permalink
Merge pull request #79 from PLhery/feat/threads
Browse files Browse the repository at this point in the history
feature: Tweet thread helper
  • Loading branch information
alkihis authored Sep 21, 2021
2 parents 6e45e22 + 0613299 commit 2e1b31a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
26 changes: 26 additions & 0 deletions doc/v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ For streaming API, see [Streaming part](./streaming.md).
* [Post and retrieve tweets](#Postandretrievetweets)
* [Create a tweet](#Createatweet)
* [Reply to a tweet](#Replytoatweet)
* [Tweet a thread of tweet](#Tweetathreadoftweet)
* [Delete a tweet](#Deleteatweet)
* [Embed a tweet](#Embedatweet)
* [Get a single tweet](#Getasingletweet)
Expand Down Expand Up @@ -271,6 +272,31 @@ await client.v1.reply(
);
```

### <a name='Tweetathreadoftweet'></a>Tweet a thread of tweet

Post multiple tweets at one time.
Tweet mode is `extended` by default.

**Method**: `.tweetThread()`

**Endpoint**: `statuses/update.json`

**Right level**: `Read-write`

**Arguments**:
- `tweets: (SendTweetV1Params | string)[]`

**Returns**: `TweetV1[]`: Created tweets (in the right order), first sent first position

**Example**
```ts
await client.v1.tweetThread([
'Hello, lets talk about Twitter!',
{ status: 'Twitter is a fantastic social network. Look at this:', media_ids: '20' },
'This thread is automatically made with twitter-api-v2 :D',
]);
```

### <a name='Deleteatweet'></a>Delete a tweet

Delete a tweet that belongs to you.
Expand Down
26 changes: 26 additions & 0 deletions src/v1/client.v1.write.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ export default class TwitterApiv1ReadWrite extends TwitterApiv1ReadOnly {
return this.post<TweetV1>('statuses/update.json', queryParams);
}

/**
* Post a series of tweets.
* https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update
*/
public async tweetThread(tweets: (SendTweetV1Params | string)[]) {
const postedTweets: TweetV1[] = [];

for (const tweet of tweets) {
// Retrieve the last sent tweet
const lastTweet = postedTweets.length ? postedTweets[postedTweets.length - 1] : null;
// Build the tweet query params
const queryParams: SendTweetV1Params = { ...(typeof tweet === 'string' ? ({ status: tweet }) : tweet) };
// Reply to an existing tweet if needed
const inReplyToId = lastTweet ? lastTweet.id_str : queryParams.in_reply_to_status_id;
const status = queryParams.status;

if (inReplyToId) {
postedTweets.push(await this.reply(status, inReplyToId, queryParams));
} else {
postedTweets.push(await this.tweet(status, queryParams));
}
}

return postedTweets;
}

/**
* Reply to an existing tweet. Shortcut to `.tweet` with tweaked parameters.
* https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update
Expand Down

0 comments on commit 2e1b31a

Please sign in to comment.