**Hi , i wanted to fetch user’s info (username and profile picture) using their phone number using telethon.
but i got an error related to rate limit of the request ImportContactRequest.
consider i have a file containing thousands of phone numbers of company’s customers, i did process the number, made a list then wanted to fetch data for each number, the below is some of the related code :
at main.py file :**
.....
counter = 0
for number in numbers:
try:
print(f"executing no.{counter}: {number}")
x = await client.fetch_user_info(number)
if x:
results.append(x)
counter += 1
# TRIED TO SLOW DOWN THE REQUESTS HERE
if counter % 30 == 0:
await asyncio.sleep(30)
else:
await asyncio.sleep(2.2)
except Exception as e:
print(f"ERROR HAPPENED DURING PROCCESS OF NUMBERS => {e}")
.....
at fetcher.py which containing the Fetcher class which offers method like connecting to telegram and other method but the method in discuss here is the below :
class Fetcher:
....
# OTHER CODES INCLUDE THE INSTANTIATING TelegramClient OBJECT, AND STARTING A SESSION.
# THESE ARE WORKING FINE
....
# THIS METHOD DOING THE FETCH DATAS JOB
async def fetch_user_info(self, contact_number):
contact = InputPhoneContact(
client_id=random.randrange(2**63),
phone=contact_number,
first_name="temp",
last_name="temp",
)
max_retries = 5
# TRIED TO IMPLEMENT SOME SORT OF EXPONENTIAL BACKOFF IF RATE LIMIT HAPPENED
for attempt in range(max_retries):
try:
data = await self.client(ImportContactsRequest([contact]))
if data.users:
user = data.users[0]
user_id = user.id
username = user.username
photo_file_path = None
if user.photo:
user_photos = await self.client(
GetUserPhotosRequest(
user_id=user_id, offset=0, max_id=0, limit=1
)
)
if user_photos.photos:
photo = user_photos.photos[0]
photo_file_path = (
f"reports/profile_pictures/pf_{contact_number}.jpg"
)
await self.client.download_media(
photo, file=photo_file_path
)
await self.client(
DeleteContactsRequest(
id=[
InputUser(user_id=user_id, access_hash=user.access_hash)
]
)
)
return {
"contact_number": contact_number,
"has_account": True,
"username": username,
"pf_file_path": photo_file_path,
}
else:
return {"contact_number": contact_number, "has_account": False}
except errors.FloodWaitError as e:
wait_time = e.seconds
print(f"Rate limit exceeded. Waiting for {wait_time} seconds...")
await asyncio.sleep(wait_time)
except Exception as e:
print(f"An error occurred: {e}")
if attempt < max_retries - 1:
sleep_time = 2**attempt
print(f"Retrying in {sleep_time} seconds...")
await asyncio.sleep(sleep_time)
else:
raise
**as you see i tried to slow down the requests and implemented exponential backoff , the issue is even with slowing down the proccess telegram automatically terminates all sessions related to the phone number i instantiated TelegramClient with. (which is my personal number) ,, tried to use a bot because i thought the rate limit for a bot will be different but for bot the mentioned method is not provided at all ( you can’t fetch user data based on phone number )
SO IS THERE ANY WAY TO DO IT AT ONE GO ? IS THERE SOMETHING I AM DOING WRONG ? IS THERE A BETTER WAY FOR GETTING WHAT I WANT DONE ?**