2018-04-28 01:09:43 +02:00
|
|
|
#
|
2018-09-11 21:12:00 +02:00
|
|
|
# Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com),
|
|
|
|
# Pellegrino Prevete (pellegrinoprevete@gmail.com) 2014-2018
|
2018-04-28 01:09:43 +02:00
|
|
|
#
|
|
|
|
# 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)
|
|
|
|
#
|
2018-12-31 20:04:05 +01:00
|
|
|
from ctypes.util import find_library
|
|
|
|
from ctypes import *
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# load shared library
|
2018-12-31 20:04:05 +01:00
|
|
|
tdjson_path = find_library("tdjson") or "tdjson.dll"
|
|
|
|
if tdjson_path is None:
|
|
|
|
print('can\'t find tdjson library')
|
|
|
|
quit()
|
|
|
|
tdjson = CDLL(tdjson_path)
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# load TDLib functions from shared library
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
2018-01-28 17:38:59 +01:00
|
|
|
td_set_log_file_path.restype = c_int
|
2018-12-31 20:04:05 +01:00
|
|
|
td_set_log_file_path.argtypes = [c_char_p]
|
|
|
|
|
2018-01-09 19:59:40 +01:00
|
|
|
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]
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
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]
|
|
|
|
|
2018-01-24 17:11:39 +01:00
|
|
|
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]
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# initialize TDLib log with desired parameters
|
2018-01-24 17:11:39 +01:00
|
|
|
def on_fatal_error_callback(error_message):
|
|
|
|
print('TDLib fatal error: ', error_message)
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
td_set_log_verbosity_level(2)
|
2018-01-24 17:11:39 +01:00
|
|
|
c_on_fatal_error_callback = fatal_error_callback_type(on_fatal_error_callback)
|
|
|
|
td_set_log_fatal_error_callback(c_on_fatal_error_callback)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# create client
|
2018-12-31 20:04:05 +01:00
|
|
|
client = td_json_client_create()
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# simple wrappers for client usage
|
2018-12-31 20:04:05 +01:00
|
|
|
def td_send(query):
|
|
|
|
query = json.dumps(query).encode('utf-8')
|
|
|
|
td_json_client_send(client, query)
|
|
|
|
|
|
|
|
def td_receive():
|
2018-01-24 17:11:39 +01:00
|
|
|
result = td_json_client_receive(client, 1.0)
|
2018-12-31 20:04:05 +01:00
|
|
|
if result:
|
|
|
|
result = json.loads(result.decode('utf-8'))
|
|
|
|
return result
|
|
|
|
|
|
|
|
def td_execute(query):
|
|
|
|
query = json.dumps(query).encode('utf-8')
|
2018-10-31 15:51:07 +01:00
|
|
|
result = td_json_client_execute(None, query)
|
2018-12-31 20:04:05 +01:00
|
|
|
if result:
|
|
|
|
result = json.loads(result.decode('utf-8'))
|
|
|
|
return result
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# testing TDLib execute method
|
2018-12-31 20:04:05 +01:00
|
|
|
print(td_execute({'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0]}))
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# testing TDLib send method
|
2018-12-31 20:04:05 +01:00
|
|
|
td_send({'@type': 'getAuthorizationState', '@extra': 1.01234})
|
2018-03-02 01:34:02 +01:00
|
|
|
|
|
|
|
# main events cycle
|
2018-12-31 20:04:05 +01:00
|
|
|
while True:
|
|
|
|
event = td_receive()
|
|
|
|
if event:
|
2018-09-11 21:12:00 +02:00
|
|
|
# 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':
|
2018-12-31 20:04:05 +01:00
|
|
|
break
|
2018-03-02 01:34:02 +01:00
|
|
|
|
2018-09-11 21:12:00 +02:00
|
|
|
# set tdlib parameters
|
|
|
|
if auth_state['@type'] == 'authorizationStateWaitTdlibParameters':
|
|
|
|
td_send({'@type':"setTdlibParameters", "parameters":{
|
|
|
|
"database_directory":"tdlib",
|
|
|
|
"use_message_database":True,
|
|
|
|
"use_secret_chats":True,
|
|
|
|
"api_id":"your_api_id",
|
|
|
|
"api_hash":"your_api_hash",
|
|
|
|
"system_language_code":"en",
|
|
|
|
"device_model":"Desktop",
|
|
|
|
"system_version":"Linux",
|
|
|
|
"application_version":"1.0",
|
|
|
|
"enable_storage_optimizer":True}})
|
|
|
|
|
|
|
|
# set an encryption key for database to let know tdlib how to open the database
|
|
|
|
if auth_state['@type'] == 'authorizationStateWaitEncryptionKey':
|
|
|
|
td_send({"@type":"checkDatabaseEncryptionKey", "key":"my_key"})
|
|
|
|
|
|
|
|
# insert phone number for login
|
|
|
|
if auth_state['@type'] == "authorizationStateWaitPhoneNumber":
|
|
|
|
phone_number = input("Please insert your phone number: ")
|
|
|
|
td_send({"@type":"setAuthenticationPhoneNumber", "phone_number":phone_number})
|
|
|
|
|
|
|
|
# wait for authorization code
|
|
|
|
if auth_state['@type'] == 'authorizationStateWaitCode':
|
|
|
|
code = input("Please insert the authentication code you received: ")
|
|
|
|
td_send({"@type":"checkAuthenticationCode", "code":code})
|
|
|
|
|
|
|
|
# wait for password if present
|
|
|
|
if auth_state['@type'] == "authorizationStateWaitPassword":
|
|
|
|
password = input("Please insert your password: ")
|
|
|
|
td_send({"@type":"checkAuthenticationPassword", "password":password})
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# handle an incoming update or an answer to a previously sent request
|
2018-12-31 20:04:05 +01:00
|
|
|
print(event)
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# destroy client when it is closed and isn't needed anymore
|
2018-12-31 20:04:05 +01:00
|
|
|
td_json_client_destroy(client)
|