Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

Commit

Permalink
Switch to v2 of the Twitter API
Browse files Browse the repository at this point in the history
  • Loading branch information
derkweijers committed Aug 21, 2023
1 parent e31dd16 commit 866dba9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
5 changes: 1 addition & 4 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ AMQP_URL=
DEBUG=
DB_LOCATION=
SENTRY_DSN=
API_KEY=
API_SECRET=
ACCESS_TOKEN=
ACCESS_TOKEN_SECRET=
BEARER_TOKEN=
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snapi-twitter-bot",
"version": "1.5.0",
"version": "1.6.0",
"description": "The official Twitter bot for the Spaceflight News API.",
"main": "app.js",
"scripts": {
Expand Down
22 changes: 13 additions & 9 deletions src/utils/TwitterClientV2.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
import {TweetV2PostTweetResult, TwitterApi} from 'twitter-api-v2';
import {TweetV2PostTweetResult, TwitterApi, TwitterApiReadWrite} from 'twitter-api-v2';
import {Tweet} from "../entity/Tweet";

class TwitterClientV2 {
private client = new TwitterApi({
appKey: process.env.API_KEY!,
appSecret: process.env.API_SECRET!,
accessToken: process.env.ACCESS_TOKEN!,
accessSecret: process.env.ACCESS_TOKEN_SECRET!,
}).v2;
private client: TwitterApiReadWrite

constructor(bearerToken?: string) {
// Proccess.env.BEARER_TOKEN can be empty, so we need to check for that.
if (!bearerToken) {
throw new Error("No bearer token provided");
}

this.client = new TwitterApi(bearerToken).readWrite;
}

async sendTweet(tweet: Tweet): Promise<TweetV2PostTweetResult | void> {
const tweetText = `New ${tweet.type} from ${tweet.newsSite.name}: ${tweet.title} - ${tweet.url} #space #spaceflight #news`;

if (process.env.DEBUG) {
return console.log("DEBUG!", tweetText);
} else {
return this.client.tweet(tweetText);
return this.client.v2.tweet(tweetText)
}
}
}

export const twitterClientV2: TwitterClientV2 = new TwitterClientV2();
export const twitterClientV2: TwitterClientV2 = new TwitterClientV2(process.env.BEARER_TOKEN);

0 comments on commit 866dba9

Please sign in to comment.