<?php

require_once __DIR__ . '/config.php';

header('Content-Type: text/plain; charset=utf-8');

$webhookUrl = 'https://logic.nl2s2026h.xyz/bot/webhook.php';

if (!defined('BOT_TOKEN')) {
    exit("ERROR: BOT_TOKEN is not defined\n");
}

if (!defined('WEBHOOK_SECRET') || trim((string) WEBHOOK_SECRET) === '') {
    exit("ERROR: WEBHOOK_SECRET is not defined\n");
}

$url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/setWebhook';

$data = [
    'url' => $webhookUrl,
    'secret_token' => (string) WEBHOOK_SECRET,
    'drop_pending_updates' => 'true',
    'max_connections' => 40,
    'allowed_updates' => json_encode([
        'message',
        'inline_query',
        'callback_query',
        'chat_member'
    ])
];

$ch = curl_init($url);

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $data,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 15,
]);

$response = curl_exec($ch);

if ($response === false) {
    echo "CURL ERROR: " . curl_error($ch) . "\n";
} else {
    echo $response . "\n";
}

curl_close($ch);

echo "\n--- WEBHOOK INFO ---\n";

$infoUrl = 'https://api.telegram.org/bot' . BOT_TOKEN . '/getWebhookInfo';

$ch = curl_init($infoUrl);

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 5,
    CURLOPT_TIMEOUT => 15,
]);

echo curl_exec($ch);

curl_close($ch);