A Python script to resolve Telegram invite links (t.me URLs) to their corresponding channel IDs. This is useful for collecting channel IDs for further analysis or monitoring.
- Python 3.7+
- A Telegram account
- Telegram API credentials (instructions below)
-
Install required packages:
pip install telethon python-dotenv
-
Get Telegram API Credentials:
- Visit https://my.telegram.org/apps and log in with your Telegram account
- Create a new application if you don't have one already
- You'll receive an
API_ID
(numbers) andAPI_HASH
(alphanumeric string) - Keep these credentials secure and never share them publicly!
-
Create a .env file:
Create a file named
.env
in the same directory as the script with the following content:API_ID=your_api_id_here API_HASH=your_api_hash_here
Replace
your_api_id_here
andyour_api_hash_here
with the credentials you obtained in step 2. -
Create a links file:
Create a text file containing the Telegram invite links you want to resolve, one per line. For example:
https://t.me/channel_name https://t.me/joinchat/INVITE_CODE
Run the script using Python:
python telegram_link_resolver.py path_to_links_file.txt
The first time you run the script, you'll need to authenticate with your Telegram account:
- The script will prompt you to enter your phone number with country code (e.g., +1234567890)
- Telegram will send a verification code to your Telegram app
- Enter this code when prompted by the script
After successful authentication, a session file will be created, and you won't need to authenticate again unless you delete the session file or run the script from a different location.
The script provides two types of output:
- Detailed results showing each link and its corresponding channel ID and title
- A list of just the channel IDs, which can be redirected to a file if needed
- The session file contains sensitive information. Keep it secure.
- Never share your API credentials or session file with others.
- Consider adding
.env
and*.session
to your.gitignore
if using version control.
- If you encounter rate limiting errors, increase the delay between requests by modifying the
await asyncio.sleep(1)
line. - Some invite links may be expired or invalid, which will result in error messages.
- If you're unable to authenticate, make sure your phone number is entered correctly and that your Telegram account is active.