Update notif_dispatch.go
This commit is contained in:
@@ -129,13 +129,12 @@ func DispatchTicketRespuestaAdmin(ticket *models.ProyectoTicket, contenido strin
|
||||
go SendTicketRespuestaPortalUser(portalUser.Email, portalUser.Nombre, proyectoNombre, ticket.Titulo, contenido, portalURL)
|
||||
}
|
||||
if cfg.CanalTelegram && portalUser.TelegramChatID != "" {
|
||||
ts := NewTelegramService()
|
||||
msg := fmt.Sprintf("💬 <b>El equipo respondió tu ticket</b>\nProyecto: <b>%s</b>\nTicket: <b>%s</b>\n\n%s\n\n🔗 %s",
|
||||
escapeTelegramHTML(proyectoNombre),
|
||||
escapeTelegramHTML(ticket.Titulo),
|
||||
escapeTelegramHTML(contenido),
|
||||
telegramHTMLLink(portalURL, "Ver en el portal"))
|
||||
if err := ts.SendMessageWithToken(portalUser.TelegramChatID, msg, getAdminBotToken()); err != nil {
|
||||
if err := sendTelegramPortalUser(portalUser.TelegramChatID, msg); err != nil {
|
||||
log.Printf("[Notif] Error telegram portal_user %d: %v", portalUser.ID, err)
|
||||
}
|
||||
}
|
||||
@@ -186,13 +185,12 @@ func DispatchFacturaSubida(factura *models.Factura) {
|
||||
go SendFacturaSubidaPortalUser(u.Email, u.Nombre, clienteNombre, factura.Numero, factura.Monto, factura.Moneda, portalURL)
|
||||
}
|
||||
if cfg.CanalTelegram && u.TelegramChatID != "" {
|
||||
ts := NewTelegramService()
|
||||
msg := fmt.Sprintf("🧾 <b>Nueva factura disponible</b>\nNúmero: <b>%s</b>\nMonto: %s %.2f\n\n🔗 %s",
|
||||
escapeTelegramHTML(factura.Numero),
|
||||
escapeTelegramHTML(factura.Moneda),
|
||||
factura.Monto,
|
||||
telegramHTMLLink(portalURL, "Ver en el portal"))
|
||||
if err := ts.SendMessageWithToken(u.TelegramChatID, msg, getAdminBotToken()); err != nil {
|
||||
if err := sendTelegramPortalUser(u.TelegramChatID, msg); err != nil {
|
||||
log.Printf("[Notif] Error telegram portal_user %d: %v", u.ID, err)
|
||||
}
|
||||
}
|
||||
@@ -223,6 +221,31 @@ func getAdminBotToken() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// sendTelegramPortalUser intenta enviar usando todos los bots activos hasta que uno funcione.
|
||||
// Esto evita fallos cuando el usuario se vinculó con un bot distinto al primer bot activo.
|
||||
func sendTelegramPortalUser(chatID string, mensaje string) error {
|
||||
configs, err := models.GetAllTelegramConfigs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var lastErr error
|
||||
for _, cfg := range configs {
|
||||
if !cfg.Activo || cfg.BotToken == "" {
|
||||
continue
|
||||
}
|
||||
ts := &TelegramService{BotToken: cfg.BotToken}
|
||||
if err := ts.SendMessage(chatID, mensaje); err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if lastErr != nil {
|
||||
return lastErr
|
||||
}
|
||||
return fmt.Errorf("no hay bots de Telegram activos para enviar")
|
||||
}
|
||||
|
||||
func sendTelegramAdmin(mensaje string) {
|
||||
configs, err := models.GetAllTelegramConfigs()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user