How to send big files from URL to Telegram bot?

15,661

Solution 1

Telegram bot API can only send files less than 20 MB by url param, you should lookup Sending Files section.

If you want to send 20-50 MB files, you should download and re-upload to Telegram bot API server.
You can refer this simple code

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => 'https://api.telegram.org/bot131210513:AXXXXXX/sendDocument?caption=Hello+World&chat_id=24523586',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Content-Type: multipart/form-data'
    ],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'document' => curl_file_create('/etc/hosts', 'plain/text', 'Hosts-file.txt')
    ]
]);
$data = curl_exec($curl);
curl_close($curl);

Solution 2

2021 Update: Telegram now provides a self hostable Telegram Bot API which can upload 1.5GB.

You can install pwrtelegram on your server. Then simply switch the API URL and you will be able to upload up to 1.5 GB of files with your bot. That is the only possible way. Check out the link for more information.

Also, you cannot pass in any random file_id, as Telegram will not send it. You can only pass in a file_id which has been uploaded by your bot previously. To bypass the limit, use the method, above. It works very well.

Share:
15,661
Admin
Author by

Admin

Updated on June 24, 2022

Comments

  • Admin
    Admin almost 2 years

    I have some big size files (in MP4 and Zip formats) and I want to send them to my chat by Telegram bot, I used the code below:

    file_get_contents('https://api.telegram.org/bot[[app:token]]/sendDocument?chat_id=24523586&document='.$fileAddress);
    

    But it just can send files with small sizes, less than 50MB! But I know there is no file size limitation for documents which are sending by file_id. You can see this page
    Now how can I make file_id for my files? My files are uploaded on my server and I am using PHP.