Here is an example of .env
file you need to use:
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()) |
Hi!
Do you solve this problem?