diff --git a/src/bot/handler.js b/src/bot/handler.js index 747b69a..7e1f750 100644 --- a/src/bot/handler.js +++ b/src/bot/handler.js @@ -1081,6 +1081,8 @@ Digite /help para ver todos os comandos disponíveis! return; } + console.log(`📝 Criando sessão para usuário ${userId}, serial: ${serialNumber}`); + // Criar sessão para coletar informações this.userSessions.set(userId, { action: 'adicionar_onu_fiberhome_step1', @@ -1088,6 +1090,8 @@ Digite /help para ver todos os comandos disponíveis! timestamp: Date.now() }); + console.log(`✅ Sessão criada. Sessões ativas:`, this.userSessions.size); + // Perguntar o alias/nome do usuário await this.bot.sendMessage(chatId, `➕ **Adicionar ONU Fiberhome**\n\n` + @@ -1103,57 +1107,76 @@ Digite /help para ver todos os comandos disponíveis! */ async handleUserSessionInput(chatId, text, userId, session) { try { - switch (session.action) { - case 'adicionar_onu_fiberhome_step1': - // Recebeu o nome do usuário, agora perguntar pelo tipo da ONU - session.usuario = text.trim(); - session.action = 'adicionar_onu_fiberhome_step2'; - this.userSessions.set(userId, session); + console.log(`🔄 Processando input de sessão. Action: ${session.action}, Type: ${session.type}, Text: ${text}`); + + // Verificar se é sessão baseada em action (Fiberhome ONU) + if (session.action) { + switch (session.action) { + case 'adicionar_onu_fiberhome_step1': + // Recebeu o nome do usuário, agora perguntar pelo tipo da ONU + session.usuario = text.trim(); + session.action = 'adicionar_onu_fiberhome_step2'; + this.userSessions.set(userId, session); - const keyboard = { - inline_keyboard: [ - [{ text: '🔌 AN5506-02-B (2 portas)', callback_data: 'fh_onu_type_AN5506-02-B' }], - [{ text: '🔌 AN5506-01-A (1 porta)', callback_data: 'fh_onu_type_AN5506-01-A' }], - [{ text: '❌ Cancelar', callback_data: 'cancel_action' }] - ] - }; + const keyboard = { + inline_keyboard: [ + [{ text: '🔌 AN5506-02-B (2 portas)', callback_data: 'fh_onu_type_AN5506-02-B' }], + [{ text: '🔌 AN5506-01-A (1 porta)', callback_data: 'fh_onu_type_AN5506-01-A' }], + [{ text: '❌ Cancelar', callback_data: 'cancel_action' }] + ] + }; - await this.bot.sendMessage(chatId, - `📡 **Porta PON:** ${session.portAlias}\n\n` + - '🔌 Selecione o **tipo da ONU**:', - { reply_markup: keyboard, parse_mode: 'Markdown' } - ); - break; + await this.bot.sendMessage(chatId, + `👤 **Usuário:** ${session.usuario}\n\n` + + '🔌 Selecione o **tipo da ONU**:', + { reply_markup: keyboard, parse_mode: 'Markdown' } + ); + return; - case 'adicionar_onu_fiberhome_step5': - // Recebeu confirmação de VLAN customizada - const vlanInput = text.trim(); - - if (vlanInput && !isNaN(vlanInput)) { - const vlan = parseInt(vlanInput); - if (vlan >= 1 && vlan <= 4094) { - session.vlan = vlan; + case 'adicionar_onu_fiberhome_step3': + // Recebeu confirmação de VLAN customizada + const vlanInput = text.trim(); + + if (vlanInput && !isNaN(vlanInput)) { + const vlan = parseInt(vlanInput); + if (vlan >= 1 && vlan <= 4094) { + session.vlan = vlan; + } else { + await this.bot.sendMessage(chatId, '❌ VLAN inválida. Usando VLAN padrão 1002.'); + session.vlan = 1002; + } } else { - await this.bot.sendMessage(chatId, '❌ VLAN inválida. Usando VLAN padrão 1002.'); session.vlan = 1002; } - } else { - session.vlan = 1002; - } - // Limpar sessão - this.userSessions.delete(userId); + // Limpar sessão + this.userSessions.delete(userId); - // Executar adição da ONU - await this.executarAdicaoOnuFiberhome(chatId, session); - break; - - default: - // Sessão desconhecida, limpar - this.userSessions.delete(userId); - await this.bot.sendMessage(chatId, '❌ Sessão expirada. Por favor, comece novamente.'); - break; + // Executar adição da ONU + await this.executarAdicaoOnuFiberhome(chatId, session); + return; + } } + + // Verificar se é sessão baseada em type (outros fluxos) + if (session.type) { + switch (session.type) { + case 'waiting_custom_command': + await this.executeCustomCommand(chatId, session.connectionId, text); + this.userSessions.delete(userId); + return; + + case 'waiting_username_disconnect': + await this.handleDisconnectUser(chatId, text.trim()); + this.userSessions.delete(userId); + return; + } + } + + // Estado desconhecido + console.log(`❓ Estado de sessão desconhecido: action=${session.action}, type=${session.type}`); + this.userSessions.delete(userId); + await this.bot.sendMessage(chatId, '❓ Estado de sessão desconhecido. Tente novamente.'); } catch (error) { console.error('❌ Erro ao processar input da sessão:', error); this.userSessions.delete(userId); @@ -1548,25 +1571,8 @@ Digite /help para ver todos os comandos disponíveis! } /** - * Lida com input de usuário durante sessões específicas + * MÉTODO REMOVIDO - handleUserSessionInput duplicado foi mesclado acima */ - async handleUserSessionInput(chatId, text, userId, session) { - switch (session.type) { - case 'waiting_custom_command': - await this.executeCustomCommand(chatId, session.connectionId, text); - break; - - case 'waiting_username_disconnect': - await this.handleDisconnectUser(chatId, text.trim()); - break; - - default: - await this.bot.sendMessage(chatId, '❓ Estado de sessão desconhecido. Tente novamente.'); - } - - // Limpar sessão - this.userSessions.delete(userId); - } /** * Executa comando personalizado em uma conexão SSH