Creating a Telegram bot to automatically forward videos using project identifiers

Hi folks! I need help with my Telegram bot project. I want to create a bot that sends specific videos to users when they enter a project ID like #ABC123.

I have all my finished videos stored in a Telegram group. Each video has a project code in its filename (example: ABC123V2.mp4). My plan is to use a JSON file to store the connection between project codes and Telegram message IDs.

My approach so far:

  1. Made a Pyrogram script to scan through group messages and match filenames to message IDs.
  2. Store these connections in project_map.json.
  3. Bot reads the JSON and sends the right video when someone types the code.

The issue is that my scanning script finds no filenames. The JSON file remains empty even though videos exist in the group.

Here’s my scanning code:

for message in client.get_chat_history(group_id, limit=100):
    if (message.video or message.document) and message.media:
        filename = ""
        if message.video:
            filename = message.video.file_name
        elif message.document:
            filename = message.document.file_name

        if filename:
            code = os.path.splitext(filename)[0].upper()
            project_map[code] = message.id

The result is always an empty dictionary. Could videos uploaded from mobile devices be missing filename data? Should I try using captions instead of filenames to identify videos?

Any suggestions would be helpful!

Had this exact problem last year building a similar bot. Telegram doesn’t preserve original filenames, especially for videos from mobile apps. Mobile uploads get their filenames stripped or replaced with generic names - you’re right about that. I switched to using captions as the main identifier and it worked way better. Try checking the file_name attribute of message.document first, then fall back to parsing captions if the filename’s None or empty. Also check if your videos are coming through as video messages or document messages. Documents keep filenames better than video messages. Add some debug prints to see what media type you’re actually getting and if the filename field even exists. Consider adding a manual mapping option too - let users associate message IDs with project codes through bot commands as backup when auto-detection fails.

Hit the same issue building a media bot for work. Telegram definitely strips metadata from mobile uploads - it’s annoying. Here’s what worked for me: check file_unique_id first to confirm you’ve got actual media objects. Then look at both file_name and file_id. Watch out - sometimes filename shows up as empty string instead of None, so use if filename and filename.strip() rather than just if filename. Large videos get compressed and lose their original names completely. I ended up going hybrid - parse captions first, then fall back to document filenames. Caption parsing was way more reliable since users could manually add project codes. Also try iterating through message history in reverse order. Newer messages usually have better metadata.

Yeah, mobile uploads def mess with filenames. Add print(filename) after each check to see what’s there. Also, Telegram sometimes saves vids as docs instead of video msgs. Your limit=100 might miss stuff if you’ve got tons of messages - try bumping it up or dropping the limit.