86 lines
2.8 KiB
PHP
86 lines
2.8 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
// Get Telegram Update
|
|
$content = file_get_contents("php://input");
|
|
$update = json_decode($content, true);
|
|
|
|
if (!$update || !isset($update['message'])) {
|
|
exit;
|
|
}
|
|
|
|
$message = $update['message'];
|
|
$chatId = $message['chat']['id'];
|
|
$text = $message['text'] ?? '';
|
|
|
|
if (empty($text)) {
|
|
exit;
|
|
}
|
|
|
|
// Get Telegram Token from DB
|
|
$db = db();
|
|
$stmt = $db->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 = "đź‘‹ <b>Welcome to Uptime Monitor Bot!</b>\n\n";
|
|
$reply .= "Your Chat ID is: <code>$chatId</code>\n\n";
|
|
|
|
if (empty($currentChatId)) {
|
|
$reply .= "âś… <b>Success!</b> 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 = "<b>Uptime Monitor Status</b>\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: <code>$chatId</code>";
|
|
sendTelegramMessage($chatId, $reply, $token);
|
|
} |