<?php
// Run this file ONCE in your browser to register the webhook.
// Delete or rename it afterward for security.

$BOT_TOKEN   = "8578052646:AAE6E6VcVj7oCoJd5NfoCgjTAgS4D8B0cg0";

// Auto-detect your domain and path
$protocol    = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host        = $_SERVER['HTTP_HOST'];
$dir         = dirname($_SERVER['REQUEST_URI']);
$webhook_url = $protocol . '://' . $host . rtrim($dir, '/') . '/chat_webhook.php';

$url = "https://api.telegram.org/bot{$BOT_TOKEN}/setWebhook";

$ch = curl_init($url);
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_POSTFIELDS     => json_encode(['url' => $webhook_url]),
    CURLOPT_HTTPHEADER     => ['Content-Type: application/json'],
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT        => 10,
]);
$result = curl_exec($ch);
curl_close($ch);

$resp = json_decode($result, true);
?>
<!DOCTYPE html>
<html>
<head><style>
    body { font-family: sans-serif; padding: 40px; background: #f4f4f2; }
    .box { background: #fff; border-radius: 12px; padding: 24px; max-width: 500px; border: 1px solid rgba(0,0,0,0.08); }
    .ok  { color: #3d9e6a; font-weight: 500; }
    .err { color: #c0392b; font-weight: 500; }
    code { background: #f0f0ee; padding: 2px 6px; border-radius: 4px; font-size: 13px; }
</style></head>
<body>
<div class="box">
    <h2 style="margin-bottom:16px;">Webhook Registration</h2>
    <?php if (!empty($resp['ok'])): ?>
        <p class="ok">✓ Webhook registered successfully!</p>
        <p style="margin-top:12px;font-size:13px;color:#666;">Webhook URL set to:<br><code><?php echo htmlspecialchars($webhook_url); ?></code></p>
        <p style="margin-top:16px;font-size:13px;color:#888;">You can now delete or rename this file. Any message you send to your Telegram bot will be instantly delivered to the chat page.</p>
    <?php else: ?>
        <p class="err">✗ Registration failed</p>
        <pre style="font-size:12px;margin-top:12px;"><?php echo htmlspecialchars(json_encode($resp, JSON_PRETTY_PRINT)); ?></pre>
    <?php endif; ?>
</div>
</body>
</html>