prepare("SELECT value FROM settings WHERE name = 'telegram_bot_token'"); $stmt->execute(); $token = $stmt->fetchColumn(); if (!$token) { exit; } // Check if telegram_chat_id is already set, if not, save it automatically! $stmt = $db->prepare("SELECT value FROM settings WHERE name = 'telegram_chat_id'"); $stmt->execute(); $currentChatId = $stmt->fetchColumn(); if (empty($currentChatId)) { $stmt = $db->prepare("INSERT INTO settings (name, value) VALUES (?, ?) ON DUPLICATE KEY UPDATE value = ?"); $stmt->execute([$chatId, $chatId, $chatId]); } function sendTelegramMessage($chatId, $text, $token) { $url = "https://api.telegram.org/bot$token/sendMessage"; $data = [ 'chat_id' => $chatId, 'text' => $text, 'parse_mode' => 'HTML' ]; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); curl_exec($ch); curl_close($ch); } if ($text === '/start') { $reply = "👋 Welcome to Uptime Monitor Bot!\n\n"; $reply .= "Your Chat ID is: $chatId\n\n"; if (empty($currentChatId)) { $reply .= "✅ Success! Your Chat ID has been automatically saved to the Uptime Dashboard. You will now receive notifications here.\n\n"; } else { $reply .= "Use this Chat ID in the Settings of your Uptime Dashboard if it's not already set.\n\n"; } $reply .= "Available commands:\n/status - Check current URL statuses"; sendTelegramMessage($chatId, $reply, $token); } elseif ($text === '/status') { $urls = $db->query("SELECT * FROM urls")->fetchAll(); if (empty($urls)) { $reply = "No URLs are currently being monitored."; } else { $reply = "Uptime Monitor Status\n\n"; foreach ($urls as $url) { $emoji = ($url['status'] === 'up') ? '✅' : (($url['status'] === 'down') ? '❌' : '❓'); $reply .= $emoji . " " . $url['name'] . "\n"; $reply .= "Status: " . strtoupper($url['status']) . "\n"; $reply .= "Response: " . $url['response_time'] . "ms\n"; $reply .= "URL: " . $url['url'] . "\n\n"; } } sendTelegramMessage($chatId, $reply, $token); } elseif ($text === '/myid') { $reply = "Your Chat ID is: $chatId"; sendTelegramMessage($chatId, $reply, $token); }