Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram bot - receive photo URL

When a user send an image via Telegram bot it there any way to get the image URL? or I just need to save the image somewhere?

like image 513
Dan Naim Avatar asked Mar 14 '16 15:03

Dan Naim


People also ask

How can I get image from Telegram bot?

From telegram's docs: On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot/, where is taken from the response. It is guaranteed that the link will be valid for at least 1 hour.

What is the URL for Telegram API?

Introduction to the Telegram Bot API The first part of the URL indicates that you want to communicate with the Telegram API ( api.telegram.org ).


1 Answers

In the message array you receive you can find the key photo. There you will find multiple arrays with the following format

"file_id" : "XXXX",
"file_size" : 1107,
"width" : 90,
"height" : 51

From one of those array you need to take the file_id. You can then request the file_path with a simple get get on the url https://api.telegram.org/bot<token>/getFile?file_id=<file_id>

You will receive an array that looks as following

"ok" : true,
"result" : {
    "file_id" : "XXXX",
    "file_size" : 27935,
    "file_path" : "photo\/file_1.jpg"
}

From the result you need the file_path and you then got the image location https://api.telegram.org/file/bot<token>/<file_path>

like image 95
Maak Avatar answered Oct 04 '22 16:10

Maak