import { Command } from 'commander' import { cliOutput } from '@/shared/utils/cli-output' import type { WebexMessage } from '../../webex/types' import type { BotOption } from './shared' import { getClient } from './shared' interface MessageResult { id?: string ref?: string roomId?: string roomRef?: string text?: string markdown?: string html?: string personEmail?: string created?: string messages?: Array<{ id: string ref: string roomId: string roomRef: string text?: string personEmail: string created: string }> deleted?: string error?: string } function formatMessage(message: WebexMessage): MessageResult { return { id: message.id, ref: message.ref, roomId: message.roomId, roomRef: message.roomRef, text: message.text, markdown: message.markdown, html: message.html, personEmail: message.personEmail, created: message.created, } } export async function sendAction( space: string, text: string, options: BotOption & { markdown?: boolean; parent?: string }, ): Promise { try { const client = await getClient(options) const message = await client.sendMessage(space, text, { markdown: options.markdown, parentId: options.parent }) return formatMessage(message) } catch (error) { return { error: (error as Error).message } } } export async function replyAction( space: string, parentId: string, text: string, options: BotOption & { markdown?: boolean }, ): Promise { try { const client = await getClient(options) const message = await client.sendMessage(space, text, { markdown: options.markdown, parentId }) return formatMessage(message) } catch (error) { return { error: (error as Error).message } } } export async function repliesAction( space: string, parentId: string, options: BotOption & { max?: string }, ): Promise { try { const client = await getClient(options) const max = options.max ? parseInt(options.max, 10) : 50 const messages = await client.listReplies(space, parentId, { max }) return { messages: messages.map((msg) => ({ id: msg.id, ref: msg.ref, roomId: msg.roomId, roomRef: msg.roomRef, text: msg.text, personEmail: msg.personEmail, created: msg.created, })), } } catch (error) { return { error: (error as Error).message } } } export async function dmAction( email: string, text: string, options: BotOption & { markdown?: boolean }, ): Promise { try { const client = await getClient(options) const message = await client.sendDirectMessage(email, text, { markdown: options.markdown }) return formatMessage(message) } catch (error) { return { error: (error as Error).message } } } export async function listAction(space: string, options: BotOption & { max?: string }): Promise { try { const client = await getClient(options) const max = options.max ? parseInt(options.max, 10) : 50 const messages = await client.listMessages(space, { max }) return { messages: messages.map((msg) => ({ id: msg.id, ref: msg.ref, roomId: msg.roomId, roomRef: msg.roomRef, text: msg.text, personEmail: msg.personEmail, created: msg.created, })), } } catch (error) { return { error: (error as Error).message } } } export async function getAction(messageId: string, options: BotOption): Promise { try { const client = await getClient(options) const message = await client.getMessage(messageId) return formatMessage(message) } catch (error) { return { error: (error as Error).message } } } export async function deleteAction(messageId: string, options: BotOption): Promise { try { const client = await getClient(options) await client.deleteMessage(messageId) return { deleted: messageId } } catch (error) { return { error: (error as Error).message } } } export async function editAction( messageId: string, space: string, text: string, options: BotOption & { markdown?: boolean }, ): Promise { try { const client = await getClient(options) const message = await client.editMessage(messageId, space, text, { markdown: options.markdown }) return formatMessage(message) } catch (error) { return { error: (error as Error).message } } } export const messageCommand = new Command('message') .description('Message commands') .addCommand( new Command('send') .description('Send a message to a space') .argument('', 'Space/Room ID') .argument('', 'Message text') .option('--markdown', 'Send as markdown') .option('--parent ', 'Reply within a thread (parent message ID)') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (space: string, text: string, opts: BotOption & { markdown?: boolean; parent?: string }) => { cliOutput(await sendAction(space, text, opts), opts.pretty) }), ) .addCommand( new Command('reply') .description('Reply to a message in a thread') .argument('', 'Space/Room ID') .argument('', 'Parent message ID') .argument('', 'Reply text') .option('--markdown', 'Send as markdown') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (space: string, parent: string, text: string, opts: BotOption & { markdown?: boolean }) => { cliOutput(await replyAction(space, parent, text, opts), opts.pretty) }), ) .addCommand( new Command('replies') .description('List replies in a thread') .argument('', 'Space/Room ID') .argument('', 'Parent message ID') .option('--max ', 'Number of replies to fetch', '50') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (space: string, parent: string, opts: BotOption & { max?: string }) => { cliOutput(await repliesAction(space, parent, opts), opts.pretty) }), ) .addCommand( new Command('dm') .description('Send a direct message by recipient email') .argument('', 'Recipient email address') .argument('', 'Message text') .option('--markdown', 'Send as markdown') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (email: string, text: string, opts: BotOption & { markdown?: boolean }) => { cliOutput(await dmAction(email, text, opts), opts.pretty) }), ) .addCommand( new Command('list') .description('List messages in a space') .argument('', 'Space/Room ID') .option('--max ', 'Number of messages to fetch', '50') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (space: string, opts: BotOption & { max?: string }) => { cliOutput(await listAction(space, opts), opts.pretty) }), ) .addCommand( new Command('get') .description('Get a single message') .argument('', 'Message ID') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (messageId: string, opts: BotOption) => { cliOutput(await getAction(messageId, opts), opts.pretty) }), ) .addCommand( new Command('delete') .description('Delete a message') .argument('', 'Message ID') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (messageId: string, opts: BotOption) => { cliOutput(await deleteAction(messageId, opts), opts.pretty) }), ) .addCommand( new Command('edit') .description('Edit a message') .argument('', 'Message ID') .argument('', 'Space/Room ID') .argument('', 'New message text') .option('--markdown', 'Send as markdown') .option('--bot ', 'Use specific bot') .option('--pretty', 'Pretty print JSON output') .action(async (messageId: string, space: string, text: string, opts: BotOption & { markdown?: boolean }) => { cliOutput(await editAction(messageId, space, text, opts), opts.pretty) }), )