Use new JSON interface in Python example.

This commit is contained in:
levlam 2020-11-13 21:23:32 +03:00
parent 00abe2f401
commit 0e824976fc

View File

@ -18,31 +18,27 @@ if tdjson_path is None:
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 = tdjson.td_create_client
_td_create_client.restype = c_int
_td_create_client.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_json_client_destroy = tdjson.td_json_client_destroy
td_json_client_destroy.restype = None
td_json_client_destroy.argtypes = [c_void_p]
_td_execute = tdjson.td_execute
_td_execute.restype = c_char_p
_td_execute.argtypes = [c_char_p]
fatal_error_callback_type = CFUNCTYPE(None, c_char_p)
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_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]
# initialize TDLib log with desired parameters
def on_fatal_error_callback(error_message):
@ -50,28 +46,28 @@ def on_fatal_error_callback(error_message):
def td_execute(query):
query = json.dumps(query).encode('utf-8')
result = td_json_client_execute(None, query)
result = _td_execute(query)
if result:
result = json.loads(result.decode('utf-8'))
return result
c_on_fatal_error_callback = fatal_error_callback_type(on_fatal_error_callback)
td_set_log_fatal_error_callback(c_on_fatal_error_callback)
_td_set_log_fatal_error_callback(c_on_fatal_error_callback)
# setting TDLib log verbosity level to 1 (errors)
print(td_execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': 1, '@extra': 1.01234}))
# create client
client = td_json_client_create()
client_id = _td_create_client()
# 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
@ -79,7 +75,7 @@ def td_receive():
# another test for TDLib execute method
print(td_execute({'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0]}))
# testing TDLib send method
# start the client by sending request to it
td_send({'@type': 'getAuthorizationState', '@extra': 1.01234})
# main events cycle
@ -137,6 +133,3 @@ while True:
# handle an incoming update or an answer to a previously sent request
print(event)
sys.stdout.flush()
# destroy client when it is closed and isn't needed anymore
td_json_client_destroy(client)