Sending Message to Telegram Members Using Telethon

In this tutorial, we are going to use the CSV file that we generated in Scraping Telegram Members tutorial to send bulk messages to our scraped Telegram group members using Telegram API and Python Telethon library. So if you did not already checked that, visit this tutorial and learn how to get this file. Basically we will 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 Key

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 check the previous tutorial and use the guide to get your own Telegram API credentials, or simply follow these steps:

• Sign up for Telegram using any application.
• Log in to your Telegram core: https://my.telegram.org.
• Go to ‘API development tools’ and fill out the form.
• You will get basic addresses as well as the api_id and api_hash parameters required for user authorization.

 

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.

Let’s get started.

 

Create Client Object and Login

Whenever you want to use Telethon, first step should be logging into the API.  So you have to create a client object and check if it’s already authorized otherwise ask for an OTP password.

So create your client object first.

 

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

Read Members from CSV File

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

So if you run python sendMessage.py users.csv in the command line  sys.argv will return a list 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

 

Create a dictionary for every user and append that to our user 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.

 

Prompt to Send by User ID or Username

As I mentioned in the previous tutorial, not all telegram members have a username. Also, access hash for every user is different for every Telegram account. It means that if you scrape users with a Telegram account and you want to send messages with another account then you cannot use those access hashes to get the users. But you can use usernames. To solve this problem and avoid errors we will ask your Python script user whether he/she wants to use Telegram username or user ID to send the messages.

Send 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. Also, format the message to include the name of the user.

Now, loop through every user and send a message. Consider that if you send messages too quickly you will get a  PeerFloodError from Telegram. So we will handle this error using a try, except block and exit the program if it happened. Otherwise the account might get banned from Telegram because of spamming. Also, add a sleep time between each message to avoid flood error.

We are going to do it step by step.

 

Loop through every user and get the receiver entity depending on the “mode” entered before.

 

Select a message from message list randomly.

 

Format the message with user’s name and send it.

 

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

Project Source Code for Messaging Telegram Members Tutorial

Here is the completed code for this tutorial.

 

 

 

Rating: 4.5/5. From 14 votes.
Please wait...

6 Replies to “Sending Message to Telegram Members Using Telethon”

  1. very good sample, I enjoy it. thank you

    Rating: 4.0/5. From 5 votes.
    Please wait...
  2. How to Handle Flooderror in this program?

    Rating: 4.8/5. From 6 votes.
    Please wait...
    1. Telegram is sensitive and if one of the users reported abuse, this is a problem.

      Rating: 2.3/5. From 3 votes.
      Please wait...
    2. This error means you are too many send message to telegram users. Telegram bans your account for 1×24 hour

      Rating: 5.0/5. From 3 votes.
      Please wait...
  3. hi GoTrained,

    How to solve “An invalid Peer was used. Make sure to pass the right peer type (caused by SendMessageRequest).

    Thanks

    Rating: 4.3/5. From 12 votes.
    Please wait...
  4. try to send the message with user id and An invalid peer was used will show up

    Rating: 1.7/5. From 6 votes.
    Please wait...

Comments are closed.