Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active October 22, 2022 19:54
Show Gist options
  • Save aahnik/c060ce1e755ed06ae1975cc5ea0d096b to your computer and use it in GitHub Desktop.
Save aahnik/c060ce1e755ed06ae1975cc5ea0d096b to your computer and use it in GitHub Desktop.
Telethon , telegram , join telegram channels from a list

Here is an example of .env file you need to use:

Screenshot from 2021-01-15 17-21-41

Make sure to have telethon and python-dotenv installed.

# make sure to have telethon and python-dotenv installed
# create a file called .env in the current directory from where you are running the script
# put API_ID and API_HASH in the .env file in the following format
# VARIABLE=VALUE
from telethon.sync import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.errors.rpcerrorlist import FloodWaitError
from dotenv import load_dotenv
import os
import asyncio
load_dotenv()
API_ID = os.getenv('API_ID')
API_HASH = os.getenv('API_HASH')
CHANNELS = ['a', 'b', 'c'] # the channels you want to join
async def main():
async with TelegramClient('tg_session', API_ID, API_HASH) as client:
for channel in CHANNELS:
try:
await client(JoinChannelRequest(channel))
except FloodWaitError as fwe:
print(f'Waiting for {fwe}')
await asyncio.sleep(delay=fwe.seconds)
except Exception as err:
print(f"Encountered an error while joining {channel}\n{err}")
asyncio.run(main())
@aahnik
Copy link
Author

aahnik commented May 7, 2021

hello, how to replace CHANNELS = [ ] with CHANNELS = chat.txt. In the file chat.txt link in the format https://t.me/****. Please help me.

you need to write some python code for that.

read the text file, and load iterate over each line.

replace this line

CHANNELS = ['a', 'b', 'c']  # the channels you want to join

with these

with open("channels.txt") as file:
    CHANNELS = [ line.strip() for line in file.readlines() ]

hi @MaksGruv52, please test and let me know.

@MaksGruv52
Copy link

MaksGruv52 commented May 7, 2021

please tell me how to add the function of skipping the channel error (if it does not exist or the channel is banned) and the script continued to add it?

@aahnik
Copy link
Author

aahnik commented May 8, 2021

hi, @MaksGruv52, I have updated the code in the gist. Please try again.

Put a few invalid channels, and some valid channels, on encountering an error, it will not stop. It will print the error on the terminal, and continue with the next channel.


star this gist, so that it reaches more people!

@MaksGruv52
Copy link

Hi, thank you very much! I'll start testing it now!

@N0us1caa
Copy link

How to add a group interval? You will soon be prompted "waiting for a wait of * * * * seconds is required"

@zlk-pixel
Copy link

zlk-pixel commented Oct 28, 2021

hy man! im trying to run your code, but i always got an error message:

The API access for bot users is restricted. The method you tried to invoke cannot be executed as a bot (caused by JoinChannelRequest)
Encountered an error while joining @cryptolinksgroup

at point it asked my phone, or bot token. my phone have a token access? i thought only api id, and hash have.

@BlackBigMind
Copy link

hi, @aahnik! how do I make it so that the cat can access channels via private links?

@naut20161990
Copy link

naut20161990 commented Sep 28, 2022

hi
please show me why it is not working

help

telethon and python-dotenv installed.

@weluponb
Copy link

Hi!
Do you solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment