summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/example/python
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2022-11-30 17:48:47 +0300
committerGeorge Hazan <ghazan@miranda.im>2022-11-30 17:48:47 +0300
commit0ece30dc7c0e34b4c5911969b8fa99c33c6d023c (patch)
tree671325d3fec09b999411e4e3ab84ef8259261818 /protocols/Telegram/tdlib/td/example/python
parent46c53ffc6809c67e4607e99951a2846c382b63b2 (diff)
Telegram: update for TDLIB
Diffstat (limited to 'protocols/Telegram/tdlib/td/example/python')
-rw-r--r--protocols/Telegram/tdlib/td/example/python/README.md4
-rw-r--r--protocols/Telegram/tdlib/td/example/python/tdjson_example.py164
2 files changed, 102 insertions, 66 deletions
diff --git a/protocols/Telegram/tdlib/td/example/python/README.md b/protocols/Telegram/tdlib/td/example/python/README.md
index c4a4ad3768..c3c171d906 100644
--- a/protocols/Telegram/tdlib/td/example/python/README.md
+++ b/protocols/Telegram/tdlib/td/example/python/README.md
@@ -7,5 +7,5 @@ Then you can run the example:
python tdjson_example.py
```
-Description of all available classes and methods can be found at [td_json_client](https://core.telegram.org/tdlib/docs/td__json__client_8h.html),
-[td_log](https://core.telegram.org/tdlib/docs/td__log_8h.html) and [td_api](https://core.telegram.org/tdlib/docs/td__api_8h.html) documentation.
+Description of all available classes and methods can be found at [td_json_client](https://core.telegram.org/tdlib/docs/td__json__client_8h.html)
+and [td_api](https://core.telegram.org/tdlib/docs/td__api_8h.html) documentation.
diff --git a/protocols/Telegram/tdlib/td/example/python/tdjson_example.py b/protocols/Telegram/tdlib/td/example/python/tdjson_example.py
index d7b2e86fbb..ee8a92a158 100644
--- a/protocols/Telegram/tdlib/td/example/python/tdjson_example.py
+++ b/protocols/Telegram/tdlib/td/example/python/tdjson_example.py
@@ -1,106 +1,142 @@
-//
-// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
-//
-// Distributed under the Boost Software License, Version 1.0. (See accompanying
-// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
-//
+#
+# Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com),
+# Pellegrino Prevete (pellegrinoprevete@gmail.com) 2014-2022
+#
+# Distributed under the Boost Software License, Version 1.0. (See accompanying
+# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+#
from ctypes.util import find_library
from ctypes import *
import json
import sys
# load shared library
-tdjson_path = find_library("tdjson") or "tdjson.dll"
+tdjson_path = find_library('tdjson') or 'tdjson.dll'
if tdjson_path is None:
- print('can\'t find tdjson library')
- quit()
+ sys.exit("Can't find 'tdjson' library")
tdjson = CDLL(tdjson_path)
# load TDLib functions from shared library
-td_json_client_create = tdjson.td_json_client_create
-td_json_client_create.restype = c_void_p
-td_json_client_create.argtypes = []
+_td_create_client_id = tdjson.td_create_client_id
+_td_create_client_id.restype = c_int
+_td_create_client_id.argtypes = []
-td_json_client_receive = tdjson.td_json_client_receive
-td_json_client_receive.restype = c_char_p
-td_json_client_receive.argtypes = [c_void_p, c_double]
+_td_receive = tdjson.td_receive
+_td_receive.restype = c_char_p
+_td_receive.argtypes = [c_double]
-td_json_client_send = tdjson.td_json_client_send
-td_json_client_send.restype = None
-td_json_client_send.argtypes = [c_void_p, c_char_p]
+_td_send = tdjson.td_send
+_td_send.restype = None
+_td_send.argtypes = [c_int, c_char_p]
-td_json_client_execute = tdjson.td_json_client_execute
-td_json_client_execute.restype = c_char_p
-td_json_client_execute.argtypes = [c_void_p, c_char_p]
+_td_execute = tdjson.td_execute
+_td_execute.restype = c_char_p
+_td_execute.argtypes = [c_char_p]
-td_json_client_destroy = tdjson.td_json_client_destroy
-td_json_client_destroy.restype = None
-td_json_client_destroy.argtypes = [c_void_p]
+log_message_callback_type = CFUNCTYPE(None, c_int, c_char_p)
-td_set_log_file_path = tdjson.td_set_log_file_path
-td_set_log_file_path.restype = c_int
-td_set_log_file_path.argtypes = [c_char_p]
+_td_set_log_message_callback = tdjson.td_set_log_message_callback
+_td_set_log_message_callback.restype = None
+_td_set_log_message_callback.argtypes = [c_int, log_message_callback_type]
-td_set_log_max_file_size = tdjson.td_set_log_max_file_size
-td_set_log_max_file_size.restype = None
-td_set_log_max_file_size.argtypes = [c_longlong]
-
-td_set_log_verbosity_level = tdjson.td_set_log_verbosity_level
-td_set_log_verbosity_level.restype = None
-td_set_log_verbosity_level.argtypes = [c_int]
+# initialize TDLib log with desired parameters
+@log_message_callback_type
+def on_log_message_callback(verbosity_level, message):
+ if verbosity_level == 0:
+ sys.exit('TDLib fatal error: %r' % message)
-fatal_error_callback_type = CFUNCTYPE(None, c_char_p)
+def td_execute(query):
+ query = json.dumps(query).encode('utf-8')
+ result = _td_execute(query)
+ if result:
+ result = json.loads(result.decode('utf-8'))
+ return result
-td_set_log_fatal_error_callback = tdjson.td_set_log_fatal_error_callback
-td_set_log_fatal_error_callback.restype = None
-td_set_log_fatal_error_callback.argtypes = [fatal_error_callback_type]
+_td_set_log_message_callback(2, on_log_message_callback)
-# initialize TDLib log with desired parameters
-def on_fatal_error_callback(error_message):
- print('TDLib fatal error: ', error_message)
+# setting TDLib log verbosity level to 1 (errors)
+print(str(td_execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': 1, '@extra': 1.01234})).encode('utf-8'))
-td_set_log_verbosity_level(2)
-c_on_fatal_error_callback = fatal_error_callback_type(on_fatal_error_callback)
-td_set_log_fatal_error_callback(c_on_fatal_error_callback)
# create client
-client = td_json_client_create()
+client_id = _td_create_client_id()
# simple wrappers for client usage
def td_send(query):
query = json.dumps(query).encode('utf-8')
- td_json_client_send(client, query)
+ _td_send(client_id, query)
def td_receive():
- result = td_json_client_receive(client, 1.0)
+ result = _td_receive(1.0)
if result:
result = json.loads(result.decode('utf-8'))
return result
-def td_execute(query):
- query = json.dumps(query).encode('utf-8')
- result = td_json_client_execute(client, query)
- if result:
- result = json.loads(result.decode('utf-8'))
- return result
-
-# testing TDLib execute method
-print(td_execute({'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0]}))
+# another test for TDLib execute method
+print(str(td_execute({'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0, 'a']})).encode('utf-8'))
-# testing TDLib send method
+# start the client by sending request to it
td_send({'@type': 'getAuthorizationState', '@extra': 1.01234})
# main events cycle
while True:
event = td_receive()
if event:
- # if client is closed, we need to destroy it and create new client
- if event['@type'] is 'updateAuthorizationState' and event['authorization_state']['@type'] is 'authorizationStateClosed':
- break
+ # process authorization states
+ if event['@type'] == 'updateAuthorizationState':
+ auth_state = event['authorization_state']
+
+ # if client is closed, we need to destroy it and create new client
+ if auth_state['@type'] == 'authorizationStateClosed':
+ break
+
+ # set TDLib parameters
+ # you MUST obtain your own api_id and api_hash at https://my.telegram.org
+ # and use them in the setTdlibParameters call
+ if auth_state['@type'] == 'authorizationStateWaitTdlibParameters':
+ td_send({'@type': 'setTdlibParameters',
+ 'database_directory': 'tdlib',
+ 'use_message_database': True,
+ 'use_secret_chats': True,
+ 'api_id': 94575,
+ 'api_hash': 'a3406de8d171bb422bb6ddf3bbd800e2',
+ 'system_language_code': 'en',
+ 'device_model': 'Desktop',
+ 'application_version': '1.0',
+ 'enable_storage_optimizer': True})
+
+ # enter phone number to log in
+ if auth_state['@type'] == 'authorizationStateWaitPhoneNumber':
+ phone_number = input('Please enter your phone number: ')
+ td_send({'@type': 'setAuthenticationPhoneNumber', 'phone_number': phone_number})
+
+ # enter email address to log in
+ if auth_state['@type'] == 'authorizationStateWaitEmailAddress':
+ email_address = input('Please enter your email address: ')
+ td_send({'@type': 'setAuthenticationEmailAddress', 'email_address': email_address})
+
+ # wait for email authorization code
+ if auth_state['@type'] == 'authorizationStateWaitEmailCode':
+ code = input('Please enter the email authentication code you received: ')
+ td_send({'@type': 'checkAuthenticationEmailCode',
+ 'code': {'@type': 'emailAddressAuthenticationCode', 'code' : 'code'}})
+
+ # wait for authorization code
+ if auth_state['@type'] == 'authorizationStateWaitCode':
+ code = input('Please enter the authentication code you received: ')
+ td_send({'@type': 'checkAuthenticationCode', 'code': code})
+
+ # wait for first and last name for new users
+ if auth_state['@type'] == 'authorizationStateWaitRegistration':
+ first_name = input('Please enter your first name: ')
+ last_name = input('Please enter your last name: ')
+ td_send({'@type': 'registerUser', 'first_name': first_name, 'last_name': last_name})
+
+ # wait for password if present
+ if auth_state['@type'] == 'authorizationStateWaitPassword':
+ password = input('Please enter your password: ')
+ td_send({'@type': 'checkAuthenticationPassword', 'password': password})
# handle an incoming update or an answer to a previously sent request
- print(event)
+ print(str(event).encode('utf-8'))
sys.stdout.flush()
-
-# destroy client when it is closed and isn't needed anymore
-td_json_client_destroy(client)