Thursday, May 5, 2022

Telegram Export Group Members more than 10000

FloodWaitError - A wait of 30 seconds is required / Telegram Export Group Members more than 10000

aggressive=True attribute was used to export group members more than 10 K.

This attribute was used in the following line of code of Telegram export group members python script.

all_participants = client.get_participants(target_group, aggressive=True)
But recently telethon has started giving FloodWait error with this attribute.
If we remove it, or make it false, we can download members but less than 10k.

all_participants = client.get_participants(target_group, aggressive=False)

But Removing the attribute or making it false can export members of a group which has less than 10K members but it gives following error for the groups which have more than 10k members.

TypeError: 'ChannelParticipants' object is not subscriptable
github page of this issue suggests a solution for the error. Upgrade your Telethon package to 1.24.0 using following command.

python3 -m pip install --upgrade https://github.com/LonamiWebs/Telethon/archive/master.zip
but after upgrading telethon some users have started getting this new error.
Traceback (most recent call last):
  File "t1_exportgroup.py", line 1, in <module>
    from telethon.sync import TelegramClient
  File "/home/pavi/virtualenvs/telegram/lib/python3.8/site-packages/telethon/__init__.py", line 4, in <module>
    from ._misc import utils as _  # depends on helpers and _tl
  File "/home/pavi/virtualenvs/telegram/lib/python3.8/site-packages/telethon/_misc/utils.py", line 23, in <module>
    from . import markdown, html
  File "/home/pavi/virtualenvs/telegram/lib/python3.8/site-packages/telethon/_misc/markdown.py", line 8, in <module>
    import markdown_it
ModuleNotFoundError: No module named 'markdown_it'
A solution was also provided for the error. Install following packages.

pip install markdown-it-py~=1.1.0
pip install pyaes~=1.6.1
pip install rsa~=4.7.2
After applying this solution above error is gone but another error is here.
Traceback (most recent call last):
  File "t1_exportgroup.py", line 1, in <module>
    from telethon.sync import TelegramClient
ModuleNotFoundError: No module named 'telethon.sync'

Telethon 2 has removed sync module. If your export script is showing this error, you need to change the script according to the changes suggested in this version 2 migration guide.

Friday, April 15, 2022

Telegram - Import Members into a Channel

There are two conditions to Import / Export members of a telegram channel.

1) You should be an administrator or owner of the channel, then only you will be able to import or export members.

2) If a member has selected ‘My Contacts’ option in ‘who can add you to groups and channels’ then you should be added in his/her contact list only then you can import him/her in the channel.

To import channel members, First Generate api id and api hash in your telegram account. To do this, open my.telegram.org
Login using your registered phone number.
Now Open Link Development Tools and copy your api id and hash
We will use the api id and hash in the Python Script

 Create virtual environment with python 3 and install Telethon using pip.

virtualenv telegram -p /usr/bin/python3
source telegram/bin/activate
pip install Telethon==1.23.0
Update API ID, API hash, telegram registered phone number and channel username(it can be found on the channel description page) in the following python script import.py.

from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty, InputPeerChannel, InputPeerUser
from telethon.errors.rpcerrorlist import PeerFloodError, UserPrivacyRestrictedError
from telethon.tl.functions.channels import InviteToChannelRequest
import sys
import csv
import traceback
import time

api_id = 9999999
api_hash = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'

client = TelegramClient('session_name', api_id, api_hash)
client.start()

channel = client.get_entity('pythonlovers07')
print(channel)

input_file = 'members.csv'
users = []
with open(input_file, encoding='UTF-8') as f:
    rows = csv.reader(f,delimiter=",",lineterminator="\n")
    next(rows, None)
    for row in rows:
        user = {}
        user['username'] = row[0]
        user['id'] = int(row[1])
        user['access_hash'] = int(row[2])
        user['name'] = row[3]
        users.append(user)
        
 
mode = int(input("Enter 1 to add by username or 2 to add by ID: "))

for user in users:
    try:
        print ("Adding {}".format(user['id']))
        if mode == 1:
            if user['username'] == "":
                continue
            user_to_add = client.get_input_entity(user['username'])
            print(user['username'])
        elif mode == 2:
            user_to_add = InputPeerUser(user['id'], user['access_hash'])
        else:
            sys.exit("Invalid Mode Selected. Please Try Again.")
        target_group = 'pythonlovers07'
        target_group_entity = InputPeerChannel(channel.id,channel.access_hash)
        print(target_group_entity)
        print(user_to_add)               
        client(InviteToChannelRequest(target_group_entity,[user_to_add]))
        print("Waiting 60 Seconds...")
        time.sleep(60)
    except PeerFloodError:
        print("Getting Flood Error from telegram. Script is stopping now. Please try again after some time.")
    except UserPrivacyRestrictedError:
        print("The user's privacy settings do not allow you to do this. Skipping.")
    except:
        traceback.print_exc()
        print("Unexpected Error")
        continue
Run the Python script in the same virtual env.

python import.py

members.csv should be in same directory where you are running the script. members.csv has all the exported channel members. 

It will list all your channels. Choose number of the channel into you want to import members. Members will be imported in the channel.

To see the steps :

 



If you want to export telegram channel members, check other post of the blog.

https://linuxamination.blogspot.com/2022/04/telegram-export-members-of-channel.html






       

Telegram - Export Members of a Channel

There are two conditions to Import / Export members of a telegram channel.

1) You should be an administrator or owner of the channel, then only you will be able to import or export members.

2) If a member has selected ‘My Contacts’ option in ‘who can add you to groups and channels’ then you should be added in his/her contact list only then you can add him/her in the channel.

To export channel members, First Generate api id and api hash in your telegram account. To do this, open my.telegram.org
Login using your registered phone number.
Now Open Link Development Tools and copy your api id and hash
We will use the api id and hash in the Python Script

 Create virtual environment with python 3 and install Telethon using pip.

virtualenv telegram -p /usr/bin/python3
source telegram/bin/activate
pip install Telethon==1.23.0

Update API ID, API hash, telegram registered phone number and channel username(it can be found on the channel description page) whose members you want to export in the following python script export.py.

from telethon import TelegramClient, sync
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.functions.channels import GetFullChannelRequest
from telethon.tl.types import ChannelParticipantsSearch

api_id = 9999999
api_hash = 'zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'
phone_number = '+919999999999'
channel_username = 'rajcomicsoldads'

client = TelegramClient(phone_number, api_id, api_hash).start()

# get all the channels that I can access
channels = {d.entity.username: d.entity
            for d in client.get_dialogs()
            if d.is_channel}

# choose the one that I want list users from
channel = channels[channel_username]
#print(client.get_participants(channel))
# get all the users and print them
print('username',",",'user id',",",'access hash',",",'name',sep='')
for u in client.get_participants(channel):
    print(u.username,",",u.id,",",u.access_hash,",",u.first_name," ",u.last_name,sep='')

Run the Python script in the same virtual env.

python export.py

It will list all your channels. Choose number whose members you want to export.

It will list all members of the channel in the csv format. Copy this output in the file members.csv 

To see the steps :


 

Telegram Import Channel Members :

 https://linuxamination.blogspot.com/2022/03/telegram-import-members-of-channel.html