In telegram API documentation I see: “You can either pass a file_id as String to resend a photo that is already on the Telegram servers”, but I can’t find ways to get file_id of uploaded file. How can I get it?
Question posted in Telegram API
A comprehensive official documentation can be found here.
A comprehensive official documentation can be found here.
7
Answers
Depending on the method (File type) which you chose to send a file, after sending a file to Telegram a response is returned. For example if you send a MP3 file to Telegram using
sendAudio
method, Telegram returns anAudio
object which contains the file ID.Source: https://core.telegram.org/bots/api#audio
Say you receive a
Message
with an array ofPhotoSize
https://core.telegram.org/bots/api#photosize
As you can see, there’s a
file_id
, you can use this to send a photo throughsendPhoto
.If we assume
Update
is an object, with in it aMessage
object, which in turn provides aChat
object with in it aid
of the chat where the initial message came from and an array ofPhotoSize
(excuse me for using PHP here, but that’s my main language…)$update->message->photo
is how you can access the array.Use some kind of For loop to iterate over the items, or just access the first one if the array isn’t bigger than 1.
After that, you can use the result(s) to extract the
file_id
and send it as astring
viasendPhoto
‘sphoto
parameter and the Chat ID via thechat_id
parameter.I hope this helped!
P.S. Here is a diagram of my current implementation of the API, i hope it brings some clarity to you!
Its depended to your content_types ,for example:
Video:
Audio:
Photo:
For more see this link.
In addition to the answers above, you can log Updates that comes to your bot, Either from
https://api.telegram.org/bot'.BOT_TOKEN.'/getUpdates
or throw updates that come in your application. there you will find aJson
property like below:This is the easiest way I’ve found to do it.
Upload your file to any chat and forward the message to @RawDataBot. It will return something like this:
What you need is the string under
file_id
. Once you have copied that, you can simply the following code to send the message.if you use PHP:
you can write this line for full size:
$file_id = $updates['message']['photo'][1]['file_id'];
and this line for thumb:
$file_id = $updates['message']['photo'][0]['file_id'];
According to the latest docs (v20.0a6) plenty of classes have been changed. I have found that the easiest way to get started with files is using the effective_attachment property.
For declaring the handler there have also been changes to filters, here is a simple way to declare it: