Adding Telegram Group Members to Your Groups Using Script

In the past Telethon instructional exercise you figured out how to send messages to Telegram bunch individuals. Presently you will figure out how to add new individuals to your own gathering. We will peruse the part list from the csv document which we removed in the past instructional exercise and add them to our gathering.

Get Telegram API Key

As expressed in the past instructional exercise you have to get your Telegram API certifications to have the option to utilize the API. So don’t spare a moment and check the past instructional exercise and utilize the manual forget your own Telegram API accreditations, or basically follow these means:

• Sign up for Telegram utilizing any application.

• Log in to your Telegram center: https://my.telegram.org.

• Go to ‘Programming interface advancement instruments’ and round out the structure.

• You will get essential locations just as the api_id and api_hash parameters required for client approval.

Install Telethon 

Also, you need to install Telethon using PIP

Note: If you are on Linux or Mac, you might need to use sudo before pip to avoid permissions issues.

Create Client Object and Login

As normal we need to create  a Telegram customer item and check in the event that it is now approved in any case request an OTP secret word.

Create a client object.

Note: For the sake of simplicity we are importing  TelegramClient from telethon.sync module.

 

Next, you need to interface the customer and check on the off chance that it is now approved, in any case Telegram will send an OTP secret word to the customer and we will request that the client enter the code which they got.

 

Read Members from CSV File

We are passing the document name in the sys.argv parameters. sys.argv is a rundown in Python, which contains the order line contentions went to the content.

So in the event that you run python sendMessage.py users.csv in the order line  sys.argv will return a rundown like this:

As you can see, the first item in the list is the name of the script and the second item is the name of our CSV file. So we can access the file name with  sys.argv[1] .

Now you have the name (or path) of our CSV file and you can use Python’s csv module to read the file and create a list of users.

 

Import the required modules

 

Make a word reference for each client and affix that to our client list.

Note: You need to cast the user id and access hash to integer type.

Note: We skip the first row in the CSV file (header) using the  next function.

 

Choose a Group to Add Members

In this step, you are going to ask the user to select a group which they want to add the members to it.

First get all groups using  GetDialogsRequest .

 

Add only super groups to  groups list.

 

Now print all of the groups on screen with their indexes and ask the user to enter the group index they want.

 

Ask for input and get the group using entered index.

 

Get the group entity.

 

Ask User to Enter the Adding Mode

For adding the user we need to get the user entity first. There is two options to do this.

First let’s ask the user which mode they want and then we will explain them one by one.

 

1- By username

Basically you can get any entity in Telethon including users, channels or groups using its username. But as I mentioned before not every user has a username.  So if this mode is selected by user we will skip the users which don’t have a user name while adding. Here is how you can get the user entity by its username.

 

2- By Id and access hash

Every entity in Telegram has an ID and an access hash. The ID is unique but the access hash is different for every telegram account. It means if you scrape the users with an account and you want to add them using another account then you can’t use this mode.

 

Add Members to the Selected Group

Now you have the users in  users  list and the selected group in  target_group . We will use  InviteToChannelRequest function to the add a user to the group. So let’s import it first.

 

Then you need to get the user based on the entered mode (i.e by ID or by user name.

 

Handle Exceptions

Now you can start adding the users but you have to consider some error scenarios.

 

1- PeerFloodError

On the off chance that you put an excessive amount of solicitations on Telegram API or submit assignments exceptionally quick, you will get  PeerFloodError . So you have to put a holding up time between each add to forestall this blunder. We will utilize 60 seconds right now remain safe. Yet, you can play with this number and locate the ideal worth.

You can likewise utilize the random library’s randrange() method giving an arbitrary rest time, say somewhere in the range of 60 and 180 seconds. For the purpose for explanation, the code underneath clients “print”, however you can obviously erase it.

 

2- UserPrivacyRestrictedError

You may get this blunder for certain clients relying upon their protection settings.

At long last, we should include our new individuals.

 

3. FloodWaitError

On the off chance that you get this mistake, you ought to either hang tight for the expressed time or utilize another Telegram number.

telethon.errors.rpcerrorlist.FloodWaitError: A hold up of 58593 seconds is required

(brought about by SendCodeRequest)

So you should maintain a strategic distance from this blunder in any case. To do as such, it is prescribed to give an additional rest time (15 minutes or progressively) subsequent to including each 50 clients. On the off chance that this will take a lot of time, you can utilize a cloud server to run your code.

To get a general thought regarding how this functions, run this straightforward code. Here, you have a rundown of names; the code circles over names, rest for 5 seconds after every 10 names, and print the name.

 

As I referenced earlier, Telegram doesn’t permit adding in excess of 200 individuals to a gathering or station by one record. Utilizing this content, I am ready to include around 150-200 individuals with 60 second rest between each add; after that, you need to change to another Telegram account/number.

 

Project Source Code for Adding Members to Telegram Groups Tutorial

Here is the completed code for this tutorial.

 

Full Code with random and extra sleep after 50 users

 

 

Download the script

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here