Application for collecting data from the Telegram (part 1: Setup and first launch)
Published: 2021-10-31 08:05:14

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](https://my.telegram.org/auth?to=apps)
![Telegram Developers Registration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20og7qk6fjgbuuaoph7v.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcqaywzzqmr56h875skv.png)
If everything is entered correctly, you will see the following information.
![App Configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sff3pulzlsyf0fnwvvah.png)
*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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijs57ylkdojgubzhq3d3.png)

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.
```python
import configparser
from telethon import TelegramClient
```

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

```python
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()
```

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.
```python
async def main():
    about_me = await client.get_entity('me')
    print(about_me)
```
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

```python
with client:
    client.loop.run_until_complete(main())
```
>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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xyt5rkggv21myad5p96k.png)
 
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:

```python
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)
```
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:
```python
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())
```
#I wish you success!

[Write me on Face....oh...Meta](https://www.facebook.com/neo.skarynsky)

[My Twitter](https://twitter.com/decodesperato)

<a href="https://www.patreon.com/bePatron?u=64009903" data-patreon-widget-type="become-patron-button"><img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4kAQAgAW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/db3jtgy2i5l4w8slkps5.png" alt="Become a patron" loading="lazy"></a><script asyncsrc="https://c6.patreon.com/becomePatronButton.bundle.js"></script>