diff --git a/api/uptime.php b/api/uptime.php index 6b41bee..a8fdc25 100644 --- a/api/uptime.php +++ b/api/uptime.php @@ -28,13 +28,46 @@ function ping($url) { } function notifyStatusChange($url, $oldStatus, $newStatus, $error = null) { - $recipient = 'yumeecute@aol.com'; $time = date('Y-m-d H:i:s'); + $statusEmoji = ($newStatus === 'error') ? '🚨' : '🟢'; + $statusText = strtoupper($newStatus); + + // 1. Send Telegram Push to Phone (24/7 real-time forcing) + try { + $stmt = db()->query("SELECT value FROM settings WHERE `key` = 'telegram_token'"); + $token = $stmt->fetchColumn(); + + $stmt2 = db()->query("SELECT value FROM settings WHERE `key` = 'telegram_chat_id'"); + $chatId = $stmt2->fetchColumn(); + + if ($token && $chatId) { + $msg = "$statusEmoji *MONITOR ALERT* +URL: $url +Status: *$statusText* +Time: $time +" . ($error ? "Error: $error" : ""); + $tgUrl = "https://api.telegram.org/bot$token/sendMessage"; + $data = ['chat_id' => $chatId, 'text' => $msg, 'parse_mode' => 'Markdown']; + $options = [ + 'http' => [ + 'header' => "Content-type: application/x-www-form-urlencoded +", + 'method' => 'POST', + 'content' => http_build_query($data), + ], + ]; + file_get_contents($tgUrl, false, stream_context_create($options)); + } + } catch (Exception $e) { + // Ignore errors if telegram not setup + } + + // 2. Original Email logic + $recipient = 'yumeecute@aol.com'; if ($oldStatus === 'ok' && $newStatus === 'error') { $subject = "🔴 ALERT: Website Down - $url"; - $html = " -
The following website is currently unresponsive:
URL: $url
@@ -46,8 +79,7 @@ function notifyStatusChange($url, $oldStatus, $newStatus, $error = null) { "; } elseif ($oldStatus === 'error' && $newStatus === 'ok') { $subject = "🟢 RESOLVED: Website Up - $url"; - $html = " -The following website is responding normally again:
URL: $url
@@ -79,6 +111,30 @@ function generateApiKey() { } switch ($action) { + + case 'save_telegram': + $data = json_decode(file_get_contents('php://input'), true); + $token = $data['telegram_token'] ?? ''; + $chatId = $data['telegram_chat_id'] ?? ''; + + $stmt = db()->prepare("INSERT INTO settings (`key`, value) VALUES ('telegram_token', ?) ON DUPLICATE KEY UPDATE value = VALUES(value)"); + $stmt->execute([$token]); + + $stmt = db()->prepare("INSERT INTO settings (`key`, value) VALUES ('telegram_chat_id', ?) ON DUPLICATE KEY UPDATE value = VALUES(value)"); + $stmt->execute([$chatId]); + + echo json_encode(['success' => true]); + break; + + case 'get_telegram': + $stmt = db()->query("SELECT `key`, value FROM settings WHERE `key` IN ('telegram_token', 'telegram_chat_id')"); + $res = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); + echo json_encode([ + 'telegram_token' => $res['telegram_token'] ?? '', + 'telegram_chat_id' => $res['telegram_chat_id'] ?? '' + ]); + break; + case 'settings': echo json_encode(['monitoring_enabled' => isMonitoringEnabled()]); break; diff --git a/backend_monitor.php b/backend_monitor.php new file mode 100644 index 0000000..193bd68 --- /dev/null +++ b/backend_monitor.php @@ -0,0 +1,47 @@ +query("SELECT * FROM urls WHERE is_active = 1"); + $urls = $stmt->fetchAll(); + + foreach ($urls as $row) { + $res = ping($row['url']); + + // Log latency to ensure history exists + $ins = db()->prepare("INSERT INTO uptime_logs (url_id, status, latency) VALUES (?, ?, ?)"); + $ins->execute([$row['id'], $res['status'], $res['latency']]); + + // Notify on change + notifyStatusChange($row['url'], $row['last_status'], $res['status'], $res['error']); + + // Update URL status + $upd = db()->prepare("UPDATE urls SET last_status = ?, last_latency = ?, last_checked_at = NOW() WHERE id = ?"); + $upd->execute([$res['status'], $res['latency'], $row['id']]); + + // Keep only last 500 logs per URL to save DB space + $del = db()->prepare( + "DELETE FROM uptime_logs + WHERE url_id = ? + AND id NOT IN ( + SELECT id FROM ( + SELECT id FROM uptime_logs WHERE url_id = ? ORDER BY id DESC LIMIT 500 + ) foo + )" + ); + $del->execute([$row['id'], $row['id']]); + } + } + sleep(2); // Wait 2 seconds before the next check +} + diff --git a/index.php b/index.php index f197b79..94ab47e 100644 --- a/index.php +++ b/index.php @@ -176,7 +176,8 @@ $v = time(); // Cache busting versionAplikasi ini berjalan online 24/7 di server Flatlogic. Untuk memaksa memunculkan notifikasi (wget monitor) langsung ke handphone Anda walau browser tertutup, hubungkan Telegram Bot Anda.
+ +/newbot lalu copy Token-nya.https://api.telegram.org/bot<TOKEN_ANDA>/getUpdates untuk menemukan "chat":{"id":123456789} Anda.