Add Python example README.md and add more documentation.

GitOrigin-RevId: 624fa5c37431dc3fabe6e451355c5b52ce12e4ab
This commit is contained in:
levlam 2018-03-02 03:34:02 +03:00
parent 376f582803
commit 017ae5865d
5 changed files with 31 additions and 0 deletions

View File

@ -18,3 +18,5 @@ cd build
cmake -DCMAKE_BUILD_TYPE=Release -DTd_DIR=<full path to TDLib sources>/example/cpp/td/lib/cmake/Td ..
cmake --build .
```
Documentation for all available classes and methods can be found at https://core.telegram.org/tdlib/docs.

View File

@ -37,3 +37,5 @@ Resulting library for iOS will work on any architecture (arv7, armv7s, arm64) an
We use [CMake/iOS.cmake](https://github.com/tdlib/td/blob/master/CMake/iOS.cmake) toolchain, other toolchains may work too.
Built libraries will be store in `tdjson` directory.
Documentation for all available classes and methods can be found at https://core.telegram.org/tdlib/docs.

11
example/python/README.md Normal file
View File

@ -0,0 +1,11 @@
# TDLib Python example
First 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.

View File

@ -3,12 +3,14 @@ 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 = []
@ -47,6 +49,7 @@ 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)
@ -54,8 +57,10 @@ 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)
@ -73,15 +78,23 @@ def td_execute(query):
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)

View File

@ -10,3 +10,6 @@ cmake --build . --target install
```
Then you can open and build the example with the latest Xcode.
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.