import { Command } from 'commander' import { cliOutput } from '@/shared/utils/cli-output' import type { AccountOption } from './shared' import { getClient } from './shared' interface MessageResult { messaging_product?: string contacts?: Array<{ input: string; wa_id: string }> messages?: Array<{ id: string }> error?: string } type MessageOptions = AccountOption & { language?: string components?: string caption?: string filename?: string } export async function sendAction(to: string, text: string, options: MessageOptions): Promise { try { const client = await getClient(options) const response = await client.sendTextMessage(to, text) return response } catch (error) { return { error: (error as Error).message } } } export async function sendTemplateAction( to: string, templateName: string, options: MessageOptions, ): Promise { try { const client = await getClient(options) const languageCode = options.language ?? 'en_US' let components: unknown[] | undefined if (options.components) { try { components = JSON.parse(options.components) as unknown[] } catch { return { error: 'Invalid --components JSON' } } } const response = await client.sendTemplateMessage(to, templateName, languageCode, components) return response } catch (error) { return { error: (error as Error).message } } } export async function sendReactionAction( to: string, messageId: string, emoji: string, options: MessageOptions, ): Promise { try { const client = await getClient(options) const response = await client.sendReaction(to, messageId, emoji) return response } catch (error) { return { error: (error as Error).message } } } export async function sendImageAction(to: string, imageUrl: string, options: MessageOptions): Promise { try { const client = await getClient(options) const response = await client.sendImageMessage(to, imageUrl, options.caption) return response } catch (error) { return { error: (error as Error).message } } } export async function sendDocumentAction( to: string, documentUrl: string, options: MessageOptions, ): Promise { try { const client = await getClient(options) const response = await client.sendDocumentMessage(to, documentUrl, options.filename, options.caption) return response } catch (error) { return { error: (error as Error).message } } } export const messageCommand = new Command('message') .description('Message commands') .addCommand( new Command('send') .description('Send a text message') .argument('', 'Recipient phone number') .argument('', 'Message text') .option('--account ', 'Account ID') .option('--pretty', 'Pretty print JSON output') .action(async (to: string, text: string, opts: MessageOptions) => { cliOutput(await sendAction(to, text, opts), opts.pretty) }), ) .addCommand( new Command('send-template') .description('Send a template message') .argument('', 'Recipient phone number') .argument('', 'Template name') .option('--language ', 'Language code', 'en_US') .option('--components ', 'Template components as JSON string') .option('--account ', 'Account ID') .option('--pretty', 'Pretty print JSON output') .action(async (to: string, templateName: string, opts: MessageOptions) => { cliOutput(await sendTemplateAction(to, templateName, opts), opts.pretty) }), ) .addCommand( new Command('send-reaction') .description('Send a reaction to a message') .argument('', 'Recipient phone number') .argument('', 'Message ID to react to') .argument('', 'Emoji reaction') .option('--account ', 'Account ID') .option('--pretty', 'Pretty print JSON output') .action(async (to: string, messageId: string, emoji: string, opts: MessageOptions) => { cliOutput(await sendReactionAction(to, messageId, emoji, opts), opts.pretty) }), ) .addCommand( new Command('send-image') .description('Send an image message') .argument('', 'Recipient phone number') .argument('', 'Image URL') .option('--caption ', 'Image caption') .option('--account ', 'Account ID') .option('--pretty', 'Pretty print JSON output') .action(async (to: string, imageUrl: string, opts: MessageOptions) => { cliOutput(await sendImageAction(to, imageUrl, opts), opts.pretty) }), ) .addCommand( new Command('send-document') .description('Send a document message') .argument('', 'Recipient phone number') .argument('', 'Document URL') .option('--filename ', 'Document filename') .option('--caption ', 'Document caption') .option('--account ', 'Account ID') .option('--pretty', 'Pretty print JSON output') .action(async (to: string, documentUrl: string, opts: MessageOptions) => { cliOutput(await sendDocumentAction(to, documentUrl, opts), opts.pretty) }), )