Sending images and more with Telegram bot (2024)

Rajitha Gunathilake

Posted on

Sending images and more with Telegram bot (3) Sending images and more with Telegram bot (4) Sending images and more with Telegram bot (5) Sending images and more with Telegram bot (6) Sending images and more with Telegram bot (7)

#bots #telegram #http

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.

Sending messages with Telegram bot Rajitha Gunathilake ・ Nov 10 '19 ・ 3 min read #bots #telegram #http

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

Sending images and more with Telegram bot (9)

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.

Sending images and more with Telegram bot (10)

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.

Sending images and more with Telegram bot (11)

Sending images and more with Telegram bot (12)

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 chatfetch( `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

Sending images and more with Telegram bot (13)

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.

Thanks for reading till the end 🙌

share your thoughts in the comments section.

Top comments (22)

Subscribe

Nilesh Kawar

Nilesh Kawar

  • Joined

May 29 '21

  • Copy link

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

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

May 29 '21

  • Copy link

can you specify the issue ?

Nilesh Kawar

Nilesh Kawar

  • Joined

May 29 '21

  • Copy link

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

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

May 29 '21

  • Copy link

I'll take a look , and let you know .

Nilesh Kawar

Nilesh Kawar

  • Joined

May 29 '21

  • Copy link

Yes please

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

May 30 '21

  • Copy link
const { Telegraf } = require("telegraf");const axios = require("axios");const bot = new Telegraf("XXXXX");bot.start((ctx) => ctx.reply("Welcome"));bot.help((ctx) => ctx.reply("Send me a sticker"));bot.on("sticker", (ctx) => ctx.reply("👍"));bot.hears("hi", (ctx) => ctx.reply("Hey there"));bot.hears("Wow", async (ctx) => { let stateNames = await getStates(); console.log(stateNames); let listt = []; for (let index = 0; index < stateNames.length - 1; index += 2) { listt.push([ { text: stateNames[index], callback_data: String(index) }, { text: stateNames[index + 1], callback_data: String(index + 1) }, ]); } console.log(listt); ctx.telegram.sendMessage(ctx.chat.id, "nani", { reply_markup: { inline_keyboard: listt, }, });});bot.launch();// Enable graceful stopprocess.once("SIGINT", () => bot.stop("SIGINT"));process.once("SIGTERM", () => bot.stop("SIGTERM"));async function getStates() { url = "https://api.covid19india.org/data.json"; res = await axios.get(url); stateArr = res.data.statewise; totalStates = stateArr.length; let stateName = new Array(); for (let i = 0; i < totalStates; i++) { stateName[i] = stateArr[i].state; } // console.log(stateName); return stateName;}

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

May 30 '21

  • Copy link

Sending images and more with Telegram bot (29)

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

May 30 '21

  • Copy link

hope this helps

Nilesh Kawar

Nilesh Kawar

  • Joined

May 30 '21

  • Copy link

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.

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

May 30 '21

  • Copy link

your welcome ✌

Sami Folio

Sami Folio

I'm trying to start a startup right now. I used to be a programmer but because of eye issues, I can't look at a computer screen for too long (photophobia) so I need help when I program.

  • Joined

Jul 1 '21

  • Copy link

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.

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

Jul 1 '21

  • Copy link

hi sami,
thanks for your offer but these days i am a bit busy , and not available for work .

Sami Folio

Sami Folio

I'm trying to start a startup right now. I used to be a programmer but because of eye issues, I can't look at a computer screen for too long (photophobia) so I need help when I program.

  • Joined

Jul 3 '21

  • Copy link

Ok, thank you.

avinash

avinash

  • Joined

Jan 4

  • Copy link

Hi Sami Folio, Is the issue resolved, or can we discuss further on it

Sending images and more with Telegram bot (44)

Comment deleted

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

Oct 23 '22

  • Copy link

can you state the error

Nagy László

Nagy László

  • Email

    taaltos@gmail.com

  • Location

    Budapest - Hungary

  • Education

    Universy of Bonn

  • Joined

Oct 13 '23

  • Copy link

Sending images and more with Telegram bot (49)

ezzel a háttérrel :

Sending images and more with Telegram bot (50)

ezt a férfit árnyékkal

Nagy László

Nagy László

  • Email

    taaltos@gmail.com

  • Location

    Budapest - Hungary

  • Education

    Universy of Bonn

  • Joined

Oct 13 '23

  • Copy link

Sending images and more with Telegram bot (53)

Sending images and more with Telegram bot (54)

Behzad Amirinezhad

Behzad Amirinezhad

  • Location

    Tabriz

  • Work

    Frontend Developer at Tamand

  • Joined

Sep 16 '21

  • Copy link

Thank you for your great post. Is it possible to send image in edit_message_text() or somehow append the image to it?

Rajitha Gunathilake

Rajitha Gunathilake

#FearHeWhoFearsNothing...

  • Location

    sri lanka

  • Education

    Student at University of Moratuwa,Sri Lanka

  • Joined

Sep 16 '21

  • Copy link

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

Bluestone Optional

Bluestone Optional

  • Joined

Oct 7 '23

  • Copy link

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 .

Nagy László

Nagy László

  • Email

    taaltos@gmail.com

  • Location

    Budapest - Hungary

  • Education

    Universy of Bonn

  • Joined

Oct 13 '23

  • Copy link

Hello
Azt szertném tudni, hogy két fotoból tudna-e egy hiteles montázst készíteni ?

View full discussion (22 comments)

For further actions, you may consider blocking this person and/or reporting abuse

Sending images and more with Telegram bot (2024)

References

Top Articles
Aquatics Centre | Paris 2024
Aquatica Pass Member News for January 2024
Restaurer Triple Vitrage
Brendon Tyler Wharton Height
What to Serve with Lasagna (80+ side dishes and wine pairings)
The Potter Enterprise from Coudersport, Pennsylvania
Myhr North Memorial
Wausau Marketplace
Toyota gebraucht kaufen in tacoma_ - AutoScout24
Emmalangevin Fanhouse Leak
Gameday Red Sox
Monticello Culver's Flavor Of The Day
Publix 147 Coral Way
Autozone Locations Near Me
Snowflake Activity Congruent Triangles Answers
Globe Position Fault Litter Robot
Uvalde Topic
Craigslist Pets Longview Tx
Walthampatch
What is Cyber Big Game Hunting? - CrowdStrike
Suffix With Pent Crossword Clue
Sussur Bloom locations and uses in Baldur's Gate 3
Brazos Valley Busted Newspaper
Teekay Vop
Bellin Patient Portal
How Taraswrld Leaks Exposed the Dark Side of TikTok Fame
Timeline of the September 11 Attacks
Cognitive Science Cornell
Craigslist Rome Ny
Gillette Craigslist
Gma' Deals & Steals Today
Truck from Finland, used truck for sale from Finland
Lininii
Mrstryst
Craigslist Central Il
Composite Function Calculator + Online Solver With Free Steps
2012 Street Glide Blue Book Value
Avance Primary Care Morrisville
Mohave County Jobs Craigslist
Verizon Outage Cuyahoga Falls Ohio
2023 Fantasy Football Draft Guide: Rankings, cheat sheets and analysis
فیلم گارد ساحلی زیرنویس فارسی بدون سانسور تاینی موویز
Mother Cabrini, the First American Saint of the Catholic Church
La Qua Brothers Funeral Home
Gonzalo Lira Net Worth
Dlnet Deltanet
Enter The Gungeon Gunther
Egg Inc Wiki
Bismarck Mandan Mugshots
Craigslist Pet Phoenix
Concentrix + Webhelp devient Concentrix
Secondary Math 2 Module 3 Answers
Latest Posts
Article information

Author: Edmund Hettinger DC

Last Updated:

Views: 5341

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edmund Hettinger DC

Birthday: 1994-08-17

Address: 2033 Gerhold Pine, Port Jocelyn, VA 12101-5654

Phone: +8524399971620

Job: Central Manufacturing Supervisor

Hobby: Jogging, Metalworking, Tai chi, Shopping, Puzzles, Rock climbing, Crocheting

Introduction: My name is Edmund Hettinger DC, I am a adventurous, colorful, gifted, determined, precious, open, colorful person who loves writing and wants to share my knowledge and understanding with you.