Sending images and more with Telegram bot
Tags: bots, telegram, http
Published: 2021-05-24 17:05:45

Hi everyone,

This is a follow-up tutorial on my previous tutorial about Sending messages with Telegram bot. make sure you check that before following this tutorial. 

{% post https://dev.to/rizkyrajitha/get-notifications-with-telegram-bot-537l %}

in the last tutorial, we talked about sending messages, but telegram has a powerful API that allows us to send more than just text messages.
so in this tutorial, we will get to know how to send images with a telegram bot.This procedure is really simple. we need to send a post request to telegram API with our photo as `multipart/form-data`.

This is similar to what we used previously, the change is now we are using a different route, and sending form-data with our image attached

Using the follwoing API endpoint `https://api.telegram.org/bot<token>/sendPhoto?chat_id=<group chat id >`

in `multipart form data` attach an image file with the name `photo`


![insomnia send request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtw5sy35i7i98lhw1dxt.png)

and after sending this we can get a response with `"ok": true,` and if we look at the chat, we can see that the photo is received. 

![chat image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thn7keewwmeigwowwp77.jpeg)

This can also be done programmatically, I will use nodejs to demonstrate in this example. 

```js
const fetch = require("node-fetch");
const fs = require("fs");
const FormData = require("form-data");

let readStream = fs.createReadStream("./image.png");

let form = new FormData();
form.append("photo", readStream);

fetch(
  `https://api.telegram.org/bot<token>/sendPhoto?chat_id=-<chat id>`,

  {
    method: "POST",
    body: form,
  }
)
  .then((res) => res.json())
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });

```
we have used `node-fetch` package to send the HTTP request from nodejs and `form-data` package to append formdata to the request. 

and after running the script we can see that we get `"ok": true,` just like before. 

![bash output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dwy5si27likv8ngpi4ut.png)

![chat image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gn77i05fpfti78fk6ds8.jpeg)

there are some limits provided by the telegram when using sendPhoto API 
"The photo must be at most **10 MB** in size. The photo's width and height must not exceed **10000** in total. Width and height ratio must be at most 20".

[Telegram API reference sendphoto method](https://core.telegram.org/bots/api#sendphoto) 

we can use `sendAudio` just like the last example to send audio files. 

```js
const fetch = require("node-fetch");
const fs = require("fs");
const FormData = require("form-data");

let readStream = fs.createReadStream("./audio.mp3");

let form = new FormData();

form.append("audio", readStream);
form.append("title", "audio dev test");// to show as the title in chat

fetch(
  `https://api.telegram.org/bot<token>/sendAudio?chat_id=-<chat id>`,

  {
    method: "POST",
    body: form,
  }
)
  .then((res) => res.json())
  .then((response) => {
    console.log(response);
  })
  .catch((error) => {
    console.log(error);
  });
```
limits provided by the telegram when using `sendAudio` API 

"Your audio must be in the **.MP3** or **.M4A** format. On success, the sent Message is returned. Bots can currently send audio files of up to **50 MB** in size, this limit may be changed in the future."

[Telegram API reference sendaudio method]
(https://core.telegram.org/bots/api#sendaudio)

![audio chat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7hwg4fanslwnmw2j7yg.jpeg)

And the list goes on and on for documents, voice messages,animations , videos  etc. you can find all the provided methods in the [Telegram reference](https://core.telegram.org/bots/api).

## Thanks for reading till the end 🙌
## share your thoughts in the comments section.