How to Get Data From Telegram Using Python
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 😝.
- Register on telegram with your mobile number.
- Create a telegram app here.
- Note down App api_id and App api_hash.
- 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.