Upload file on Telegram with bot

19,828

Sending a file by URL only works for certaining file types. If you want to upload other types of files you will have to post the file, after saving it on your own server, using multipart/form-data.

Sending by URL
In sendDocument, sending by URL will currently only work for gif, pdf and zip files. [doc]


Sending file in PHP

$filepath = realpath('folder/.attheme');
$post = array('chat_id' => $GLOBALS["chat_id"],'document'=>new CurlFile($filepath));    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.telegram.org/bot" . $GLOBALS["token"] . "/sendDocument");
curl_setopt($ch, CURLOPT_POST, 1);   
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec ($ch);
curl_close ($ch); 
Share:
19,828

Related videos on Youtube

Mahdi Asiyabi
Author by

Mahdi Asiyabi

Updated on June 04, 2022

Comments

  • Mahdi Asiyabi
    Mahdi Asiyabi almost 2 years

    I want to send file from URL to user with Telegram Bots, My files extension in .attheme but I can't upload this files from Url.

    Currently I can upload .zip , .pdf, but i want upload a .attheme file from PHP code.

    This bot can upload any type of files into Telegram: @uploadbot

    How can I do this ?

    • Ivan Vinogradov
      Ivan Vinogradov about 6 years
      what kind of error do you receive?
  • parsecer
    parsecer about 5 years
    Hello! I'm trying to do this with Java. I have a .gif file on my computer. To pass it in sendAnimation method I need to provide a type InputFile. I'm not sure how do I create that type? Telegram API says: Post the file using multipart/form-data in the usual way that files are uploaded via the browser. 10 MB max size for photos, 50 MB for other files. Bu then, what is meant by multipart/form-data?
  • parsecer
    parsecer about 5 years
    But sendDocument requires a file URL or an existing file ID or some form-data: File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data. . How to upload a local file using that multipart/form-data thing?