Py + Telegram API => Download Files

Devil’s Advocate
3 min readApr 18, 2023


I learned how to program a simple Telegram bot today and I took the help of ChatGPT for that.

Disclaimer — This post is strictly only for educational purposes. Stealing the intellectual property of anyone can be considered a crime and be subject to the laws of the land, wherever you are.

Me: Hi, is there a Python script to download all the files from a telegram channel?

ChatGPT: Yes, you can use the Telethon library in Python to download all the files from a Telegram channel. Telethon is an asynchronous Python API for the Telegram API that makes it easy to work with the Telegram platform.

Here’s a sample script to download all the files from a Telegram channel:

import os
import asyncio
from telethon import TelegramClient
from import InputMessagesFilterDocument

# Replace 'YOUR_API_ID' and 'YOUR_API_HASH' with your API credentials from
api_id = 'YOUR_API_ID'
api_hash = 'YOUR_API_HASH'
channel_name = 'CHANNEL_NAME' # Replace with the desired channel name or link

async def main():
client = TelegramClient('anon', api_id, api_hash)
await client.start()

# Make sure the output directory exists
output_dir = "telegram_channel_files"
if not os.path.exists(output_dir):

# Get the channel entity
channel = await client.get_entity(channel_name)

# Iterate through all messages with documents and download the files
async for message in client.iter_messages(channel, filter=InputMessagesFilterDocument):
file_name = message.document.attributes[0].file_name
print(f'Downloading {file_name}')
await client.download_media(message, os.path.join(output_dir, file_name))
except Exception as e:
print(f'Error downloading {file_name}: {e}')

print("Finished downloading files.")
await client.disconnect()

Before running the script, make sure you have Telethon installed. You can install it using pip:

pip install telethon

Also, don’t forget to replace ‘YOUR_API_ID’, ‘YOUR_API_HASH’, and ‘CHANNEL_NAME’ with your own credentials and desired channel name. The script will download all files and save them in a folder named “telegram_channel_files” in the current directory.

Keep in mind that this script may take some time to download all the files, depending on the number of files and their sizes.

I have not programmed a bot with Telegram before today although I have used Python extensively for other purposes. So, I did not know how to get the API ID, API Hash, etc. ChatGPT was helpful with instructions for that as well.

Me: I am using a Mac. How to get the API ID and API hash from telegram for this purpose?

ChatGPT: To get the API ID and API hash for your Telegram account, follow these steps:

Open a web browser and visit the Telegram Core website.

Log in with your phone number (associated with your Telegram account) in the international format, e.g., +1234567890.

Telegram will send a confirmation code to your Telegram account. Check your account and enter the code in the browser.

Once you are logged in, click on “API development tools” or go to

If you have already created an application, you can find the API ID and API hash under the “App configuration” section. If not, click on “Create a New Application” to create one.

Fill out the required fields in the “Create new application” form. The required fields are the app’s title and short name. You can also provide an optional description and image, but these are not necessary for the Python script.

After you submit the form, you will see your API ID and API hash. Note these down, as you’ll need them for the Python script.

Now that you have your API ID and API hash, replace ‘YOUR_API_ID’ and ‘YOUR_API_HASH’ in the Python script with your own credentials. The script should now work for your Telegram account on your Mac.

Remember that your API ID and API hash should be kept private and not shared with others, as they provide access to your Telegram account’s API.

I repeat — this post is strictly for educational purposes only. Please do not misuse such scripts for any nefarious purposes.

Note: Though this script is good,

  • It can choke your network by trying to download all the files on the channel.
  • A channel on Telegram can have files with different extensions (like .pdf, .epub, .djvu, etc.) and you may want to download only certain types of files.
  • One may want to add a randomized sleep between each file download

So, it is best to modify such a script before even attempting to download anything from a Telegram channel.



Devil’s Advocate

Seeker for life. Looking to make technology simpler for everyone.