<?php  
$url = $_SERVER['REQUEST_URI'];  
$parts = explode('/', trim($url, '/'));  

if (isset($parts[1]) && !empty($parts[1])) {  
    $token = $parts[1];  

    if (isset($parts[2]) && !empty($parts[2])) {  
        $command = $parts[2];  
        $command_parts = explode('?', $command);  
        $command_name = $command_parts[0];   
        
        //echo "Token: " . htmlspecialchars($token) . "<br>";  
        //echo "Command: " . htmlspecialchars($command_name) . "<br>";  

        // آماده سازی URL  
        $getdata = http_build_query($_GET);  
        $requrl = "https://api.telegram.org/" . $token . "/" . $command_name . "?" . $getdata;  
        
        $data = json_decode(file_get_contents('php://input'), true);  
        $data_string = json_encode($data);  

        //Setup cURL  
        $ch = curl_init();  
        curl_setopt($ch, CURLOPT_URL, $requrl);  
        curl_setopt($ch, CURLOPT_POST, 1);  
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
            'Content-Type: application/json',                                                                                
            'Content-Length: ' . strlen($data_string))                                                                       
        );      

        // ارسال درخواست  
        $response = curl_exec($ch);  
        header('Content-Type: application/json');

        // بررسی وجود خطا در cURL  
        if ($response === false) {  
            echo 'Curl error: ' . curl_error($ch);  
        } else {  
            echo $response;  
        }  

        // بستن جلسه cURL  
        curl_close($ch);  
    } else {  
        echo "کامند وجود ندارد.";  
    }  
} else {  
    echo "توکن وجود ندارد.";  
}  
?>