Skip to content

Commit 971684a

Browse files
AYMENJDlevlam
authored andcommitted
Improve Python example.
1 parent 6e32e56 commit 971684a

File tree

1 file changed

+114
-68
lines changed

1 file changed

+114
-68
lines changed

example/python/tdjson_example.py

Lines changed: 114 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import sys
1111
from ctypes import CDLL, CFUNCTYPE, c_char_p, c_double, c_int
1212
from ctypes.util import find_library
13-
from typing import Any, Dict, Optional, Union
13+
from typing import Any, Dict, Optional
1414

1515

1616
class TdExample:
@@ -32,12 +32,14 @@ def __init__(self, api_id: int = None, api_hash: str = None):
3232

3333
def _load_library(self) -> None:
3434
"""Load the TDLib shared library."""
35-
tdjson_path = find_library('tdjson')
35+
tdjson_path = find_library("tdjson")
3636
if tdjson_path is None:
37-
if os.name == 'nt':
38-
tdjson_path = os.path.join(os.path.dirname(__file__), 'tdjson.dll')
37+
if os.name == "nt":
38+
tdjson_path = os.path.join(os.path.dirname(__file__), "tdjson.dll")
3939
else:
40-
sys.exit("Error: Can't find 'tdjson' library. Make sure it's installed correctly.")
40+
sys.exit(
41+
"Error: Can't find 'tdjson' library. Make sure it's installed correctly."
42+
)
4143

4244
try:
4345
self.tdjson = CDLL(tdjson_path)
@@ -70,21 +72,27 @@ def _setup_functions(self) -> None:
7072
self.log_message_callback_type = CFUNCTYPE(None, c_int, c_char_p)
7173
self._td_set_log_message_callback = self.tdjson.td_set_log_message_callback
7274
self._td_set_log_message_callback.restype = None
73-
self._td_set_log_message_callback.argtypes = [c_int, self.log_message_callback_type]
75+
self._td_set_log_message_callback.argtypes = [
76+
c_int,
77+
self.log_message_callback_type,
78+
]
7479

7580
def _setup_logging(self, verbosity_level: int = 1) -> None:
7681
"""Configure TDLib logging.
7782
7883
Args:
7984
verbosity_level: 0-fatal, 1-errors, 2-warnings, 3+-debug
8085
"""
86+
8187
@self.log_message_callback_type
8288
def on_log_message_callback(verbosity_level, message):
8389
if verbosity_level == 0:
84-
sys.exit(f'TDLib fatal error: {message.decode("utf-8")}')
90+
sys.exit(f"TDLib fatal error: {message.decode('utf-8')}")
8591

8692
self._td_set_log_message_callback(2, on_log_message_callback)
87-
self.execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': verbosity_level})
93+
self.execute(
94+
{"@type": "setLogVerbosityLevel", "new_verbosity_level": verbosity_level}
95+
)
8896

8997
def execute(self, query: Dict[str, Any]) -> Optional[Dict[str, Any]]:
9098
"""Execute a synchronous TDLib request.
@@ -95,10 +103,10 @@ def execute(self, query: Dict[str, Any]) -> Optional[Dict[str, Any]]:
95103
Returns:
96104
Response from TDLib or None
97105
"""
98-
query_json = json.dumps(query).encode('utf-8')
106+
query_json = json.dumps(query).encode("utf-8")
99107
result = self._td_execute(query_json)
100108
if result:
101-
return json.loads(result.decode('utf-8'))
109+
return json.loads(result.decode("utf-8"))
102110
return None
103111

104112
def send(self, query: Dict[str, Any]) -> None:
@@ -107,7 +115,7 @@ def send(self, query: Dict[str, Any]) -> None:
107115
Args:
108116
query: The request to send
109117
"""
110-
query_json = json.dumps(query).encode('utf-8')
118+
query_json = json.dumps(query).encode("utf-8")
111119
self._td_send(self.client_id, query_json)
112120

113121
def receive(self, timeout: float = 1.0) -> Optional[Dict[str, Any]]:
@@ -121,12 +129,12 @@ def receive(self, timeout: float = 1.0) -> Optional[Dict[str, Any]]:
121129
"""
122130
result = self._td_receive(timeout)
123131
if result:
124-
return json.loads(result.decode('utf-8'))
132+
return json.loads(result.decode("utf-8"))
125133
return None
126134

