Integração com Telegram
This commit is contained in:
@@ -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');
|
||||
|
||||
|
||||
58
app/Controllers/SettingsController.php
Normal file
58
app/Controllers/SettingsController.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\Services\TelegramService;
|
||||
use App\Utils\View;
|
||||
|
||||
class SettingsController
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$settingModel = new Setting();
|
||||
$settings = $settingModel->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;
|
||||
}
|
||||
}
|
||||
45
app/Models/Setting.php
Normal file
45
app/Models/Setting.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Model;
|
||||
|
||||
class Setting extends Model
|
||||
{
|
||||
protected $table = 'settings';
|
||||
protected $primaryKey = 'key';
|
||||
|
||||
public function get($key, $default = null)
|
||||
{
|
||||
$stmt = $this->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;
|
||||
}
|
||||
}
|
||||
61
app/Services/TelegramService.php
Normal file
61
app/Services/TelegramService.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Setting;
|
||||
|
||||
class TelegramService
|
||||
{
|
||||
private $token;
|
||||
private $chatId;
|
||||
private $settingModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->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 = "<b>Nova Ordem Cadastrada!</b>\n\n<b>Título:</b> {title}\n<b>Tipo:</b> {type}\n<b>Domínios:</b> {total_domains}";
|
||||
}
|
||||
|
||||
$message = str_replace(
|
||||
['{title}', '{type}', '{total_domains}', '{order_id}'],
|
||||
[$order['title'], $order['type'], $domainCount, $order['id']],
|
||||
$template
|
||||
);
|
||||
|
||||
return $this->sendMessage($message);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user