DEV Community

Cover image for Application for collecting data from the Telegram (part 1: Setup and first launch)
Vadim Kolobanov
Vadim Kolobanov

Posted on • Edited on • Originally published at dev.to

Application for collecting data from the Telegram (part 1: Setup and first launch)

Hello everyone parsing sites is fun, you can use this data for your resource or do it to order. And what if you download, for example, all the messages from a telegram chat or a list of its participants, and then use this data for analytics, or even better, to form your database of users who are interested in this or that topic.

In this series of articles, we will look at how to write our own separate Telegram client that will collect data from the chats we are interested in, and also see how to save this information to our database.
To create a separate client, the asynchronous library "Telethon" is well suited. The library itself can be used both to create telegram bots and to create separate applications working with the Telegram API. The main advantage is clear documentation in which you can find answers to all questions.

We will start creating our project by registering a developer account here
Telegram Developers Registration
We enter the numeric-letter code that came to Telegram and get to the registration page of the new application. Fill out the form, the first two fields are enough:
Create Application
If everything is entered correctly, you will see the following information.
App Configuration
I'll make a reservation right away, there will be a little more data, but the App api_id and App api_hash parameters are important to us.

Congratulations! You have registered your app.

It's not worth closing the page yet. We will take from there the values of App app_id, App api_hash, Short_name for our application.

Go to PyCharm

It would be a good tone not to store our hash and app_id in the code, so let's do it nicely. Use the configparser library to create a settings file. Create a file with the .ini extension in the root of the project (example config.ini) and let's put our data from the developer account that we have registered there.
For example:
Configuration

I know about venv and environment variables. You can use a method that is convenient for you.

Next, the most interesting thing awaits us. Let's install the Telethon library itself in our project with the "pip install telethon" command and import the TelegramClient class from our installed library into the project.



import configparser
from telethon import TelegramClient


Enter fullscreen mode Exit fullscreen mode

Next, let's configure the transfer of our data to the connection from the settings file



config = configparser.ConfigParser()
config.read("config.ini")
api_id: str = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
username = config['Telegram']['username']
client = TelegramClient(username, api_id, api_hash)
client.start()


Enter fullscreen mode Exit fullscreen mode

Please note that in the "config.ini" file, we specified [Telegram] as the first line. With these tags, we will simply not get confused about the settings variables and separate them in one file.

Let's create our main function and request information about us from the telegram server.



async def main():
    about_me = await client.get_entity('me')
    print(about_me)


Enter fullscreen mode Exit fullscreen mode

Telethon is asynchronous, which means we will use functions and methods with the addition of the keywords async and await

In order for our client not to close after launch, we will add such an entry at the end of our file



with client:
    client.loop.run_until_complete(main())


Enter fullscreen mode Exit fullscreen mode

Our app is almost ready for the first launch

At the first launch in the PyCharm console, you will be asked to enter your phone number or bot token
Phone request

This is necessary to create a session file, it will be stored in the root of the project with the extension .session (it's not worth deleting them, we'll talk about them later)

Enter your phone number in international format without "+"

You have received the telegram account code again, only now it consists of 5 digits. Enter them.

Congratulations you have launched your Telegram client.

So what did our main function return to us?

Our "about_me" variable now contains a User object with the telethon library data type.

Inside the object you can see data about your account.
In order to see some information, let's make this code:



async def main():
    about_me = await client.get_entity('me')
    print('Name:', about_me.first_name)
    print('Nickname:', about_me.username)
    print('Id', about_me.id)
    print('Number', about_me.phone)


Enter fullscreen mode Exit fullscreen mode

I hope you liked the result? =)

The Telegram server has just told you a little more about you than you see in your account.

Data leakage from Telegram is a Telegram problem.

All information that we will receive is publicly available. And we can see the same information through the official app. But I still urge you not to use the acquired knowledge for bad purposes. All the information presented is provided for the purpose of education and popularization of the programming language, its libraries and capabilities.

If you, dear reader, show your activity to my work, then in the following articles we will look at a way to get all messages from the telegram chat, as well as all participants of the chat or group, we will study ways to get the user's phone number even if it is hidden and learn how to find information about people in Telegram by their phone number.

Unfortunately English is not my native language, but I am working on it. I will be waiting for your suggestions, feedback and comments.

By tradition, the full code:



import configparser
from telethon import TelegramClient
config = configparser.ConfigParser()
config.read("config.ini")
api_id: str = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
username = config['Telegram']['username']
client = TelegramClient(username, api_id, api_hash)
client.start()
async def main():
about_me = await client.get_entity('me')
print('Name:', about_me.first_name)
print('Nickname:', about_me.username)
print('Id', about_me.id)
print('Phone', about_me.phone)
with client:
client.loop.run_until_complete(main())

Enter fullscreen mode Exit fullscreen mode




I wish you success!

Write me on Face....oh...Meta

My Twitter

Become a patron

Top comments (0)