In this tutorial, we are going to use the CSV file that we generated in Scraping Telegram Members tutorial to transport bulk messages to our scraped Telegram group members using Telegram API and Python Telethon library. And so if you did not already checked that, visit this tutorial and acquire how to become this file. Basically we volition feed our new Python script with that CSV file to read usernames or user IDs from it and send a message to them.

Get Telegram API Fundamental

As stated in the previous tutorial you need to get your Telegram API credentials to be able to use the API. So don't hesitate and bank check the previous tutorial and use the guide to get your own Telegram API credentials, or simply follow these steps:

• Sign upwards for Telegram using whatever awarding.
• Log in to your Telegram cadre: https://my.telegram.org.
• Go to 'API development tools' and fill out the form.
• You will become basic addresses as well as the api_id and api_hash parameters required for user authorization.

Install Telethon

Also yous 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.

Allow'southward go started.

Create Client Object and Login

Whenever y'all want to use Telethon, starting time step should be logging into the API.  So you have to create a client object and check if information technology'south already authorized otherwise inquire for an OTP countersign.

So create your customer object beginning.

Connect and check if the customer is authorized. Otherwise prompt the user to enter the code they received from Telegram.

Read Members from CSV File

We are passing the file proper name in the sys . argv  parameters. sys . argv  is a listing in Python, which contains the control-line arguments passed to the script.

So if you run python sendMessage . py users . csv  in the command line sys . argv  will render a list like this:

Every bit you can see, the first item in the list is the name of the script and the 2d item is the proper noun of our CSV file. So we can access the file name with sys . argv [ i ]  .

Now y'all have the proper noun (or path) of our CSV file and y'all tin can employ Python's csv module to read the file and create a list of users.

Import the required modules

Create a dictionary for every user and append that to our user listing.

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

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

Prompt to Send by User ID or Username

Equally I mentioned in the previous tutorial, not all telegram members have a username. Also, access hash for every user is different for every Telegram business relationship. It ways that if you scrape users with a Telegram business relationship and you want to transport messages with another account and then you cannot use those access hashes to become the users. Simply y'all tin utilise usernames. To solve this problem and avert errors we will ask your Python script user whether he/she wants to use Telegram username or user ID to send the messages.

Ship Messages

Now, you have all the requirements to  start sending messages.

Create a list of messages and randomly select one of them to send to a user. As well, format the bulletin to include the proper name of the user.

Now, loop through every user and send a message. Consider that if you lot send letters too quickly y'all will get a PeerFloodError  from Telegram. And then we will handle this fault using a try, except cake and exit the plan if information technology happened. Otherwise the account might get banned from Telegram because of spamming. Likewise, add a sleep fourth dimension between each message to avoid inundation error.

Nosotros are going to do it footstep past step.

Loop through every user and get the receiver entity depending on the "fashion" entered earlier.

Select a bulletin from message list randomly.

Format the message with user'due south proper noun and ship it.

The complete code block for sending messages including exception handling will await like this:

Project Source Lawmaking for Messaging Telegram Members Tutorial

Here is the completed code for this tutorial.

Rating: 4.seven/5. From xi votes.

Please wait...