diff --git a/telegram/client.py b/telegram/client.py index d558d154..53eac70f 100644 --- a/telegram/client.py +++ b/telegram/client.py @@ -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() @@ -723,22 +727,26 @@ 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') @@ -746,14 +754,9 @@ def _set_initial_params(self) -> AsyncResult: 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') diff --git a/tests/test_telegram_methods.py b/tests/test_telegram_methods.py index 606c7c6b..3271bd69 100644 --- a/tests/test_telegram_methods.py +++ b/tests/test_telegram_methods.py @@ -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'}, }