Trouble with Telegram bot's media group sending feature

Hey everyone, I’m having a problem with my Telegram bot. I’m trying to send a video with a thumbnail using the send_media_group method, but I’m getting an error. Here’s what’s happening:

The error message says: Can't parse inputmedia: media not found

I’m using Python 3.11.6 and the python-telegram-bot library version 20.7. Here’s a simplified version of my code:

video = InputFile('video.mp4')
thumb = InputFile('thumb.jpg')
media = InputMediaVideo(media=video, thumbnail=thumb)
await bot.send_media_group(chat_id=123456, media=[media])

The weird thing is, I can confirm that the video and thumbnail paths are correct. The code runs up to the point where it tries to send the media group, but then it fails.

Has anyone run into this issue before? Any ideas on what might be causing it or how to fix it? Thanks in advance for any help!

I’ve encountered this issue before when working with Telegram bots. The problem might be related to how you’re handling the file paths. Instead of using InputFile directly, try opening the files and passing the file objects to InputMediaVideo. Here’s an example:

with open('video.mp4', 'rb') as video_file, open('thumb.jpg', 'rb') as thumb_file:
    media = InputMediaVideo(media=video_file, thumbnail=thumb_file)
    await bot.send_media_group(chat_id=123456, media=[media])

This approach ensures that the files are properly opened and read before being sent. Also, double-check that your bot has the necessary permissions to access and send files in the chat. If the issue persists, you might want to verify the file formats and sizes, as Telegram has certain limitations on media files.

I’ve been working with Telegram bots for a while now, and I’ve seen this error pop up before. One thing that often gets overlooked is file size limitations. Telegram has strict limits on media file sizes, especially for bots. For videos, the limit is 50MB, and for thumbnails, it’s 200KB. Make sure your files are within these limits.

Another potential issue could be file format compatibility. Telegram prefers MP4 for videos and JPEG for thumbnails. If you’re using different formats, try converting them first.

Also, have you checked your bot’s token? Sometimes, if the token is invalid or has been revoked, you’ll get cryptic errors like this. It might be worth regenerating your bot token through BotFather and updating it in your code.

Lastly, if none of these solve the problem, try using a URL instead of a local file. Sometimes, Telegram’s API handles URLs more reliably than local files. You can upload your video and thumbnail to a file hosting service and use the direct URLs in your code.

hey mate, i had this problem too. try using the file’s actual path instead of just the filename. like this:

media = InputMediaVideo(media=open(‘/full/path/to/video.mp4’, ‘rb’), thumbnail=open(‘/full/path/to/thumb.jpg’, ‘rb’))

this worked for me. good luck!