127135
def login(self) -> None:
128136
"""Start the authentication process."""
129-
self.send({'@type': 'getOption', 'name': 'version'})
137+
self.send({"@type": "getOption", "name": "version"})
130138

131139
print("Starting Telegram authentication flow...")
132140
print("Press Ctrl+C to cancel at any time.")
@@ -145,70 +153,102 @@ def _handle_authentication(self) -> None:
145153
continue
146154

147155
# Print all updates for debugging
148-
if event.get('@type') != 'updateAuthorizationState':
156+
event_type = event["@type"]
157+
if event_type != "updateAuthorizationState":
149158
print(f"Receive: {json.dumps(event, indent=2)}")
150159

151160
# Process authorization states
152-
if event.get('@type') == 'updateAuthorizationState':
153-
auth_state = event['authorization_state']
154-
auth_type = auth_state.get('@type')
161+
if event_type == "updateAuthorizationState":
162+
auth_state = event["authorization_state"]
163+
auth_type = auth_state["@type"]
155164

156-
if auth_type == 'authorizationStateClosed':
165+
if auth_type == "authorizationStateClosed":
157166
print("Authorization state closed.")
158167
break
159168

160-
elif auth_type == 'authorizationStateWaitTdlibParameters':
169+
elif auth_type == "authorizationStateWaitTdlibParameters":
161170
if not self.api_id or not self.api_hash:
162-
print("\nYou MUST obtain your own api_id and api_hash at https://my.telegram.org")
171+
print(
172+
"\nYou MUST obtain your own api_id and api_hash at https://my.telegram.org"
173+
)
163174
self.api_id = int(input("Please enter your API ID: "))
164175
self.api_hash = input("Please enter your API hash: ")
165176

166177
print("Setting TDLib parameters...")
167-
self.send({
168-
'@type': 'setTdlibParameters',
169-
'database_directory': 'tdlib_data',
170-
'use_message_database': True,
171-
'use_secret_chats': True,
172-
'api_id': self.api_id,
173-
'api_hash': self.api_hash,
174-
'system_language_code': 'en',
175-
'device_model': 'Python TDLib Client',
176-
'application_version': '1.1',
177-
})
178-
179-
elif auth_type == 'authorizationStateWaitPhoneNumber':
180-
phone_number = input('Please enter your phone number (international format): ')
181-
self.send({'@type': 'setAuthenticationPhoneNumber', 'phone_number': phone_number})
182-
183-
elif auth_type == 'authorizationStateWaitPremiumPurchase':
178+
self.send(
179+
{
180+
"@type": "setTdlibParameters",
181+
"database_directory": "tdlib_data",
182+
"use_message_database": True,
183+
"use_secret_chats": True,
184+
"api_id": self.api_id,
185+
"api_hash": self.api_hash,
186+
"system_language_code": "en",
187+
"device_model": "Python TDLib Client",
188+
"application_version": "1.1",
189+
}
190+
)
191+
192+
elif auth_type == "authorizationStateWaitPhoneNumber":
193+
phone_number = input(
194+
"Please enter your phone number (international format): "
195+
)
196+
self.send(
197+
{
198+
"@type": "setAuthenticationPhoneNumber",
199+
"phone_number": phone_number,
200+
}
201+
)
202+
203+
elif auth_type == "authorizationStateWaitPremiumPurchase":
184204
print("Telegram Premium subscription is required.")
185205
return
186206

