package services import ( "fmt" "net/http" "net/url" "github.com/sujit-baniya/fiber-boilerplate/pkg/models" ) // ProcesarMensajeTelegramUmind adapta un mensaje entrante del canal Telegram // de un tenant al mismo motor que atiende el widget web // (ProcessWidgetMessage) y responde usando el bot token propio del canal // (no el bot interno de staff). La sesión se separa por chat_id con un // prefijo para no colisionar con session_ids del widget. func ProcesarMensajeTelegramUmind(canal *models.UmindCanal, chatID int64, texto string) error { tenant, err := models.GetUmindTenantByID(canal.TenantID) if err != nil || !tenant.Activo { return fmt.Errorf("tenant no encontrado o inactivo: %w", err) } credenciales, err := DescifrarCredencialesCanal(canal.CredencialesEnc) if err != nil { return fmt.Errorf("credenciales del canal corruptas: %w", err) } botToken := credenciales["bot_token"] if botToken == "" { return fmt.Errorf("el canal no tiene bot_token configurado") } sessionID := fmt.Sprintf("tg:%d", chatID) respuesta, err := ProcessWidgetMessage(tenant, sessionID, texto) if err != nil { return fmt.Errorf("error del agente: %w", err) } return (&TelegramService{}).SendMessageWithToken(chatID, respuesta, botToken) } // RegistrarWebhookTelegram le dice a Telegram a qué URL mandar los updates // del bot — se llama una vez al crear el canal (o al reconfigurar el token). func RegistrarWebhookTelegram(botToken, webhookURL string) error { if botToken == "" || webhookURL == "" { return fmt.Errorf("bot_token y webhookURL son requeridos") } api := fmt.Sprintf("https://api.telegram.org/bot%s/setWebhook?url=%s", botToken, url.QueryEscape(webhookURL)) resp, err := telegramHTTPClient.Get(api) if err != nil { return fmt.Errorf("no se pudo contactar la API de Telegram: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("Telegram respondió %d al registrar el webhook", resp.StatusCode) } return nil }