<?php









$botToken = '8291470736:AAHMwfbO6ABPoe3OtzRujXmaLzXJaMPphIQ';


// hook.php

// Capture the raw POST data from the webhook
// $update = json_decode(file_get_contents('php://input'), TRUE);

$filePath = './download/'; // Directory to save files (ensure it has write permissions)

// Receive the webhook update data
$update = json_decode(file_get_contents("php://input"), TRUE);
$botToken = "8291470736:AAHMwfbO6ABPoe3OtzRujXmaLzXJaMPphIQ";
$chat_id = $update['message']['chat']['id'];
$message1 = json_encode($update["message"]);

// file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id={$chat_id}&text=".json_encode($update["message"])."");

// file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id=".$update['message']['chat']['id']."&text=".json_encode($update["message"])."");


if (isset($update["message"])) {
    $message = $update["message"];
    $file_id = null;
    $file_name = "downloaded_file"; // Default filename

    // Check for different file types
    if (isset($message["document"])) {
        $file_id = $message["document"]["file_id"];
        $file_name = $message["document"]["file_name"];
    } elseif (isset($message["photo"])) {
        // Photos come in different sizes; get the largest one (last element in array)
        $photo_sizes = $message["photo"];
        $largest_photo = end($photo_sizes);
        $file_id = $largest_photo["file_id"];
        // $file_name = $file_id . ".jpg"; // Telegram doesn't provide a filename for photos
        $file_name = uniqid() . ".jpg";
        // file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id=".$update['message']['chat']['id']."&text=".json_encode($file_id)."");
    } elseif (isset($message["audio"])) {
        $file_id = $message["audio"]["file_id"];
        $file_name = $message["audio"]["file_name"] ?? $file_id . ".mp3";
    } elseif (isset($message["video"])) {
        $file_id = $message["video"]["file_id"];
        $file_name = $message["video"]["file_name"] ?? $file_id . ".mp4";
    }
    // Add other file types (voice, sticker, etc.) as needed

// $message1 = urlencode("mulai download the file.");
// file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id={$chat_id}&text={$message1}");

// $file_id = $update['message']['photo'][0]['file_id'];

    if ($file_id) {
        // Step 3: Get the file path from Telegram API
        // $api_url = "api.telegram.org";
        https://api.telegram.org/bot<YourBotToken>/getFile?file_id=<file_id> 
         $api_url = "https://api.telegram.org/bot{$botToken}/getFile?file_id={$file_id}";
        $file_info_json = file_get_contents($api_url);
        $file_info = json_decode($file_info_json, TRUE);





// file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id={$chat_id}&text=ok");

// file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id=".$update['message']['chat']['id']."&text=".$file_info["result"]["file_path"]."");


        if (isset($file_info["result"]["file_path"])) {


            // file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id={$chat_id}&text=ok1");
            $telegram_file_path = $file_info["result"]["file_path"];
            
            // Step 4: Download the file from Telegram file server
            // https://api.telegram.org/file/bot1234:abcd/photos/file_name.jpg
            $download_url = "https://api.telegram.org/file/bot{$botToken}/{$telegram_file_path}";
            $local_save_path = $filePath . $file_name;

            // Ensure the download directory exists
            if (!is_dir($filePath)) {
                mkdir($filePath, 0755, true);
            }

            // Use cURL or file_get_contents to download and save the file
            file_put_contents($local_save_path, file_get_contents($download_url));
            
            // Optional: Send a confirmation message back to the user
            $chat_id = $message["chat"]["id"];
            $confirmation_text = "File '$file_name' saved successfully.";
            $send_message_url = "api.telegram.org" . urlencode($confirmation_text);
            file_get_contents($send_message_url);
            file_get_contents("https://api.telegram.org/bot{$botToken}/sendMessage?chat_id={$chat_id}&text={$confirmation_text }");

        }
    }
}
?>