Files

42 lines
1.4 KiB
PHP
Executable File

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../../../shared/utils/helpers.php';
require_once __DIR__ . '/../../../shared/auth/jwt.php';
require_once __DIR__ . '/../../../shared/database/connection.php';
// Verificar autenticación
$userData = JWTAuth::authenticate();
if (!$userData) {
jsonResponse(['success' => false, 'error' => 'No autenticado'], 401);
}
// Verificar permiso
if (!hasPermission('manage_recipients', 'telegram')) {
jsonResponse(['success' => false, 'error' => 'No tienes permiso para crear destinatarios de Telegram.'], 403);
}
$input = json_decode(file_get_contents('php://input'), true);
$nombre = trim($input['nombre'] ?? '');
$telegram_id = trim($input['telegram_id'] ?? '');
$tipo = trim($input['tipo'] ?? 'canal');
if (empty($nombre) || empty($telegram_id)) {
jsonResponse(['success' => false, 'error' => 'Faltan datos'], 400);
}
try {
$db = getDB();
$stmt = $db->prepare("
INSERT INTO destinatarios_telegram (nombre, telegram_id, tipo, activo)
VALUES (?, ?, ?, 1)
");
$stmt->execute([$nombre, $telegram_id, $tipo]);
jsonResponse(['success' => true]);
} catch (PDOException $e) {
if ($e->getCode() == 23000) {
jsonResponse(['success' => false, 'error' => 'Este ID de Telegram ya está registrado'], 409);
}
jsonResponse(['success' => false, 'error' => $e->getMessage()], 500);
}