summaryrefslogtreecommitdiff
path: root/protocols/Telegram/tdlib/td/example/python
diff options
context:
space:
mode:
authoraunsane <aunsane@gmail.com>2018-04-27 21:33:17 +0300
committeraunsane <aunsane@gmail.com>2018-04-27 21:33:17 +0300
commite1ec72eab6d00b3ba38e5932bc88920f103b6e4a (patch)
tree999de2725a83e30fbbf6576200525d4ef0c5fe38 /protocols/Telegram/tdlib/td/example/python
parentb9ce1d4d98525490ca1a38e2d9fd4f3369adb3e0 (diff)
Telegram: initial commit
- tdlib moved to telegram dir
Diffstat (limited to 'protocols/Telegram/tdlib/td/example/python')
-rw-r--r--protocols/Telegram/tdlib/td/example/python/README.md11
-rw-r--r--protocols/Telegram/tdlib/td/example/python/tdjson_example.py106
2 files changed, 117 insertions, 0 deletions
diff --git a/protocols/Telegram/tdlib/td/example/python/README.md b/protocols/Telegram/tdlib/td/example/python/README.md
new file mode 100644
index 0000000000..c4a4ad3768
--- /dev/null
+++ b/protocols/Telegram/tdlib/td/example/python/README.md
@@ -0,0 +1,11 @@
+# TDLib Python example
+
+To run this example you need to [build](https://github.com/tdlib/td#building) TDLib and copy built tdjson shared library to this directory.
+
+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.
diff --git a/protocols/Telegram/tdlib/td/example/python/tdjson_example.py b/protocols/Telegram/tdlib/td/example/python/tdjson_example.py
new file mode 100644
index 0000000000..d7b2e86fbb
--- /dev/null
+++ b/protocols/Telegram/tdlib/td/example/python/tdjson_example.py
@@ -0,0 +1,106 @@
+//
+// 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)
+//
+from ctypes.util import find_library
+from ctypes import *
+import json
+import sys
+
+# load shared library
+tdjson_path = find_library("tdjson") or "tdjson.dll"
+if tdjson_path is None:
+ print('can\'t find tdjson library')
+ quit()
+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_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_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_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_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_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]
+
+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]
+
+# initialize TDLib log with desired parameters
+def on_fatal_error_callback(error_message):
+ print('TDLib fatal error: ', error_message)
+
+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()
+
+# simple wrappers for client usage
+def td_send(query):
+ query = json.dumps(query).encode('utf-8')
+ td_json_client_send(client, query)
+
+def td_receive():
+ result = td_json_client_receive(client, 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]}))
+
+# testing TDLib send method
+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
+
+ # 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)