How to Get Data From Telegram Using Python

NITHIN BHARADWAJ.D
2 min readJul 16, 2021

--

As part of analyzing telegram chat, I was exploring the telethon python package. But the tutorials out there on the wild internet, have been redundant as async behaviour was made default for the telethon.

If you have a channel where you are planning to extract up to 5k messages, then sync behavior will complete the task in a decent amount of time. But if you are planning to extract more messages(Maybe 10k+ messages) and need to do it optimally async is the go-to option.

To those who just want to extract messages from the channels for their initial analysis telethon’s sync should be sufficient. Please follow the below steps to set up and extract the info from the channel.

Pre-requisites

First, you need to have a telegram account in order to extract information. Follow the below steps to get your api_id and api_hash. As these are secrets, I suggest not to hard code these values in the script directly and push them to GitHub 😝.

  1. Register on telegram with your mobile number.
  2. Create a telegram app here.
  3. Note down App api_id and App api_hash.
  4. You should be a member of the telegram channel to extract the messages from the channel. If you are not, please join using the invitation link.

After the above steps are complete, install the Telethon python package on your system using the terminal command pip install telethon.

Create a Telegram client in your Python Script

Before we jump into python script, to store your credentials a config file can be created and python configparser can be used to parse the config file. Below is a sample.

After you are done setting up the config file, below is a simple script that reads the messages from the channel and dumps the messages to CSV using pandas data frame.

If you are planning to dump the JSON format, please feel free to change the code according to your needs.

--

--

NITHIN BHARADWAJ.D

Incoming Student at Northeastern| Ex.Senior Data Analyst at @Netradyne , Bangalore.