How to send image or documents using Telegram API

Spread the love

Hello friends in previous blog we learned how we can send message on telegram using official api. by using simple method of cURL, PHP and Python.

Now we are going to learn how we can send documents or image using telgram api. So lets start our learning. First of all we have to create our telegram bot using bot father.

How to get Bot Token

After that you have to take your bot token of your bot, because bot token is key of your bot. you require everytime when you create any type of bot. Your bot token look like this:

123456789:AAE_xxxxxxxxxxxxxxxxxxxxxxxxxxxx

Now we are ready to use our bot for sending image or documents. We use this api for sending documents or images on our bot.

https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendDocument

Which type of documents we can send through this api?

You can send documents whose extension contains .PDF, .DOCX, .TXT, .JPEG, .PNG etc. You can send Upto 50 MB using this sendDocument API.

Send A Document (PDF, DOCX, TXT, etc)

cURL example :

curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendDocument" \
  -F chat_id="984654311" \
  -F document=@"vidyarays.txt"

PHP example

$token = "YOUR_BOT_TOKEN";
$chat_id = "984654311";
$file = "/path/to/vidyarays.txt";

$url = "https://api.telegram.org/bot$token/sendDocument";

$post_fields = [
    'chat_id' => $chat_id,
    'document' => new CURLFile(realpath($file))
];

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

Send an Image (JPG, PNG)

We use sendPhoto method to send image files.

cURL example :

curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendPhoto" \
  -F chat_id="984654311" \
  -F photo=@"image.jpg"

PHP example :

$token = "YOUR_BOT_TOKEN";
$chat_id = "984654311";
$image = "/path/to/image.jpg";

$url = "https://api.telegram.org/bot$token/sendPhoto";

$post_fields = [
    'chat_id' => $chat_id,
    'photo' => new CURLFile(realpath($image))
];

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$response = curl_exec($ch);
curl_close($ch);

echo $response;

In these api we can use Optional Parameters:

  1. caption : short message below the photo
  2. parse_mode – supports Markdown or HTML

Send Files or Images from a URL

Instead of uploading local files, you can directly pass URL as a document of photo value.

Example (send photo from URL):

curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendPhoto" \
  -d chat_id="984654311" \
  -d photo="https://example.com/image.jpg"

Note : The URL is must publically avilable no require any login.

Handling Large Files

If you want to share large files that exceed the limit.

  1. Use a telegram upload endpoint or split the file in smaller chunks.
  2. Or upload manually and reuse the file_id from previous upload

Example of a message containing a file:

"document": {
  "file_id": "BQACAkQAAxkBA...",
  "file_name": "file.txt",
  "mime_type": "application/pdf",
  "file_size": 10560
}

Now You can reuse file_id like this:

curl -X POST "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendDocument" \
  -d chat_id="984654311" \
  -d document="BQACAkQAAxkBA..."

Some Security Tips :

  1. Keep you bot token secret. Else anyone can control your bot using you bot token, Must secure it like your favorite password.
  2. Dont share your API url with your token in pulic groups/ channels or any third party person.
  3. Rotate your token from BotFather if leaked.

I hope you like this content, We meet again in next beautful article till than keep learning keep smiling.