LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.77k stars 1.39k forks source link

ValueError inside another ValueError #1221

Closed shosaman closed 5 years ago

shosaman commented 5 years ago

Checklist

Code that causes the issue

from telethon.sync import TelegramClient
...
for ListofFiles in IndexFileContent:
try:
  username = client.get_entity(UserPhoneNumber)
except ValueError:
  try:
    result = client(functions.contacts.ImportContactsRequest(
       contacts=[types.InputPhoneContact(
         client_id=random.randrange(-2**63, 2**63),
         phone=UserPhoneNumber,
         first_name=UserFirstName,
         last_name=UserLastName
       )]
    ))
  except ValueError:
    print ("Message NOT sent to " + UserFirstName + ' ' + UserLastName + ' ' + UserPhoneNumber)
    print ("Could not find "  + UserFirstName + ' ' + UserLastName + ' ' + UserPhoneNumber + " in Telegram")
    continue
    print ("Could not find "  + UserFirstName + ' ' + UserLastName + ' ' + UserPhoneNumber + " in Telegram")
    continue

username = client.get_entity(UserPhoneNumber)
client.send_message(username, 'Dear ' + UserFirstName + ', We are pleased to keep you updated on Telegram Messenger')
client.send_message(username, 'We sent you this information on your email ID as well')
  1. I'm doing a FOR LOOP through an index of Phone numbers
  2. First I try to get the client entity using client.get_entity(UserPhoneNumber)
  3. If this results in valueError - (I'm assuming it errors when I do not have this particular user in my contacts) I'm catching that error and try to add the Phone Number, First Name, Last name using ImportContactsRequest() function
  4. If ImportContactsRequest() also errors (I'm assuming it means this particular phone / user is not even registered in Telegram database)
  5. I'm trying to handle this exception and ignoring such an user and trying to move to the next iteration of the For Loop
  6. If ImportContactsRequest() does not result in any error, then I proceed further to the code
  7. The except ValueError: is not even printing the message above however it is going to the next line of code username = client.get_entity(UserPhoneNumber) and erroring out.

So except inside another except - I'm not able to make it work. Please help if I'm not doing it right - is this how it is intended to work

Traceback

Traceback (most recent call last):
  File "code.py", line 1, in <code>

Traceback (most recent call last): File "TelegramNew.py", line 117, in username = client.get_entity(UserPhoneNumber) File "/home/shosaman/.local/lib/python3.6/site-packages/telethon/sync.py", line 39, in syncified return loop.run_until_complete(coro) File "/usr/lib/python3.6/asyncio/base_events.py", line 473, in run_until_complete return future.result() File "/home/shosaman/.local/lib/python3.6/site-packages/telethon/client/users.py", line 285, in get_entity result.append(await self._get_entity_from_string(x)) File "/home/shosaman/.local/lib/python3.6/site-packages/telethon/client/users.py", line 521, in _get_entity_from_string 'Cannot find any entity corresponding to "{}"'.format(string) ValueError: Cannot find any entity corresponding to "+919449019181"

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "TelegramNew.py", line 134, in username = client.get_entity(UserPhoneNumber) File "/home/shosaman/.local/lib/python3.6/site-packages/telethon/sync.py", line 39, in syncified return loop.run_until_complete(coro) File "/usr/lib/python3.6/asyncio/base_events.py", line 473, in run_until_complete return future.result() File "/home/shosaman/.local/lib/python3.6/site-packages/telethon/client/users.py", line 285, in get_entity result.append(await self._get_entity_from_string(x)) File "/home/shosaman/.local/lib/python3.6/site-packages/telethon/client/users.py", line 521, in _get_entity_from_string 'Cannot find any entity corresponding to "{}"'.format(string) ValueError: Cannot find any entity corresponding to "+919449019181"

Lonami commented 5 years ago

This is working as intended.

shosaman commented 5 years ago

If so, when I say continue in the 2nd level except, it should go to next element of FOR loop rather than erroring out and does not process next loops of FOR

Lonami commented 5 years ago

Your issue is formatted very poorly, and only get_entity will raise ValueError, not raw methods.

shosaman commented 5 years ago

Thank you for the information. So how can I catch this raw method client(functions.contacts.ImportContactsRequest(....) if this has successfully given anything or not - I do not see any documentation on this particular function call. Please help. Thank you in advance.