<?php

// ----------------------------------------------------------------------------
//  Telegram Bot with:
// ----------------------------------------------------------------------------

// Bot API Token (Get this from BotFather)
$bot_token = "7806510181:AAE5Hjx9HOJbrK1__s9qRLP_rxn8ciyRRvI";

// Source Channel Username or ID (with '-100...' for channels)
$source_channel ="-1002458733993";

// API base URL
$api = "https://api.telegram.org/bot{$bot_token}/";

$START_EFFECT_ID = "5104841245755180586";

// ----------------------------------------------------------------------------

// Retrieve update
$input = json_decode(file_get_contents("php://input"), true);
$message = $input['message'] ?? [];

if (isset($message['text'])) {
    $text = trim($message['text']);
    $chat_id = $message['chat']['id'];

    if ($text == "/start") {
        sendWelcomeMessage($chat_id);
    } else if ($text == "/random") {
        forwardRandomMessage($api, $chat_id, $source_channel);
    } else {
        // Unknown command
        file_get_contents($api . "sendMessage?" . http_build_query([
            'chat_id' => $chat_id,
            'text'    => "❥ Unknown command. Please use /start or /random for a surprise! 🍃✨"
        ]));

    }
}

if (isset($input['callback_query'])) {
    $callback = $input['callback_query'];
    $chat_id = $callback['message']['chat']['id'];
    $callback_id = $callback['id'];

    if ($callback['data'] == "get_random") {
        forwardRandomMessage($api, $chat_id, $source_channel);
        // Answer callback to show notification
        file_get_contents($api . "answerCallbackQuery?" . http_build_query([
            'callback_query_id' => $callback_id,
            'text' => "✨ Loading your surprise message! 🍃",
            'show_alert' => false,
        ]));

    }
}

/**
 * Send welcome message with sticker
 */
function sendWelcomeMessage($chatId) {
    global $api, $START_EFFECT_ID;

    // First send a sticker or animation related to your START_EFFECT_ID
    // This assumes you have a sticker file_id or animation
    $sticker_file_id = $START_EFFECT_ID;

    // Send sticker first
    file_get_contents($api . "sendSticker?" . http_build_query([
        'chat_id'      => $chatId,
        'sticker'      => $sticker_file_id,
    ]));

    // Then send a welcome message with a button
    file_get_contents($api . "sendMessage?" . http_build_query([
        'chat_id'    => $chatId,
        'text'       => "✨ 𝚆𝚎𝚕𝚌𝚘𝚖𝚎! 🌸\nClick the button below to get a surprise message!",
        'reply_markup' => json_encode([
            'inline_keyboard' => [
                [
                    ['text' => '🎁 Get a Random Message!', 'callback_data' => 'get_random']
                ],
            ],
        ])
    ]));

}

/**
 * Forward a random message from a source chat
 */
function forwardRandomMessage($api, $chat_id, $source_channel) {
    $attempts = 0;
    $maxAttempts = 20;

    $msgId = 0;

    do {
        $msgId = rand(0, 10000);
        $attempts++;

        $copy = file_get_contents($api . "copyMessage?" . http_build_query([
            'chat_id'      => $chat_id,
            'from_chat_id' => $source_channel,
            'message_id'   => $msgId
        ]));

        $copy = json_decode($copy, true);

        if ($copy && $copy['ok'] == true) {
            // Message successfully forwarded
            // Now send a follow-up message with custom text and another button
            file_get_contents($api . "sendMessage?" . http_build_query([
                'chat_id'   => $chat_id,
                'text'      => "✨ 𝑬𝑛𝑦𝑦 𝑡ℎ𝑖𝑠 𝑟𝑎𝑛𝑑𝑜𝑚 𝑚𝑒𝑠𝑠𝑎𝑔𝑒! 🍃✨",
                'reply_markup' => json_encode([
                    'inline_keyboard' => [
                        [
                            ['text' => '🎁 Get Another!', 'callback_data' => 'get_random']
                        ],
                    ],
                ])
            ]));

            break;
        }
    } while ($attempts < $maxAttempts);

    if ($attempts == $maxAttempts) {
        // Failed after many attempts
        file_get_contents($api . "sendMessage?" . http_build_query([
            'chat_id' => $chat_id,
            'text'    => "❌ Failed to forward a message after multiple attempts."
        ]));

    }
}

?>