187-
elif auth_type == 'authorizationStateWaitEmailAddress':
188-
email_address = input('Please enter your email address: ')
189-
self.send({'@type': 'setAuthenticationEmailAddress', 'email_address': email_address})
190-
191-
elif auth_type == 'authorizationStateWaitEmailCode':
192-
code = input('Please enter the email authentication code you received: ')
193-
self.send({
194-
'@type': 'checkAuthenticationEmailCode',
195-
'code': {'@type': 'emailAddressAuthenticationCode', 'code': code}
196-
})
197-
198-
elif auth_type == 'authorizationStateWaitCode':
199-
code = input('Please enter the authentication code you received: ')
200-
self.send({'@type': 'checkAuthenticationCode', 'code': code})
201-
202-
elif auth_type == 'authorizationStateWaitRegistration':
203-
first_name = input('Please enter your first name: ')
204-
last_name = input('Please enter your last name: ')
205-
self.send({'@type': 'registerUser', 'first_name': first_name, 'last_name': last_name})
206-
207-
elif auth_type == 'authorizationStateWaitPassword':
208-
password = input('Please enter your password: ')
209-
self.send({'@type': 'checkAuthenticationPassword', 'password': password})
210-
211-
elif auth_type == 'authorizationStateReady':
207+
elif auth_type == "authorizationStateWaitEmailAddress":
208+
email_address = input("Please enter your email address: ")
209+
self.send(
210+
{
211+
"@type": "setAuthenticationEmailAddress",
212+
"email_address": email_address,
213+
}
214+
)
215+
216+
elif auth_type == "authorizationStateWaitEmailCode":
217+
code = input(
218+
"Please enter the email authentication code you received: "
219+
)
220+
self.send(
221+
{
222+
"@type": "checkAuthenticationEmailCode",
223+
"code": {
224+
"@type": "emailAddressAuthenticationCode",
225+
"code": code,
226+
},
227+
}
228+
)
229+
230+
elif auth_type == "authorizationStateWaitCode":
231+
code = input("Please enter the authentication code you received: ")
232+
self.send({"@type": "checkAuthenticationCode", "code": code})
233+
234+
elif auth_type == "authorizationStateWaitRegistration":
235+
first_name = input("Please enter your first name: ")
236+
last_name = input("Please enter your last name: ")
237+
self.send(
238+
{
239+
"@type": "registerUser",
240+
"first_name": first_name,
241+
"last_name": last_name,
242+
}
243+
)
244+
245+
elif auth_type == "authorizationStateWaitPassword":
246+
password = input("Please enter your password: ")
247+
self.send(
248+
{"@type": "checkAuthenticationPassword", "password": password}
249+
)
250+
251+
elif auth_type == "authorizationStateReady":
212252
print("Authorization complete! You are now logged in.")
213253
return
214254

@@ -222,10 +262,14 @@ def main():
222262

223263
print("TDLib Python Client")
224264
print("===================")
225-
print("IMPORTANT: You should obtain your own api_id and api_hash at https://my.telegram.org")
265+
print(
266+
"IMPORTANT: You should obtain your own api_id and api_hash at https://my.telegram.org"
267+
)
226268
print(" The default values are for demonstration only.\n")
227269

228-
use_default = input("Use default API credentials for testing? (y/n): ").lower() == 'y'
270+
use_default = (
271+
input("Use default API credentials for testing? (y/n): ").lower() == "y"
272+
)
229273

230274
if use_default:
231275
client = TdExample(DEFAULT_API_ID, DEFAULT_API_HASH)
@@ -234,10 +278,12 @@ def main():
234278

235279
# Test execute method
236280
print("\nTesting TDLib execute method...")
237-
result = client.execute({
238-
'@type': 'getTextEntities',
239-
'text': '@telegram /test_command https://telegram.org telegram.me'
240-
})
281+
result = client.execute(
282+
{
283+
"@type": "getTextEntities",
284+
"text": "@telegram /test_command https://telegram.org telegram.me",
285+
}
286+
)
241287
print(f"Text entities: {json.dumps(result, indent=2)}")
242288

243289
# Start login process

0 commit comments

Comments
 (0)