UNPKG

skailan-conversations

Version:

Servicio de conversaciones y mensajería para Skailan

134 lines 4.54 kB
export class ConfigureTelegramChannel { channelRepository; constructor(channelRepository) { this.channelRepository = channelRepository; } async execute(request) { try { // Validar parámetros requeridos if (!request.channelId) { return { success: false, error: "Channel ID is required", }; } if (!request.organizationId) { return { success: false, error: "Organization ID is required", }; } if (!request.botToken) { return { success: false, error: "Telegram bot token is required", }; } // Validar formato del bot token (debe empezar con números seguidos de ':') if (!this.isValidBotToken(request.botToken)) { return { success: false, error: "Invalid Telegram bot token format", }; } // Buscar el canal existente const existingChannel = await this.channelRepository.findById(request.channelId); if (!existingChannel) { return { success: false, error: "Channel not found", }; } // Verificar que el canal pertenece a la organización if (existingChannel.organizationId !== request.organizationId) { return { success: false, error: "Channel does not belong to the specified organization", }; } // Verificar que es un canal de Telegram if (existingChannel.type !== "telegram") { return { success: false, error: "Channel is not a Telegram channel", }; } // Crear la configuración de Telegram const telegramConfig = { botToken: request.botToken, webhookUrl: request.webhookUrl, allowedUpdates: request.allowedUpdates || [ "message", "edited_message", "callback_query", ], }; // Actualizar la configuración del canal existingChannel.updateConfig(telegramConfig); // Activar el canal si está configurado correctamente if (existingChannel.isConfigured()) { existingChannel.activate(); } // Guardar los cambios const updatedChannel = await this.channelRepository.update(existingChannel); return { success: true, channel: updatedChannel, }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : "Unknown error occurred", }; } } /** * Valida el formato del bot token de Telegram * Formato esperado: números:letras_números * Ejemplo: 123456789:ABCdefGHIjklMNOpqrsTUVwxyz */ isValidBotToken(token) { const botTokenRegex = /^\d+:[A-Za-z0-9_-]+$/; return botTokenRegex.test(token); } /** * Valida la URL del webhook */ isValidWebhookUrl(url) { if (!url) return true; // URL opcional try { const parsedUrl = new URL(url); return parsedUrl.protocol === "https:" || parsedUrl.protocol === "http:"; } catch { return false; } } /** * Valida los tipos de actualizaciones permitidos */ isValidAllowedUpdates(updates) { if (!updates) return true; // Opcional const validUpdates = [ "message", "edited_message", "channel_post", "edited_channel_post", "inline_query", "chosen_inline_result", "callback_query", "shipping_query", "pre_checkout_query", "poll", "poll_answer", "my_chat_member", "chat_member", "chat_join_request", ]; return updates.every((update) => validUpdates.includes(update)); } } //# sourceMappingURL=ConfigureTelegramChannel.js.map