|
|
|
|
@@ -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,31 @@ 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:
|
|
|
|
|
# Attempt to remove the contact from the address book
|
|
|
|
|
# The response from DeleteContactsRequest contains more information than from ImportContactsRequest
|
|
|
|
|
del_user = client(functions.contacts.DeleteContactsRequest(id=[users[0].get('id')]))
|
|
|
|
|
user = del_user.to_dict().get('users')[0]
|
|
|
|
|
# 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:
|
|
|
|
|
@@ -48,7 +53,7 @@ def validate_users(client, phone_numbers):
|
|
|
|
|
if not phone_numbers or not len(phone_numbers):
|
|
|
|
|
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:
|
|
|
|
|
@@ -91,6 +96,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)
|
|
|
|
|
|