diff options
| author | levlam <levlam@telegram.org> | 2025-04-12 22:57:26 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2025-04-12 22:57:26 +0300 |
| commit | 6271c889a3cb44665c8b2dcbc4dc99f7ed421ece (patch) | |
| tree | ba0e6b4d5e44c366f6e108d7d9d94c33efc141cc | |
| parent | a7e4cb437784fefee861501b5776688c4bba0038 (diff) | |
Minor improvements.
| -rw-r--r-- | example/python/tdjson_example.py | 68 | ||||
| -rw-r--r-- | td/telegram/AuthManager.cpp | 3 | ||||
| -rw-r--r-- | td/telegram/cli.cpp | 2 | ||||
| -rw-r--r-- | tde2e/td/e2e/Call.cpp | 2 |
4 files changed, 40 insertions, 35 deletions
diff --git a/example/python/tdjson_example.py b/example/python/tdjson_example.py index cb9e694f8..db304e39a 100644 --- a/example/python/tdjson_example.py +++ b/example/python/tdjson_example.py @@ -18,7 +18,7 @@ class TdExample: def __init__(self, api_id: int = None, api_hash: str = None): """Initialize a Telegram client. - + Args: api_id: Telegram API ID (get from https://my.telegram.org) api_hash: Telegram API hash (get from https://my.telegram.org) @@ -38,7 +38,7 @@ class TdExample: tdjson_path = os.path.join(os.path.dirname(__file__), 'tdjson.dll') else: sys.exit("Error: Can't find 'tdjson' library. Make sure it's installed correctly.") - + try: self.tdjson = CDLL(tdjson_path) except Exception as e: @@ -74,7 +74,7 @@ class TdExample: def _setup_logging(self, verbosity_level: int = 1) -> None: """Configure TDLib logging. - + Args: verbosity_level: 0-fatal, 1-errors, 2-warnings, 3+-debug """ @@ -82,16 +82,16 @@ class TdExample: def on_log_message_callback(verbosity_level, message): if verbosity_level == 0: sys.exit(f'TDLib fatal error: {message.decode("utf-8")}') - + self._td_set_log_message_callback(2, on_log_message_callback) self.execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': verbosity_level}) def execute(self, query: Dict[str, Any]) -> Optional[Dict[str, Any]]: """Execute a synchronous TDLib request. - + Args: query: The request to execute - + Returns: Response from TDLib or None """ @@ -103,7 +103,7 @@ class TdExample: def send(self, query: Dict[str, Any]) -> None: """Send an asynchronous request to TDLib. - + Args: query: The request to send """ @@ -112,10 +112,10 @@ class TdExample: def receive(self, timeout: float = 1.0) -> Optional[Dict[str, Any]]: """Receive a response or update from TDLib. - + Args: timeout: Maximum number of seconds to wait - + Returns: An update or response from TDLib, or None if nothing received """ @@ -127,42 +127,42 @@ class TdExample: def login(self) -> None: """Start the authentication process.""" self.send({'@type': 'getOption', 'name': 'version'}) - + print("Starting Telegram authentication flow...") print("Press Ctrl+C to cancel at any time.") - + try: self._handle_authentication() except KeyboardInterrupt: - print("\nAuthentication cancelled by user.") + print("\nAuthentication canceled by user.") sys.exit(0) - + def _handle_authentication(self) -> None: """Handle the TDLib authentication flow.""" while True: event = self.receive() if not event: continue - + # Print all updates for debugging if event.get('@type') != 'updateAuthorizationState': - print(f"Received: {json.dumps(event, indent=2)}") - + print(f"Receive: {json.dumps(event, indent=2)}") + # Process authorization states if event.get('@type') == 'updateAuthorizationState': auth_state = event['authorization_state'] auth_type = auth_state.get('@type') - + if auth_type == 'authorizationStateClosed': print("Authorization state closed.") break - + elif auth_type == 'authorizationStateWaitTdlibParameters': if not self.api_id or not self.api_hash: print("\nYou MUST obtain your own api_id and api_hash at https://my.telegram.org") self.api_id = int(input("Please enter your API ID: ")) self.api_hash = input("Please enter your API hash: ") - + print("Setting TDLib parameters...") self.send({ '@type': 'setTdlibParameters', @@ -175,35 +175,35 @@ class TdExample: 'device_model': 'Python TDLib Client', 'application_version': '1.1', }) - + elif auth_type == 'authorizationStateWaitPhoneNumber': phone_number = input('Please enter your phone number (international format): ') self.send({'@type': 'setAuthenticationPhoneNumber', 'phone_number': phone_number}) - + elif auth_type == 'authorizationStateWaitEmailAddress': email_address = input('Please enter your email address: ') self.send({'@type': 'setAuthenticationEmailAddress', 'email_address': email_address}) - + elif auth_type == 'authorizationStateWaitEmailCode': code = input('Please enter the email authentication code you received: ') self.send({ '@type': 'checkAuthenticationEmailCode', 'code': {'@type': 'emailAddressAuthenticationCode', 'code': code} }) - + elif auth_type == 'authorizationStateWaitCode': code = input('Please enter the authentication code you received: ') self.send({'@type': 'checkAuthenticationCode', 'code': code}) - + elif auth_type == 'authorizationStateWaitRegistration': first_name = input('Please enter your first name: ') last_name = input('Please enter your last name: ') self.send({'@type': 'registerUser', 'first_name': first_name, 'last_name': last_name}) - + elif auth_type == 'authorizationStateWaitPassword': password = input('Please enter your password: ') self.send({'@type': 'checkAuthenticationPassword', 'password': password}) - + elif auth_type == 'authorizationStateReady': print("Authorization complete! You are now logged in.") return @@ -213,32 +213,32 @@ def main(): """Main function to demonstrate client usage.""" # Example API credentials - DO NOT USE THESE # Get your own from https://my.telegram.org - DEFAULT_API_ID = 94575 + DEFAULT_API_ID = 94575 DEFAULT_API_HASH = "a3406de8d171bb422bb6ddf3bbd800e2" - + print("TDLib Python Client") print("===================") print("IMPORTANT: You should obtain your own api_id and api_hash at https://my.telegram.org") print(" The default values are for demonstration only.\n") - + use_default = input("Use default API credentials for testing? (y/n): ").lower() == 'y' - + if use_default: client = TdExample(DEFAULT_API_ID, DEFAULT_API_HASH) else: client = TdExample() - + # Test execute method print("\nTesting TDLib execute method...") result = client.execute({ - '@type': 'getTextEntities', + '@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me' }) print(f"Text entities: {json.dumps(result, indent=2)}") - + # Start login process client.login() - + # Main event loop print("\nEntering main event loop. Press Ctrl+C to exit.") try: diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index f5a5243c5..10886ccef 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -39,6 +39,7 @@ #include "td/telegram/Version.h" #include "td/utils/base64.h" +#include "td/utils/buffer.h" #include "td/utils/format.h" #include "td/utils/JsonBuilder.h" #include "td/utils/logging.h" @@ -49,6 +50,8 @@ #include "td/utils/Time.h" #include "td/utils/tl_helpers.h" +#include <type_traits> + namespace td { struct AuthManager::DbState { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 978290b77..c3bf95cf3 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -6949,7 +6949,7 @@ class CliClient final : public Actor { } else if (op == "spp" || op == "spppf") { InputChatPhoto input_chat_photo; get_args(args, input_chat_photo); - send_request(td_api::make_object<td_api::setProfilePhoto>(input_chat_photo, op == "sppf")); + send_request(td_api::make_object<td_api::setProfilePhoto>(input_chat_photo, op == "spppf")); } else if (op == "suppp") { UserId user_id; InputChatPhoto input_chat_photo; diff --git a/tde2e/td/e2e/Call.cpp b/tde2e/td/e2e/Call.cpp index dca0e3459..806a01891 100644 --- a/tde2e/td/e2e/Call.cpp +++ b/tde2e/td/e2e/Call.cpp @@ -16,6 +16,7 @@ #include "td/utils/as.h" #include "td/utils/common.h" #include "td/utils/crypto.h" +#include "td/utils/FlatHashSet.h" #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/overloaded.h" @@ -27,6 +28,7 @@ #include <algorithm> #include <limits> #include <memory> +#include <mutex> #include <tuple> #include <utility> |
