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

feat: make twitter client be optional #1773

Merged
merged 2 commits into from
Oct 24, 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
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
Loading