<?php
// Bot tokeningizni kiriting
define('BOT_TOKEN', '8930798194:AAH3X2tLaAdc6dwBWqI4McmJjdLR3yIlbSU');
define('API_URL', 'https://api.telegram.org/bot' . BOT_TOKEN . '/');

// Telegramdan kelgan ma'lumotlarni o'qib olish
Php;
$content = file_get_contents("php://input");
$update = json_decode($content, true);

if (!$update) {
    exit;
}

// Xabar kelganligini tekshirish
if (isset($update['message'])) {
    $chat_id = $update['message']['chat']['id'];
    $text = trim($update['message']['text']);

    // Test savoli va to'g'ri javob (Soddalashtirilgan holat)
    // Haqiqiy loyihada buni ma'lumotlar bazasi (MySQL) yoki sessiya orqali qilish tavsiya etiladi.
    $question = "O'zbekistonning poytaxti qaysi shahar?\n\nA) Samarqand\nB) Toshkent\nC) Buxoro";
    $correct_answer = 'b'; // To'g'ri javob varianti

    if ($text == '/start') {
        // Testni boshlash uchun xabar va tugmalar yuborish
        sendQuestion($chat_id, $question);
    } else {
        // Foydalanuvchi yuborgan javobni tekshirish
        $user_answer = strtolower($text);
        
        if ($user_answer == 'a' || $user_answer == 'b' || $user_answer == 'c') {
            if ($user_answer == $correct_answer) {
                sendMessage($chat_id, "Tabriklayman! To'g'ri javob 🎉");
            } else {
                sendMessage($chat_id, "Afsuski, noto'g'ri javob. To'g'ri javob: B) Toshkent");
            }
        } else {
            sendMessage($chat_id, "Iltimos, faqat A, B yoki C harflaridan birini yuboring!");
        }
    }
}

// Xabar yuborish uchun yordamchi funksiya
function sendMessage($chat_id, $text) {
    $url = API_URL . "sendMessage?chat_id=" . $chat_id . "&text=" . urlencode($text);
    file_get_contents($url);
}

// Savol va variantlarni yuborish funksiyasi
function sendQuestion($chat_id, $question) {
    // Inline tugmalar orqali ham qilish mumkin, lekin oddiy matn ko'rinishida yozish ham mumkin:
    $text = "<b>Siz uchun test savoli:</b>\n\n" . $question . "\n\n<i>Javobingizni A, B yoki C ko'rinishida yuboring:</i>";
    
    $url = API_URL . "sendMessage?chat_id=" . $chat_id . "&text=" . urlencode($text) . "&parse_mode=HTML";
    file_get_contents($url);
}
?>