Skip to content
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"jsdom": "^29.0.2",
"playwright": "^1.59.1",
"tailwindcss": "^4",
"twitter-api-v2": "^1.29.0",
"typescript": "^5",
"vitest": "^4.1.4"
}
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions scripts/tweet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Post a tweet to @swfactory_dev via Twitter API v2.
*
* Usage: npx tsx scripts/tweet.ts "Your tweet text here"
*
* Required env vars (OAuth 1.0a User Context):
* TWITTER_CONSUMER_KEY
* TWITTER_SECRET_KEY
* TWITTER_ACCESS_TOKEN
* TWITTER_ACCESS_TOKEN_SECRET
*/

import { TwitterApi } from "twitter-api-v2";

const text = process.argv[2];

if (!text) {
console.error("Usage: npx tsx scripts/tweet.ts <text>");
process.exit(1);
}

if (text.length > 280) {
console.error(`Tweet is ${text.length} chars (max 280). Trim it first.`);
process.exit(1);
}

const required = [
"TWITTER_CONSUMER_KEY",
"TWITTER_SECRET_KEY",
"TWITTER_ACCESS_TOKEN",
"TWITTER_ACCESS_TOKEN_SECRET",
] as const;

const missing = required.filter((k) => !process.env[k]);
if (missing.length) {
console.error(`Missing env vars: ${missing.join(", ")}`);
process.exit(1);
}

const client = new TwitterApi({
appKey: process.env.TWITTER_CONSUMER_KEY!,
appSecret: process.env.TWITTER_SECRET_KEY!,
accessToken: process.env.TWITTER_ACCESS_TOKEN!,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET!,
});

async function main() {
try {
const { data } = await client.v2.tweet(text);
const url = `https://x.com/swfactory_dev/status/${data.id}`;
console.log(`Posted: ${url}`);
} catch (err: unknown) {
if (err instanceof Error) {
console.error(`Failed to post tweet: ${err.message}`);
} else {
console.error("Failed to post tweet:", err);
}
process.exit(1);
}
}

main();
Loading