3

I send to my telegram bot a few photos as group. How I can get the file_id all of my uploaded photos?

I use webhooks, but response is not contain any data about all photos, just some data about last uploaded photo and media_group_id. How I can get all file_id's of my uploaded group photos

response: enter image description here

Milo
  • 3,365
  • 9
  • 30
  • 44
  • You have to receive several updates, and them have the same `media_group_id`. – Sean Wei May 04 '18 at 18:46
  • Yes, this is a good way to get photos of the one group, but in my logic, after upload the last photo, webhook must call function for send message to user, for this I need to know when the last photo was uploaded – Alexey Shablowski May 04 '18 at 20:35
  • interesting fact - the "date" element in response is the same in all of webhooks. I upload 5 photos and receive 5 response with the same date element. This means that you can detect group photos by media_group_id or time element – Alexey Shablowski May 04 '18 at 20:53
  • @AlexeyShablowski did you find a solution? I have the same problem and I get only last photo on publishing multiple photos message. After that on the next published message (if any) I receive all the rest from that media group. Not consistent at all since I can't get all the images until any other random message is published. – Max Jan 19 '21 at 23:07
  • When using `sendMediaGroup` it returns array of `Message`. – Muaath Apr 10 '21 at 15:10

3 Answers3

2

The Telegram Bot API does not give to your web-hook any reliable information about the order of each item in a media group. https://core.telegram.org/bots/api#message

Suggestions:

  1. If bot is in a private chat, save the incoming file_id against their media_group_id. Whenever media_group_id changes you would have all you need to use. Engage the user in some other way so that you can quickly determine the media_group_id change and respond quickly from that processing.

  2. If bot is in a group chat, save incoming file_id against the users id as well as media_group_id and similarly monitor changes to media_group_id and user id to take action.

When a solution starts getting too complex for my liking, I prefer to go back to the basic reason for my need and perhaps find out that I do not need to do something an API doesn't afford like "Get all uploaded photos by media_group_id". Maybe I can just process them individually as the updates stream in.

Tip: If the media group has a caption and you only care about the first media item in the group, then monitoring the media_group_id and caption of an incoming message should be sufficient.

if(message.caption != null && message.media_group_id != null){ // first item in new group received }

Jomeno Vona
  • 71
  • 1
  • 6
0

Okay, this way is not simple and not elegant but it's working.

  1. We need to see in webhook response media_group_id element - this means that this is a group of media.
  2. If the first point is true - save the data about this media on own server (data must be contain media_group_id)

example: ["media_group_id" => [[some data about photo]]]

  1. When next webhook is come, we need to see his media_group_id, if it's the same - ok, add him to array...

example: ["media_group_id" => [[some data about photo],[some data about photo]]]

if not - ok, make a new element of array:

example: ["media_group_id" => [[some data about photo]], "media_group_id2" => [[some data about photo]]]

  1. So we got an array that contains all data about the photos of the one(or more) group.

hmmm... I don't like this way, but how make better?

  • I'm interested in a better way of doing this if possible. I want to achieve some processing on a bulk of images (i.e an album) – Ahmed Besbes Oct 03 '19 at 15:16
0

In Pyrogram, we can use the get_media_group function, https://docs.pyrogram.org/api/methods/get_media_group.html#pyrogram.Client.get_media_group. in my opinion, it is much better than using BOT API.

SpEcHiDe
  • 116
  • 1
  • 17
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 26 '21 at 03:02
  • This is not BotApi though, which the user seemingly prefers to use for the given use case. – nyx69 Mar 02 '22 at 07:33
  • Pyrogram can be used with bot_logins. In my opinion, it is much better than using the BOT API. – SpEcHiDe Mar 03 '22 at 06:05