forked from bellingcat/telegram-phone-number-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram-phone-validation.py
67 lines (57 loc) · 2.4 KB
/
telegram-phone-validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/local/bin/python3
from telethon import TelegramClient, errors, events, sync
from telethon.tl.types import InputPhoneContact
from telethon import functions, types
from dotenv import load_dotenv
import argparse
import os
from getpass import getpass
load_dotenv()
result = {}
API_ID = os.getenv('API_ID')
API_HASH = os.getenv('API_HASH')
PHONE_NUMBER = os.getenv('PHONE_NUMBER')
def get_names(phone_number):
try:
contact = InputPhoneContact(client_id = 0, phone = phone_number, first_name="", last_name="")
contacts = client(functions.contacts.ImportContactsRequest([contact]))
username = contacts.to_dict()['users'][0]['username']
if not username:
print("*"*5 + f' Response detected, but no user name returned by the API for the number: {phone_number} ' + "*"*5)
del_usr = client(functions.contacts.DeleteContactsRequest(id=[username]))
return
else:
del_usr = client(functions.contacts.DeleteContactsRequest(id=[username]))
return username
except IndexError as e:
return f'ERROR: there was no response for the phone number: {phone_number}'
except TypeError as e:
return f"TypeError: {e}. --> The error might have occured due to the inability to delete the {phone_number} from the contact list."
except:
raise
def user_validator():
'''
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.
'''
input_phones = input("Phone numbers: ")
phones = input_phones.split()
try:
for phone in phones:
api_res = get_names(phone)
result[phone] = api_res
except:
raise
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Check to see if a phone number is a valid Telegram account')
args = parser.parse_args()
client = TelegramClient(PHONE_NUMBER, API_ID, API_HASH)
client.connect()
if not client.is_user_authorized():
client.send_code_request(PHONE_NUMBER)
try:
client.sign_in(PHONE_NUMBER, input('Enter the code (sent on telegram): '))
except errors.SessionPasswordNeededError:
pw = getpass('Two-Step Verification enabled. Please enter your account password: ')
client.sign_in(password=pw)
user_validator()
print(result)