UNPKG

plazbot-cli

Version:
78 lines (66 loc) 2.64 kB
import { Command } from 'commander'; import { Message } from 'plazbot'; import { getStoredCredentials } from '../../utils/credentials'; import { logger } from '../../utils/logger'; import { createSpinner, theme } from '../../utils/ui'; import readline from 'readline'; import chalk from 'chalk'; export const whatsappChatCommand = new Command('chat') .description('Chat interactivo directo por WhatsApp') .argument('<phone>', 'Numero de telefono destino (con codigo de pais)') .option('--dev', 'Usar ambiente de desarrollo', false) .action(async (phone: string, options: any) => { try { const credentials = await getStoredCredentials(); const messageClient = new Message({ workspaceId: credentials.workspace, apiKey: credentials.apiKey, zone: credentials.zone, ...(options.dev && { customUrl: "http://localhost:5090" }) }); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); console.clear(); console.log(); console.log(chalk.hex('#25D366')(' ┌' + '─'.repeat(50) + '┐')); console.log(chalk.hex('#25D366')(' │') + chalk.bold(` WhatsApp Chat`).padEnd(60) + chalk.hex('#25D366')('│')); console.log(chalk.hex('#25D366')(' │') + chalk.gray(` Destino: ${phone}`).padEnd(60) + chalk.hex('#25D366')('│')); console.log(chalk.hex('#25D366')(' │') + chalk.gray(' /exit para salir').padEnd(60) + chalk.hex('#25D366')('│')); console.log(chalk.hex('#25D366')(' └' + '─'.repeat(50) + '┘')); console.log(); const ask = () => { rl.question(chalk.hex('#25D366')(' > '), async (message) => { if (!message.trim()) { ask(); return; } if (message.toLowerCase() === '/exit') { console.log(chalk.gray('\n Chat terminado.\n')); rl.close(); return; } try { const spinner = createSpinner('Enviando...'); spinner.start(); await messageClient.onWhatsappMessage({ message, to: phone, }); spinner.succeed('Enviado'); ask(); } catch (error) { const msg = error instanceof Error ? error.message : 'Error'; console.log(chalk.hex('#EF5350')(` ✖ ${msg}`)); ask(); } }); }; ask(); } catch (error) { const message = error instanceof Error ? error.message : 'Error desconocido'; logger.error(message); process.exit(1); } });