Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
Omar Sánchez Pizarro
2026-01-20 03:21:50 +01:00
parent 19932854ca
commit 81bf0675ed
32 changed files with 3081 additions and 932 deletions

View File

@@ -1,27 +1,66 @@
import express from 'express';
import { basicAuthMiddleware } from '../middlewares/auth.js';
import { getConfig, reloadConfig } from '../services/redis.js';
import { readFileSync } from 'fs';
import yaml from 'yaml';
import { PATHS } from '../config/constants.js';
import { getTelegramConfig, setTelegramConfig } from '../services/mongodb.js';
const router = express.Router();
// Obtener configuración de Telegram del usuario autenticado
router.get('/config', basicAuthMiddleware, async (req, res) => {
try {
const username = req.user.username;
const config = await getTelegramConfig(username);
if (!config) {
return res.json({
token: '',
channel: '',
enable_polling: false
});
}
res.json(config);
} catch (error) {
console.error('Error obteniendo configuración de Telegram:', error);
res.status(500).json({ error: error.message });
}
});
// Guardar configuración de Telegram del usuario autenticado
router.put('/config', basicAuthMiddleware, async (req, res) => {
try {
const username = req.user.username;
const { token, channel, enable_polling } = req.body;
if (!token || !channel) {
return res.status(400).json({ error: 'Token y channel son requeridos' });
}
await setTelegramConfig(username, {
token,
channel,
enable_polling: enable_polling || false
});
res.json({ success: true });
} catch (error) {
console.error('Error guardando configuración de Telegram:', error);
res.status(500).json({ error: error.message });
}
});
// Obtener threads/topics de Telegram
router.get('/threads', basicAuthMiddleware, async (req, res) => {
try {
let config = getConfig();
if (!config) {
config = yaml.parse(readFileSync(PATHS.CONFIG, 'utf8'));
}
const username = req.user.username;
const config = await getTelegramConfig(username);
const token = config?.telegram_token;
const channel = config?.telegram_channel;
if (!token || !channel) {
if (!config || !config.token || !config.channel) {
return res.status(400).json({ error: 'Token o canal de Telegram no configurados' });
}
const token = config.token;
const channel = config.channel;
// Convertir el canal a chat_id si es necesario
let chatId = channel;
if (channel.startsWith('@')) {