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())
@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