Skip to content

Commit

Permalink
Merge pull request #1773 from ishowvel/development
Browse files Browse the repository at this point in the history
feat: make twitter client be optional
  • Loading branch information
0x4007 authored Oct 24, 2024
2 parents 87d949e + 2c5187d commit b43c64e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
20 changes: 12 additions & 8 deletions src/twitter/delete-tweet.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { twitterClient } from "./twitter";

export async function deleteTweet(id: string) {
try {
const { data } = await twitterClient.v2.deleteTweet(id);
if (data?.deleted) {
console.log(`Successfully deleted tweet, id: ${id}`);
} else {
console.log(`Could not delete tweet, id ${id}`);
if (twitterClient) {
try {
const { data } = await twitterClient.v2.deleteTweet(id);
if (data?.deleted) {
console.log(`Successfully deleted tweet, id: ${id}`);
} else {
console.log(`Could not delete tweet, id ${id}`);
}
} catch (error) {
console.error("Error deleting tweet", error);
}
} catch (error) {
console.error("Error deleting tweet", error);
} else {
console.log("Skipping deleting a tweet due to missing env variables");
}
}
16 changes: 10 additions & 6 deletions src/twitter/post-tweet.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { twitterClient } from "./twitter";

export async function postTweet(status: string) {
try {
const { data } = await twitterClient.v2.tweet(status);
console.log(`Tweet posted successfully, id: ${data.id}, text: ${data.text}`);
return data;
} catch (error) {
console.error("Error posting tweet", error);
if (twitterClient) {
try {
const { data } = await twitterClient.v2.tweet(status);
console.log(`Tweet posted successfully, id: ${data.id}, text: ${data.text}`);
return data;
} catch (error) {
console.error("Error posting tweet", error);
}
} else {
console.log("Skipping posting a tweet due to missing env variables");
}
}
18 changes: 11 additions & 7 deletions src/twitter/twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ const apiKeySecret = process.env.TWITTER_API_KEY_SECRET;
const accessToken = process.env.TWITTER_ACCESS_TOKEN;
const accessTokenSecret = process.env.TWITTER_ACCESS_TOKEN_SECRET;

let twitterClient: TwitterApi | undefined;

if (!apiKey || !apiKeySecret || !accessToken || !accessTokenSecret) {
throw new Error("Twitter environment variables are not set");
console.log("Twitter environment variables not found! Skipping sync to social media.");
} else {
twitterClient = new TwitterApi({
appKey: apiKey,
appSecret: apiKeySecret,
accessToken: accessToken,
accessSecret: accessTokenSecret,
});
}

export const twitterClient = new TwitterApi({
appKey: apiKey,
appSecret: apiKeySecret,
accessToken: accessToken,
accessSecret: accessTokenSecret,
});
export { twitterClient };

export default {
postTweet,
Expand Down

0 comments on commit b43c64e

Please sign in to comment.