diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index 79f7e8a0..a850da7f 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -4,6 +4,8 @@ Changelog [unreleased] +- **Incompatible** default path for files is changed. Now the library uses an md5 hash of the phone number or bot token instead of just a phone number. + It should not be noticeable for most cases, but if you rely on locally saved files or database, you need to pass the ``files_directory`` parameter to the ``telegram.client.Telegram``. - Fixed problem with randomly raised "Database encryption key is needed" errors during login process. (#12) - Fixed `stop` method execution. (#8) - Added ``examples/bot_login.py`` example. diff --git a/telegram/client.py b/telegram/client.py index 2a43d2c9..0794692e 100644 --- a/telegram/client.py +++ b/telegram/client.py @@ -1,4 +1,5 @@ import os +import hashlib import time import queue import signal @@ -75,7 +76,13 @@ def __init__( self._database_encryption_key = database_encryption_key if not files_directory: - files_directory = f'/tmp/.tdlib_files/{self.phone}/' + hasher = hashlib.md5() + hasher.update( + (self.phone or self.bot_token).encode('utf-8') # type: ignore + ) + directory_name = hasher.hexdigest() + files_directory = f'/tmp/.tdlib_files/{directory_name}/' + self.files_directory = files_directory self._authorized = False diff --git a/tests/test_telegram_methods.py b/tests/test_telegram_methods.py index 3ba85d0b..ada7e9e3 100644 --- a/tests/test_telegram_methods.py +++ b/tests/test_telegram_methods.py @@ -236,6 +236,7 @@ def test_get_chat_history(self, telegram): def test_set_initial_params(self, telegram): async_result = telegram._set_initial_params() + phone_md5 = '69560384b84c896952ef20352fbce705' exp_data = { '@type': 'setTdlibParameters', @@ -247,9 +248,9 @@ def test_set_initial_params(self, telegram): 'system_version': 'unknown', 'application_version': VERSION, 'system_language_code': 'en', - 'database_directory': f'/tmp/.tdlib_files/{PHONE}/database', + 'database_directory': f'/tmp/.tdlib_files/{phone_md5}/database', 'use_message_database': True, - 'files_directory': f'/tmp/.tdlib_files/{PHONE}/files', + 'files_directory': f'/tmp/.tdlib_files/{phone_md5}/files', }, '@extra': {'request_id': 'updateAuthorizationState'}, }