UNPKG

plazbot-cli

Version:
99 lines (83 loc) 3.5 kB
import { Command } from 'commander'; import { Agent } from 'plazbot'; import { getStoredCredentials } from '../../utils/credentials'; import { logger } from '../../utils/logger'; import { createSpinner, createTable, theme, section, statusBadge } from '../../utils/ui'; export const channelsCommand = new Command('channels') .description('Listar agentes con canales de WhatsApp asignados') .option('--dev', 'Usar ambiente de desarrollo', false) .action(async (options: any) => { try { const credentials = await getStoredCredentials(); const agent = new Agent({ workspaceId: credentials.workspace, apiKey: credentials.apiKey, zone: credentials.zone, ...(options.dev && { customUrl: "http://localhost:5090" }) }); const spinner = createSpinner('Cargando canales...'); spinner.start(); const agents = await agent.getAgents(); spinner.stop(); console.log(section('Canales WhatsApp')); const whatsappAgents = (agents || []).filter((a: any) => a.channels && a.channels.some((c: any) => c.channel === 'whatsapp') ); if (whatsappAgents.length === 0) { console.log(theme.muted('\n No hay agentes con WhatsApp configurado')); console.log(theme.muted(' Usa el wizard: plazbot agent create\n')); return; } const rows = whatsappAgents.map((a: any) => { const waChannel = a.channels.find((c: any) => c.channel === 'whatsapp'); return [ a.id || a._id, a.name, waChannel?.key || 'N/A', statusBadge(a.enable !== false), ]; }); console.log(createTable(['Agent ID', 'Nombre', 'Numero WhatsApp', 'Estado'], rows)); } catch (error) { const message = error instanceof Error ? error.message : 'Error desconocido'; logger.error(message); process.exit(1); } }); export const assignCommand = new Command('assign') .description('Asignar un agente a un numero de WhatsApp') .argument('<phone>', 'Numero de WhatsApp') .argument('<agentId>', 'ID del agente') .option('--dev', 'Usar ambiente de desarrollo', false) .action(async (phone: string, agentId: string, options: any) => { try { const credentials = await getStoredCredentials(); const agent = new Agent({ workspaceId: credentials.workspace, apiKey: credentials.apiKey, zone: credentials.zone, ...(options.dev && { customUrl: "http://localhost:5090" }) }); const spinner = createSpinner('Cargando agente...'); spinner.start(); const agentData = await agent.getAgentById({ id: agentId }); spinner.stop(); const channels = agentData.channels || []; // Verificar si ya tiene este canal const existing = channels.findIndex((c: any) => c.channel === 'whatsapp' && c.key === phone); if (existing >= 0) { logger.warning('Este numero ya esta asignado a este agente'); return; } // Agregar canal channels.push({ channel: 'whatsapp', key: phone, multianswer: false }); const updateSpinner = createSpinner('Asignando...'); updateSpinner.start(); await agent.updateAgent(agentId, { channels }); updateSpinner.succeed(`Agente "${agentData.name}" asignado a WhatsApp ${phone}`); } catch (error) { const message = error instanceof Error ? error.message : 'Error desconocido'; logger.error(message); process.exit(1); } });