Checking if a phone number is on Telegram, using TDLib and Python
Tags: python, tutorial, telegram, api
Published: 2021-01-28 21:12:55

This may come in handy if you plan to migrate, for example, an existing WhatsApp group. A good number of users that have already migrated may be a convincing argument to promote such change, specially in communities where Telegram wasn't that well known so far.

In this tutorial we'll be using the [Telegram Database Library][1] (or TDLib), which lets you build your own Telegram clients. Happily, there is a nice [python wrapper][2] for it.

## 1. Setting up

The first thing is [registering a new Telegram Application][3]. 

Once that we have both **api_id** and **api_hash**, we need to get `python-telegram`. Make sure you use Python 3.6+.

```shell
python3 -m pip install python-telegram
```

## 2. Calling the API

We are ready to instantiate the client.

```python
from telegram.client import Telegram

tg = Telegram(
    api_id='YOUR API ID',
    api_hash='YOUR API HASH',
    phone='+575555555555',
    database_encryption_key='changeme1234',
)

tg.login()
```
A prompt will wait for us to enter a verification code, which will be sent to our telegram account.

**Note**: `database_encryption_key` is just a string TDLib [needs][10] in order to encrypt and decrypt the local database. If you're going to build a real Telegram client, make sure this key is a strong one (just like a password). For this exercise it may not be worth coming up with a super secure key, but this is up to you.

The `Telegram` class has some TDLib methods available, like `login()`, `send_message()`, and [a few more][4]. For [any other][5] we need to use [`call_method()`][6].

In order to check whether a phone number is using Telegram or not, we need to add it as a contact. [`importContacts()`][7] is what we are looking for. It let us add new contacts just by their phone numbers, though you can also specify things like `first_name`, `last_name`, and [others][8].

```python
response = tg.call_method('importContacts', {
    'contacts': [
        {'phone_number': '+57 555 123 4567'},
    ]
})

response.wait()

user_ids = response.update['user_ids']
```

If a number is not currently on the platform, it will return `0` as its user id.

```python
if user_ids[0] == 0:
    print('This contact is NOT using Telegram.')
else:
    print(f'¡This contact({user_ids[0]}) uses Telegram!')
```

## 3. Cleaning

Time to remove those testing contacts.

```python
tg.call_method('removeContacts', {'user_ids': user_ids})
```


And that's it. Hope you enjoyed my first tutorial 🧪

Happy hacking!




[![Link to my Buy me a coffee account](https://dev-to-uploads.s3.amazonaws.com/i/1slnz77dvrx76i3u0u7s.png)][9]

[1]: https://core.telegram.org/tdlib
[2]: http://python-telegram.readthedocs.io/
[3]: https://core.telegram.org/api/obtaining_api_id
[4]: https://python-telegram.readthedocs.io/en/0.13.0/telegram.html
[5]: https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1_function.html
[6]: https://python-telegram.readthedocs.io/en/0.13.0/telegram.html#telegram.client.Telegram.call_method
[7]: https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1import_contacts.html
[8]: https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1contact.html
[9]: https://www.buymeacoffee.com/luvejo
[10]: https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1authorization_state_wait_encryption_key.html