message_handler: Processes the first two media together when sent simultaneously (type: media_group_id)
Please answer these questions before submitting your issue. Thanks!
- What version of pyTelegramBotAPI are you using? 4.22.1
- What OS are you using? windows
- What version of python are you using? 3.11.9
The first two media files are processed together, leading to the same output or action being applied to both. Code:
i = 1
@bot.message_handler(func=lambda message: True)
def handle_message(message):
global i
print(i)
print(message)
i = i+1
Result:
1
1
message
message
2
message
3
message
...
Hi. This behaviour is not due to library but API.
The only solution I can offer is implementing a logic yourself. Examples:
- timeframe in which all photos are gathered
- "done" callback button
- ...etc
@coder2020official Frankly speaking, receiving gallery is a real pain. I suppose when (if) we have time it may be useful to make some joint handler for media gallery, that will allow to receive gallery itself. I did not make long thinking on it, but it may be useful.
Yes, I will take a look at this at some point.
A group can be any file, not just photos. I have media group handling built into my bots and I have never experienced this issue. You cannot just 'catch' a media group at the handler level. Uploads take time and the user might have a slow connection or the files could be big. So if you try and handle them immediately you will split the media group 100% of the time as all uploads are not sent together but instead are sent one at a time as they finish.
Even if you did catch the pieces, they then have to be turned into InputMedia objects in order to be sent as a group of something, and keyword arguments like captions, protections, and effects need to be built for the objects as well as keyword arguments for the group itself. And then it needs to be sent as a group. This cannot be done with a handler. It is up to the user to implement this.
In addition, there is no way to know if a media group contains 2 things, 6 things, or 10 things. You can't check that. You have to make an assumption. Then there's silly things like certain InputMedia types cannot be grouped together and certain types cannot accept a caption.