import { Command } from 'commander' import { handleError } from '@/shared/utils/error-handler' import { formatOutput } from '@/shared/utils/output' import { parseLimitOption, withWhatsAppClient } from './shared' async function listAction( chat: string, options: { account?: string; pretty?: boolean; limit?: string }, ): Promise { try { const limit = parseLimitOption(options.limit, 25) const messages = await withWhatsAppClient(options, (client) => client.getMessages(chat, limit)) console.log(formatOutput(messages, options.pretty)) process.exit(0) } catch (error) { handleError(error as Error) } } async function sendAction(chat: string, text: string, options: { account?: string; pretty?: boolean }): Promise { try { const message = await withWhatsAppClient(options, (client) => client.sendMessage(chat, text)) console.log(formatOutput(message, options.pretty)) process.exit(0) } catch (error) { handleError(error as Error) } } async function editAction( chat: string, messageId: string, text: string, options: { account?: string; pretty?: boolean }, ): Promise { try { const message = await withWhatsAppClient(options, (client) => client.editMessage(chat, messageId, text)) console.log(formatOutput(message, options.pretty)) process.exit(0) } catch (error) { handleError(error as Error) } } async function reactAction( chat: string, messageId: string, emoji: string, options: { account?: string; pretty?: boolean; fromMe?: boolean }, ): Promise { try { await withWhatsAppClient(options, (client) => client.sendReaction(chat, messageId, emoji, options.fromMe)) console.log(formatOutput({ success: true, chat, message_id: messageId, emoji }, options.pretty)) process.exit(0) } catch (error) { handleError(error as Error) } } export const messageCommand = new Command('message') .description('WhatsApp message commands') .addCommand( new Command('list') .description('List messages from a chat (JID or phone number)') .argument('', 'Chat JID (e.g. 12025551234@s.whatsapp.net) or phone number') .option('--limit ', 'Number of messages to fetch', '25') .option('--account ', 'Use a specific WhatsApp account') .option('--pretty', 'Pretty print JSON output') .action(listAction), ) .addCommand( new Command('send') .description('Send a text message to a chat') .argument('', 'Chat JID or phone number') .argument('', 'Message text') .option('--account ', 'Use a specific WhatsApp account') .option('--pretty', 'Pretty print JSON output') .action(sendAction), ) .addCommand( new Command('edit') .description('Edit one of your own messages (within ~15 minutes)') .argument('', 'Chat JID or phone number') .argument('', 'Message ID to edit') .argument('', 'New message text') .option('--account ', 'Use a specific WhatsApp account') .option('--pretty', 'Pretty print JSON output') .action(editAction), ) .addCommand( new Command('react') .description('React to a message with an emoji') .argument('', 'Chat JID or phone number') .argument('', 'Message ID to react to') .argument('', 'Emoji reaction') .option('--from-me', 'React to your own outgoing message') .option('--account ', 'Use a specific WhatsApp account') .option('--pretty', 'Pretty print JSON output') .action(reactAction), )