Skip to content
Open
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
11 changes: 8 additions & 3 deletions src/commands/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export function registerPostCommands(program: Command, ctx: CliContext): void {
.command('tweet')
.description('Post a new tweet')
.argument('<text>', 'Tweet text')
.action(async (text: string) => {
.option('--quote <tweet-id-or-url>', 'Quote tweet: embed the given tweet')
.action(async (text: string, cmdOpts: { quote?: string }) => {
const opts = program.opts();
const timeoutMs = ctx.resolveTimeoutFromOptions(opts);
const quoteDepth = ctx.resolveQuoteDepthFromOptions(opts);
Expand Down Expand Up @@ -58,10 +59,14 @@ export function registerPostCommands(program: Command, ctx: CliContext): void {

const client = new TwitterClient({ cookies, timeoutMs, quoteDepth });
const mediaIds = await uploadMediaOrExit(client, media, ctx);
const result = await client.tweet(text, mediaIds);
const quoteTweetId = cmdOpts.quote ? ctx.extractTweetId(cmdOpts.quote) : undefined;
if (quoteTweetId) {
console.error(`${ctx.p('info')}Quoting tweet: ${quoteTweetId}`);
}
const result = await client.tweet(text, mediaIds, quoteTweetId);

if (result.success) {
console.log(`${ctx.p('ok')}Tweet posted successfully!`);
console.log(`${ctx.p('ok')}${quoteTweetId ? 'Quote tweet' : 'Tweet'} posted successfully!`);
console.log(formatTweetUrlLine(result.tweetId, ctx.getOutput()));
} else {
console.error(`${ctx.p('err')}Failed to post tweet: ${result.error}`);
Expand Down
10 changes: 7 additions & 3 deletions src/lib/twitter-client-posting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { buildTweetCreateFeatures } from './twitter-client-features.js';
import type { CreateTweetResponse, TweetResult } from './twitter-client-types.js';

export interface TwitterClientPostingMethods {
tweet(text: string, mediaIds?: string[]): Promise<TweetResult>;
tweet(text: string, mediaIds?: string[], quoteTweetId?: string): Promise<TweetResult>;
reply(text: string, replyToTweetId: string, mediaIds?: string[]): Promise<TweetResult>;
}

Expand All @@ -20,8 +20,8 @@ export function withPosting<TBase extends AbstractConstructor<TwitterClientBase>
/**
* Post a new tweet
*/
async tweet(text: string, mediaIds?: string[]): Promise<TweetResult> {
const variables = {
async tweet(text: string, mediaIds?: string[], quoteTweetId?: string): Promise<TweetResult> {
const variables: Record<string, unknown> = {
tweet_text: text,
dark_request: false,
media: {
Expand All @@ -31,6 +31,10 @@ export function withPosting<TBase extends AbstractConstructor<TwitterClientBase>
semantic_annotation_ids: [],
};

if (quoteTweetId) {
variables.attachment_url = `https://x.com/i/web/status/${quoteTweetId}`;
}

const features = buildTweetCreateFeatures();

return this.createTweet(variables, features);
Expand Down