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.
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
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.
This can also be done programmatically, I will use nodejs to demonstrate in this example.
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.
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
we can use sendAudio
just like the last example to send audio files.
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
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.
Top comments (22)
Hey buddy I need help regarding telegram bot development
I'm new to telegram bot development i have been stuck in one issue for 5 days and I can't figure it out It'd be great if you could help me pls
can you specify the issue ?
So basically I have an array i want to show elements from an array in inlineKeyboard buttons
I have explained it on stackoverflow:
stackoverflow.com/questions/676461...
Pls help
I'll take a look , and let you know .
Yes please
hope this helps
Thank you so much, sir. I have been stuck for one week on this issue. Thank you so much for resolving and providing a solution.
I tried myself but couldn't solve it. I googled it, asked so many peoples but no one replied, and finally, you solved it.
Again Thank you so much.
your welcome ✌
Hello Rajitha, I need to receive specific photos from Telegram to a Telegram widget on my site. I can pay $100 if you can help. I will have lots of other things to do after that. The chat ID i want photos from has lots of images, and i only want those with a word before them. Suppose there are photos of animals and i only want the ones with cats, so the sentence before has to start with "cat" . And it can only be from the Channel owner, not people who comment.
hi sami,
thanks for your offer but these days i am a bit busy , and not available for work .
Ok, thank you.
Hi Sami Folio, Is the issue resolved, or can we discuss further on it
can you state the error
ezzel a háttérrel :
ezt a férfit árnyékkal
Thank you for your great post. Is it possible to send image in edit_message_text() or somehow append the image to it?
if you want to send image with a caption , you can use caption in parameter in
formdata
refer this documentation - core.telegram.org/bots/api#sendphoto
Can telegram bot store data? Of yes what about if I send a photo to telegram bot will it save it forever???? Or do they delete it .
Hello
Azt szertném tudni, hogy két fotoból tudna-e egy hiteles montázst készíteni ?