## Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com),# Pellegrino Prevete (pellegrinoprevete@gmail.com) 2014-2020## 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_libraryfrom ctypes import *import jsonimport sys# load shared librarytdjson_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 librarytd_json_client_create = tdjson.td_json_client_createtd_json_client_create.restype = c_void_ptd_json_client_create.argtypes = []td_json_client_receive = tdjson.td_json_client_receivetd_json_client_receive.restype = c_char_ptd_json_client_receive.argtypes = [c_void_p, c_double]td_json_client_send = tdjson.td_json_client_sendtd_json_client_send.restype = Nonetd_json_client_send.argtypes = [c_void_p, c_char_p]td_json_client_execute = tdjson.td_json_client_executetd_json_client_execute.restype = c_char_ptd_json_client_execute.argtypes = [c_void_p, c_char_p]td_json_client_destroy = tdjson.td_json_client_destroytd_json_client_destroy.restype = Nonetd_json_client_destroy.argtypes = [c_void_p]fatal_error_callback_type = CFUNCTYPE(None, c_char_p)td_set_log_fatal_error_callback = tdjson.td_set_log_fatal_error_callbacktd_set_log_fatal_error_callback.restype = Nonetd_set_log_fatal_error_callback.argtypes = [fatal_error_callback_type]# initialize TDLib log with desired parametersdef on_fatal_error_callback(error_message):print('TDLib fatal error: ', error_message)def td_execute(query):query = json.dumps(query).encode('utf-8')result = td_json_client_execute(None, query)if result:result = json.loads(result.decode('utf-8'))return resultc_on_fatal_error_callback = fatal_error_callback_type(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 clientclient = td_json_client_create()# simple wrappers for client usagedef 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# another test for TDLib execute methodprint(td_execute({'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0]}))# testing TDLib send methodtd_send({'@type': 'getAuthorizationState', '@extra': 1.01234})# main events cyclewhile True:event = td_receive()if event:# process authorization statesif event['@type'] == 'updateAuthorizationState':auth_state = event['authorization_state']# if client is closed, we need to destroy it and create new clientif 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 callif auth_state['@type'] == 'authorizationStateWaitTdlibParameters':td_send({'@type': 'setTdlibParameters', 'parameters': {'database_directory': 'tdlib','use_message_database': True,'use_secret_chats': True,'api_id': 94575,'api_hash': 'a3406de8d171bb422bb6ddf3bbd800e2','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 databaseif auth_state['@type'] == 'authorizationStateWaitEncryptionKey':td_send({'@type': 'checkDatabaseEncryptionKey', 'key': 'my_key'})# enter phone number to log inif auth_state['@type'] == 'authorizationStateWaitPhoneNumber':phone_number = input('Please enter your phone number: ')td_send({'@type': 'setAuthenticationPhoneNumber', 'phone_number': phone_number})# wait for authorization codeif 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 usersif 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 presentif 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 requestprint(event)sys.stdout.flush()# destroy client when it is closed and isn't needed anymoretd_json_client_destroy(client)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。