This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e31dd16
commit 866dba9
Showing
3 changed files
with
15 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,4 @@ AMQP_URL= | |
DEBUG= | ||
DB_LOCATION= | ||
SENTRY_DSN= | ||
API_KEY= | ||
API_SECRET= | ||
ACCESS_TOKEN= | ||
ACCESS_TOKEN_SECRET= | ||
BEARER_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |