10 Commits

Author SHA1 Message Date
Galen Reich
fe57ce1443 Match accounts by id instead of username (#15)
* Replace account matching by (optional) username with (mandatory) id field
* Use get instead of direct key retrieval
* Bump version
2024-02-12 15:03:12 +00:00
msramalho
4011e916c8 removes unused imports 2024-02-08 17:50:41 +00:00
msramalho
2a503856c2 fix poetry build default env file is empty 2024-02-08 15:08:35 +00:00
msramalho
d7f5415a4e fixes .env not working from CLI 2024-02-08 12:52:50 +00:00
msramalho
9ed496d690 remove whitespace in phone numbers 2024-01-30 22:37:00 +00:00
msramalho
3c9cb2ab99 click.prompt to input for notebooks compatibility 2024-01-25 17:35:48 +00:00
msramalho
dd6108a449 fix urls 2024-01-25 17:09:14 +00:00
msramalho
a649ce5ab6 pypi urls 2024-01-25 17:08:51 +00:00
msramalho
2b7dea31fd minor documentation improvements 2024-01-25 17:07:25 +00:00
msramalho
2a873a3733 fix badge 2024-01-25 17:02:08 +00:00
3 changed files with 38 additions and 28 deletions

View File

@@ -1,10 +1,11 @@
# telegram-phone-number-checker
Python tool/script toc heck if phone numbers are connected to Telegram accounts.
Python tool/script to check if phone numbers are connected to Telegram accounts. Retrieving username, name, and IDs where available.
## Installation
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/telegram-phone-number-checker)](https://pypi.org/project/telegram-phone-number-checker/)
[![PyPI - Version](https://img.shields.io/pypi/v/telegram-phone-number-checker)
](https://pypi.org/project/telegram-phone-number-checker/)
You can install this tool directly from the [official pypi release](https://pypi.org/project/telegram-phone-number-checker/).
@@ -56,7 +57,7 @@ The result will be written to the console but also written as JSON to a `results
For each phone number, you can expect the following possible responses:
1. If available, you will receive the Telegram Username,Name, and ID that are connected with this number.
1. If available, you will receive the Telegram Username, Name, and ID that are connected with this number.
2. 'no username detected'. This means that it looks like the number was used to create a Telegram account but the user did not choose a Telegram Username. It is optional to create a Username on Telegram.
3. 'ERROR: no response, the user does not exist or has blocked contact adding.': There can be several reasons for this response. Either the phone number has not been used to create a Telegram account. Or: The phone number is connected to a Telegram account but the user has restricted the option to find him/her via the phone number.
4. Or: another error occurred.

View File

@@ -1,10 +1,14 @@
[tool.poetry]
name = "telegram-phone-number-checker"
version = "1.0.0"
version = "1.0.6"
description = "Check if phone numbers are connected to Telegram accounts."
authors = ["Bellingcat"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/bellingcat/telegram-phone-number-checker"
[tool.poetry.urls]
"Bug Tracker" = "https://github.com/bellingcat/telegram-phone-number-checker/issues"
[tool.poetry.scripts]
telegram-phone-number-checker = "telegram_phone_number_checker.main:main_entrypoint"

View File

@@ -1,13 +1,10 @@
import os, json
from telethon.sync import TelegramClient, errors
import os, json, re
from telethon.sync import TelegramClient, errors, functions
from telethon.tl.types import InputPhoneContact
from telethon import functions
from dotenv import load_dotenv
from getpass import getpass
import click
load_dotenv()
def get_names(client, phone_number):
"""
This function takes in a phone number and returns the username first name and the last name of the user if the user exists. It does so by first adding the user's phones to the contact list, retrieving the information, and then deleting the user from the contact list.
@@ -15,23 +12,30 @@ def get_names(client, phone_number):
result = {}
print(f'Checking: {phone_number=} ...', end="", flush=True)
try:
# Create a contact
contact = InputPhoneContact(client_id = 0, phone = phone_number, first_name="", last_name="")
# Attempt to add the contact from the address book
contacts = client(functions.contacts.ImportContactsRequest([contact]))
username = contacts.to_dict()['users'][0]['username']
if not username:
result.update({"error": f'ERROR: no username detected'})
del_usr = client(functions.contacts.DeleteContactsRequest(id=[username]))
else:
result.update({"username": username})
del_usr = client(functions.contacts.DeleteContactsRequest(id=[username]))
# getting more information about the user
id = del_usr.to_dict()['users'][0]['id']
first_name = del_usr.to_dict()['users'][0]['first_name']
last_name = del_usr.to_dict()['users'][0]['last_name']
result.update({"first_name": first_name, "last_name": last_name, "id": id})
except IndexError as e:
result.update({"error": f'ERROR: no response, the user does not exist or has blocked contact adding.'})
users = contacts.to_dict().get('users', [])
number_of_matches = len(users)
if number_of_matches == 0:
result.update({"error": f'No response, the phone number is not on Telegram or has blocked contact adding.'})
elif number_of_matches == 1:
user = users[0]
# Attempt to remove the contact from the address book
client(functions.contacts.DeleteContactsRequest(id=[user.get('id')]))
# getting more information about the user
result.update({
"id": user.get('id'),
"username": user.get('username'),
"first_name": user.get('first_name'),
"last_name": user.get('last_name')
})
else:
result.update({"error": f'This phone number matched multiple Telegram accounts, which is unexpected. Please contact the developer: contact-tech@bellingcat.com'})
except TypeError as e:
result.update({"error": f"TypeError: {e}. --> The error might have occurred due to the inability to delete the {phone_number=} from the contact list."})
except Exception as e:
@@ -46,9 +50,9 @@ def validate_users(client, phone_numbers):
The function uses the get_api_response function to first check if the user exists and if it does, then it returns the first user name and the last user name.
'''
if not phone_numbers or not len(phone_numbers):
phone_numbers = click.prompt('Enter the phone numbers to check, separated by commas')
phone_numbers = input('Enter the phone numbers to check, separated by commas: ')
result = {}
phones = [p.strip() for p in phone_numbers.split(",")]
phones = [re.sub(r"\s+", "", p, flags=re.UNICODE) for p in phone_numbers.split(",")]
try:
for phone in phones:
if phone not in result:
@@ -62,9 +66,9 @@ def validate_users(client, phone_numbers):
def login(api_id, api_hash, phone_number):
"""Create a telethon session or reuse existing one"""
print('Logging in...', end="", flush=True)
API_ID = api_id or os.getenv('API_ID') or click.prompt('Enter your API ID')
API_HASH = api_hash or os.getenv('API_HASH') or click.prompt('Enter your API HASH')
PHONE_NUMBER = phone_number or os.getenv('PHONE_NUMBER') or click.prompt('Enter your phone number')
API_ID = api_id or os.getenv('API_ID') or input('Enter your API ID: ')
API_HASH = api_hash or os.getenv('API_HASH') or input('Enter your API HASH: ')
PHONE_NUMBER = phone_number or os.getenv('PHONE_NUMBER') or input('Enter your phone number: ')
client = TelegramClient(PHONE_NUMBER, API_ID, API_HASH)
client.connect()
if not client.is_user_authorized():
@@ -91,6 +95,7 @@ def show_results(output, res):
@click.option('--output', help='results filename, default to results.json', default="results.json", type=str)
def main_entrypoint(phone_numbers, api_id, api_hash, api_phone_number, output):
"""Check to see if one or more phone numbers belong to a valid Telegram account"""
load_dotenv(".env")
client = login(api_id, api_hash, api_phone_number)
res = validate_users(client, phone_numbers)
show_results(output, res)