diff --git a/app/Controllers/OrderController.php b/app/Controllers/OrderController.php index d55caa5..4048af9 100644 --- a/app/Controllers/OrderController.php +++ b/app/Controllers/OrderController.php @@ -4,6 +4,7 @@ namespace App\Controllers; use App\Models\Order; use App\Services\OrderProcessor; +use App\Services\TelegramService; use App\Utils\View; class OrderController @@ -99,6 +100,16 @@ class OrderController $processor = new OrderProcessor(); $count = $processor->process($orderId, $type, $_FILES['csv_file']['tmp_name']); + // Send Telegram Notification + $telegramService = new TelegramService(); + $typeLabel = ($type === 'block') ? 'Bloqueio' : (($type === 'unblock') ? 'Desbloqueio' : $type); + + $telegramService->sendOrderNotification([ + 'id' => $orderId, + 'title' => $title, + 'type' => $typeLabel + ], $count); + $_SESSION['flash_success'] = "Ordem criada com sucesso! $count domínios processados."; View::redirect('/admin/orders'); diff --git a/app/Controllers/SettingsController.php b/app/Controllers/SettingsController.php new file mode 100644 index 0000000..0e650b3 --- /dev/null +++ b/app/Controllers/SettingsController.php @@ -0,0 +1,58 @@ +getAll(); + + View::render('layouts.admin', [ + 'title' => 'Configurações', + 'content' => __DIR__ . '/../../resources/views/admin/settings/index.php', + 'settings' => $settings + ]); + } + + public function update() + { + $settingModel = new Setting(); + + foreach ($_POST as $key => $value) { + $settingModel->set($key, $value); + } + + $_SESSION['flash_success'] = "Configurações atualizadas com sucesso!"; + View::redirect('/admin/settings'); + } + + public function testTelegram() + { + header('Content-Type: application/json'); + + $telegramService = new TelegramService(); + + // Use sendOrderNotification to test with the configured template + $response = $telegramService->sendOrderNotification([ + 'id' => '12345', + 'title' => 'Teste de Integração', + 'type' => 'Teste' + ], 999); + + $result = json_decode($response, true); + + if ($result && isset($result['ok']) && $result['ok']) { + echo json_encode(['success' => true, 'message' => 'Mensagem enviada com sucesso!']); + } else { + $error = $result['description'] ?? 'Erro desconhecido ao contatar API do Telegram.'; + echo json_encode(['success' => false, 'message' => 'Falha ao enviar: ' . $error]); + } + exit; + } +} diff --git a/app/Models/Setting.php b/app/Models/Setting.php new file mode 100644 index 0000000..3e07081 --- /dev/null +++ b/app/Models/Setting.php @@ -0,0 +1,45 @@ +conn->prepare("SELECT value FROM {$this->table} WHERE `key` = :key"); + $stmt->execute(['key' => $key]); + $result = $stmt->fetch(); + + return $result ? $result['value'] : $default; + } + + public function set($key, $value) + { + // Check if exists + if ($this->get($key) !== null) { + $stmt = $this->conn->prepare("UPDATE {$this->table} SET value = :value WHERE `key` = :key"); + } else { + $stmt = $this->conn->prepare("INSERT INTO {$this->table} (`key`, value) VALUES (:key, :value)"); + } + + return $stmt->execute(['key' => $key, 'value' => $value]); + } + + public function getAll() + { + $stmt = $this->conn->query("SELECT * FROM {$this->table}"); + $results = $stmt->fetchAll(); + + $settings = []; + foreach ($results as $row) { + $settings[$row['key']] = $row['value']; + } + + return $settings; + } +} diff --git a/app/Services/TelegramService.php b/app/Services/TelegramService.php new file mode 100644 index 0000000..55e2e05 --- /dev/null +++ b/app/Services/TelegramService.php @@ -0,0 +1,61 @@ +settingModel = new Setting(); + $this->token = $this->settingModel->get('telegram_bot_token'); + $this->chatId = $this->settingModel->get('telegram_chat_id'); + } + + public function sendMessage($message) + { + if (empty($this->token) || empty($this->chatId)) { + return false; + } + + $url = "https://api.telegram.org/bot{$this->token}/sendMessage"; + $data = [ + 'chat_id' => $this->chatId, + 'text' => $message, + 'parse_mode' => 'HTML' + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + curl_close($ch); + + return $response; + } + + public function sendOrderNotification($order, $domainCount) + { + $template = $this->settingModel->get('telegram_message_template'); + + if (empty($template)) { + $template = "Nova Ordem Cadastrada!\n\nTítulo: {title}\nTipo: {type}\nDomínios: {total_domains}"; + } + + $message = str_replace( + ['{title}', '{type}', '{total_domains}', '{order_id}'], + [$order['title'], $order['type'], $domainCount, $order['id']], + $template + ); + + return $this->sendMessage($message); + } +} diff --git a/app/routes.php b/app/routes.php index 7e67ec3..3c18e43 100644 --- a/app/routes.php +++ b/app/routes.php @@ -43,6 +43,11 @@ $router->get('/admin/orders/create', [\App\Controllers\OrderController::class, ' $router->post('/admin/orders/store', [\App\Controllers\OrderController::class, 'store']); $router->get('/admin/orders/view/{id}', [\App\Controllers\OrderController::class, 'view']); +// Settings +$router->get('/admin/settings', [\App\Controllers\SettingsController::class, 'index']); +$router->post('/admin/settings/update', [\App\Controllers\SettingsController::class, 'update']); +$router->post('/admin/settings/test-telegram', [\App\Controllers\SettingsController::class, 'testTelegram']); + $router->addMiddleware(\App\Middleware\AdminMiddleware::class); // API Routes diff --git a/database/migration_create_settings.php b/database/migration_create_settings.php new file mode 100644 index 0000000..ea8d7f4 --- /dev/null +++ b/database/migration_create_settings.php @@ -0,0 +1,23 @@ +load(); + +try { + $conn = Database::getInstance()->getConnection(); + + $sql = "CREATE TABLE IF NOT EXISTS settings ( + `key` VARCHAR(255) PRIMARY KEY, + `value` TEXT + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; + + $conn->exec($sql); + echo "Tabela 'settings' criada com sucesso!" . PHP_EOL; + +} catch (PDOException $e) { + echo "Erro ao criar tabela: " . $e->getMessage() . PHP_EOL; +} diff --git a/resources/views/admin/settings/index.php b/resources/views/admin/settings/index.php new file mode 100644 index 0000000..0e48dff --- /dev/null +++ b/resources/views/admin/settings/index.php @@ -0,0 +1,193 @@ +
Configure as notificações automáticas para o Telegram.
+ + +