alexander-akhmetov / python-telegram

Python client for the Telegram's tdlib
MIT License
610 stars 122 forks source link

Support authorizationStateWaitRegistration state #142

Closed fah closed 3 years ago

fah commented 3 years ago

After manually adding a code from an SMS I received I got the message:

ValueError: 'authorizationStateWaitRegistration' is not a valid AuthorizationState

It looks like this state is not handled at the moment.

According to this page https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1authorization_state_wait_registration.html The user is unregistered and need to accept terms of service and enter their first name and last name to finish registration.

Addition question: Is there a way to add name and term acceptance right now?

I replaced secrets with "11".

Error message:

/Users/f/PyCharm/firstTest/venv/bin/python /Users/f/PyCharm/firstTest/Main.py
[ 2][t 4][1606988251.419583082][AuthDataShared.cpp:109][#1][!Td]    DcId{2} [auth_key_id:11][state:NoAuth][created_at:11.000000]
[ 2][t 4][1606988251.419795990][SessionMultiProxy.cpp:121][#1][!SessionMultiProxy:2:main]   [session_count:1]
[ 2][t 4][1606988251.420382023][Session.cpp:147][#1][!SessionProxy:2:main]  Generate new session_id 11 for auth key 11 for main DC10002
ValueError: 'authorizationStateWaitRegistration' is not a valid AuthorizationState

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/f/PyCharm/firstTest/Main.py", line 3, in <module>
    tg = Telegram(
  File "/Users/f/PyCharm/firstTest/venv/lib/python3.8/site-packages/telegram/client.py", line 134, in __init__
    self.login()
  File "/Users/f/PyCharm/firstTest/venv/lib/python3.8/site-packages/telegram/client.py", line 574, in login
    self.authorization_state = self._wait_authorization_result(result)
  File "/Users/f/PyCharm/firstTest/venv/lib/python3.8/site-packages/telegram/client.py", line 514, in _wait_authorization_result
    return AuthorizationState(authorization_state)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py", line 309, in __call__
    return cls.__new__(cls, value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py", line 600, in __new__
    raise exc
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py", line 584, in __new__
    result = cls._missing_(value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/enum.py", line 613, in _missing_
    raise ValueError("%r is not a valid %s" % (value, cls.__name__))
ValueError: 'authorizationStateWaitRegistration' is not a valid AuthorizationState

Process finished with exit code 1

Code:

from telegram.client import Telegram

tg = Telegram(
    api_id=11,
    api_hash='11',
    phone='+11',
    database_encryption_key='11',

    use_test_dc=True,
    use_message_database=False,
    login=True
)

def main():
    code = '11'
    password = '11'

    state = tg.login(blocking=False)

    if state == AuthorizationState.WAIT_CODE:
        # Telegram expects a pin code
        tg.send_code(code)
        state = tg.login(blocking=False)  # continue the login process

    if state == AuthorizationState.WAIT_PASSWORD:
        tg.send_password(password)
        state = tg.login(blocking=False)  # continue the login process

# TODO I guess here the missing state should be handled

    # if this is the first run, library needs to preload all chats
    # otherwise the message will not be sent
    result = tg.get_chats()
    # `tdlib` is asynchronous, so `python-telegram` always returns you an `AsyncResult` object.
    # You can wait for a result with the blocking `wait` method.
    result.wait()

    if result.error:
        print(f'get chats error: {result.error_info}')
    else:
        print(f'chats: {result.update}')

    result = tg.send_message(
        chat_id=11,
        text="A test message",
    )

    result.wait()
    if result.error:
        print(f'send message error: {result.error_info}')
    else:
        print(f'message has been sent: {result.update}')

    tg.stop()

main()
alexander-akhmetov commented 3 years ago

Hi, yes, it's not supported right now. I see in tdlib docs that registerUser finishes user registration, but currently the library does not know about authorizationStateWaitRegistration at all, it should be supported first.