Repository navigation
Expand file tree
/
Copy pathx-post.mjs
More file actions
54 lines (47 loc) · 1.41 KB
/
Copy pathx-post.mjs
File metadata and controls
54 lines (47 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env node
import { TwitterApi } from 'twitter-api-v2';
import {
getCredentialHelpText,
getPostHelpText,
loadCredentials,
parsePostArgs,
} from './lib/x-cli-common.mjs';
const parsed = parsePostArgs(process.argv.slice(2));
if (parsed.help) {
console.log(getPostHelpText());
process.exit(0);
}
if (parsed.error) {
console.error(`❌ ${parsed.error}`);
console.error(getPostHelpText());
process.exit(1);
}
const credentialState = loadCredentials({ configPath: parsed.configPath });
if (!credentialState.credentials) {
console.error(`❌ ${credentialState.error}`);
console.error(getCredentialHelpText(credentialState.searchedPaths));
process.exit(1);
}
const client = new TwitterApi({
appKey: credentialState.credentials.consumerKey,
appSecret: credentialState.credentials.consumerSecret,
accessToken: credentialState.credentials.accessToken,
accessSecret: credentialState.credentials.accessTokenSecret,
});
const payload = { text: parsed.text };
if (parsed.replyTo) {
payload.reply = { in_reply_to_tweet_id: parsed.replyTo };
}
if (parsed.quoteTo) {
payload.quote_tweet_id = parsed.quoteTo;
}
try {
const { data } = await client.v2.tweet(payload);
console.log(`✅ Posted: https://x.com/i/status/${data.id}`);
} catch (error) {
console.error('❌ Failed to post tweet:', error.message);
if (error.data) {
console.error(JSON.stringify(error.data, null, 2));
}
process.exit(1);
}