Skip to content

Commit

Permalink
Support tdlib > 1.8.5 #348 (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
JleMyP authored Mar 12, 2023
1 parent f121d25 commit 75f99fa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
45 changes: 24 additions & 21 deletions telegram/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def __init__(
raise ValueError('You must provide bot_token or phone')

self._database_encryption_key = database_encryption_key
if isinstance(self._database_encryption_key, str):
self._database_encryption_key = self._database_encryption_key.encode()

self._database_encryption_key = base64.b64encode(self._database_encryption_key).decode()

if not files_directory:
hasher = hashlib.md5()
Expand Down Expand Up @@ -723,37 +727,36 @@ def _set_initial_params(self) -> AsyncResult:
self.files_directory,
self.use_test_dc,
)
data = {
# todo: params

parameters = {
'use_test_dc': self.use_test_dc,
'api_id': self.api_id,
'api_hash': self.api_hash,
'device_model': self.device_model,
'system_version': self.system_version,
'application_version': self.application_version,
'system_language_code': self.system_language_code,
'database_directory': os.path.join(self.files_directory, 'database'),
'use_message_database': self.use_message_database,
'files_directory': os.path.join(self.files_directory, 'files'),
'use_secret_chats': self.use_secret_chats,
}
data: Dict[str, typing.Any] = {
'@type': 'setTdlibParameters',
'parameters': {
'use_test_dc': self.use_test_dc,
'api_id': self.api_id,
'api_hash': self.api_hash,
'device_model': self.device_model,
'system_version': self.system_version,
'application_version': self.application_version,
'system_language_code': self.system_language_code,
'database_directory': os.path.join(self.files_directory, 'database'),
'use_message_database': self.use_message_database,
'files_directory': os.path.join(self.files_directory, 'files'),
'use_secret_chats': self.use_secret_chats,
},
'parameters': parameters,
# since tdlib 1.8.6
'database_encryption_key': self._database_encryption_key,
**parameters,
}

return self._send_data(data, result_id='updateAuthorizationState')

def _send_encryption_key(self) -> AsyncResult:
logger.info('Sending encryption key')

key = self._database_encryption_key

if isinstance(key, str):
key = key.encode()

data = {
'@type': 'checkDatabaseEncryptionKey',
'encryption_key': base64.b64encode(key).decode(),
'encryption_key': self._database_encryption_key,
}

return self._send_data(data, result_id='updateAuthorizationState')
Expand Down
32 changes: 18 additions & 14 deletions tests/test_telegram_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,25 +311,29 @@ def test_get_chat_history(self, telegram):

telegram._tdjson.send.assert_called_once_with(exp_data)

def test_set_initial_params(self, telegram):
def test_set_initial_params(self):
telegram = _get_telegram_instance(database_encryption_key='key')
async_result = telegram._set_initial_params()
phone_md5 = '69560384b84c896952ef20352fbce705'

parameters = {
'use_test_dc': False,
'api_id': API_ID,
'api_hash': API_HASH,
'device_model': 'python-telegram',
'system_version': 'unknown',
'application_version': VERSION,
'system_language_code': 'en',
'database_directory': f'/tmp/.tdlib_files/{phone_md5}/database',
'use_message_database': True,
'files_directory': f'/tmp/.tdlib_files/{phone_md5}/files',
'use_secret_chats': True,
}
exp_data = {
'@type': 'setTdlibParameters',
'parameters': {
'use_test_dc': False,
'api_id': API_ID,
'api_hash': API_HASH,
'device_model': 'python-telegram',
'system_version': 'unknown',
'application_version': VERSION,
'system_language_code': 'en',
'database_directory': f'/tmp/.tdlib_files/{phone_md5}/database',
'use_message_database': True,
'files_directory': f'/tmp/.tdlib_files/{phone_md5}/files',
'use_secret_chats': True,
},
'parameters': parameters,
**parameters,
'database_encryption_key': 'a2V5',
'@extra': {'request_id': 'updateAuthorizationState'},
}

Expand Down

0 comments on commit 75f99fa

Please sign in to comment.