skip to Main Content

I used python-telegram-bot to get download url from the posts sent to my implemented bot. The problem was the file size limitation (max upload size: 50 MB, max download size: 20 MB).

So I changed my python library to telethon to use mtproto. It doesn’t have the file size limitation, but, I can’t find the method to get the download URL! It has a method called ‘download_media’ and it downloads the media!

@client.on(events.NewMessage)
async def download_media(event, **kwargs):
    message = event.message
    file = await message.download_media()

Is there any way to get just the download URL without downloading the file?

2

Answers


  1. Telegram’s MTProto API, which Telethon connects with, doesn’t offer the ability to download files from a URL, that functionality is provided by the HTTP bot API for convenience.

    Login or Signup to reply.
  2. 1- Using message.document.file_id you can access the file_id of the video.

    2- Use getFile(<file_id>) method to get the file_path of the file (which is the equivalent of curling https://api.telegram.org/$bot_token/getFile, read more on Telegram Bot getFile API and python-telegram-bot getFile documentation)

    3- The final URL of the video will be the following:

    https://api.telegram.org/file/<bot_token>/<file_path>
    

    P.S: USE THE URL ONLY ON YOUR OWN CODE-BASE AS IT’LL GIVEAWAY YOUR BOT_TOKEN.

    P.S.2: The links are valid for 1 hour, after that another getFile request must be made.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search