<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Vaqt zonasini belgilash
date_default_timezone_set("Asia/Tashkent"); // Toshkent uchun vaqt zonasi

// Telegram API tokeni
$botToken = "7529140894:AAGj8EQZObKWNtsbnbhcQzuWfpL-9GK6nFE"; // Bu yerga tokenni kiriting
$apiUrl = "https://api.telegram.org/bot" . $botToken;

// Xabar yuborish funksiyasi
function sendMessage($chatId, $message) {
    global $apiUrl;

    $url = $apiUrl . "/sendMessage";
    $data = [
        'chat_id' => $chatId,
        'text' => $message
    ];

    $options = [
        'http' => [
            'header'  => "Content-Type: application/json\r\n",
            'method'  => 'POST',
            'content' => json_encode($data)
        ]
    ];

    $context = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

    if ($response === false) {
        error_log("Xabar yuborishda xato: " . json_encode($data));
    }
}

// Kiruvchi ma'lumotlarni olish
$content = file_get_contents("php://input");
if ($content === false) {
    die("Kirish ma'lumotini olishda xato.");
}

error_log("Kelayotgan JSON ma'lumot: " . $content); // JSON ma'lumotni logga yozish

$update = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    error_log("JSONni o'qishda xato: " . json_last_error_msg());
    die("Yaroqsiz JSON ma'lumot.");
}

// Xabarni qayta ishlash
if (isset($update['message'])) {
    $message = $update['message'];
    $chatId = $message['chat']['id'];

    $firstName = isset($message['from']['first_name']) ? $message['from']['first_name'] : 'Noma';
    $lastName = isset($message['from']['last_name']) ? $message['from']['last_name'] : 'Noma';
    $username = isset($message['from']['username']) ? $message['from']['username'] : 'Noma';
    $userId = $message['from']['id'];
    $languageCode = isset($message['from']['language_code']) ? $message['from']['language_code'] : 'Noma';
    $isBot = isset($message['from']['is_bot']) && $message['from']['is_bot'] ? 'Ha' : 'Yo\'q';

    // Telefon raqamini olish (agar mavjud bo'lsa)
    $phoneNumber = isset($message['contact']['phone_number']) ? $message['contact']['phone_number'] : 'Noma';

    // Foydalanuvchi haqida ma'lumotlarni tayyorlash
    $userInfo = "Siz haqingizdagi ma'lumotlar:\n";
    $userInfo .= "👤 Ism: $firstName\n";
    $userInfo .= "👤 Familiya: $lastName\n";
    $userInfo .= "🌐 Foydalanuvchi nomi: @$username\n";
    $userInfo .= "🆔 ID: $userId\n";
    $userInfo .= "🌍 Til kodi: $languageCode\n";
    $userInfo .= "🤖 Botmi: $isBot\n";
    $userInfo .= "📞 Telefon raqami: $phoneNumber\n";

    // Telegramga javob yuborish
    sendMessage($chatId, $userInfo);
}
?>
