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),
|
2020-01-01 02:23:48 +01:00
|
|
|
# Pellegrino Prevete (pellegrinoprevete@gmail.com) 2014-2020
|
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
|
2019-04-30 19:27:31 +02:00
|
|
|
tdjson_path = find_library('tdjson') or 'tdjson.dll'
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
2020-11-14 23:13:11 +01:00
|
|
|
_td_create_client_id = tdjson.td_create_client_id
|
|
|
|
_td_create_client_id.restype = c_int
|
|
|
|
_td_create_client_id.argtypes = []
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-11-13 19:23:32 +01:00
|
|
|
_td_receive = tdjson.td_receive
|
|
|
|
_td_receive.restype = c_char_p
|
|
|
|
_td_receive.argtypes = [c_double]
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-11-13 19:23:32 +01:00
|
|
|
_td_send = tdjson.td_send
|
|
|
|
_td_send.restype = None
|
|
|
|
_td_send.argtypes = [c_int, c_char_p]
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-11-13 19:23:32 +01:00
|
|
|
_td_execute = tdjson.td_execute
|
|
|
|
_td_execute.restype = c_char_p
|
|
|
|
_td_execute.argtypes = [c_char_p]
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-01-24 17:11:39 +01:00
|
|
|
fatal_error_callback_type = CFUNCTYPE(None, c_char_p)
|
|
|
|
|
2020-11-13 19:23:32 +01:00
|
|
|
_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-01-24 17:11:39 +01:00
|
|
|
|
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):
|
2020-11-14 22:41:29 +01:00
|
|
|
print('TDLib fatal error: ', error_message)
|
|
|
|
sys.stdout.flush()
|
2018-01-24 17:11:39 +01:00
|
|
|
|
2019-04-30 21:51:17 +02:00
|
|
|
def td_execute(query):
|
|
|
|
query = json.dumps(query).encode('utf-8')
|
2020-11-13 19:23:32 +01:00
|
|
|
result = _td_execute(query)
|
2019-04-30 21:51:17 +02:00
|
|
|
if result:
|
|
|
|
result = json.loads(result.decode('utf-8'))
|
|
|
|
return result
|
|
|
|
|
2018-01-24 17:11:39 +01:00
|
|
|
c_on_fatal_error_callback = fatal_error_callback_type(on_fatal_error_callback)
|
2020-11-13 19:23:32 +01:00
|
|
|
_td_set_log_fatal_error_callback(c_on_fatal_error_callback)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-04-30 21:51:17 +02:00
|
|
|
# setting TDLib log verbosity level to 1 (errors)
|
2020-11-13 21:49:33 +01:00
|
|
|
print(str(td_execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': 1, '@extra': 1.01234})).encode('utf-8'))
|
2019-04-30 21:51:17 +02:00
|
|
|
|
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# create client
|
2020-11-14 23:13:11 +01:00
|
|
|
client_id = _td_create_client_id()
|
2018-12-31 20:04:05 +01:00
|
|
|
|
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')
|
2020-11-13 19:23:32 +01:00
|
|
|
_td_send(client_id, query)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
def td_receive():
|
2020-11-13 19:23:32 +01:00
|
|
|
result = _td_receive(1.0)
|
2018-12-31 20:04:05 +01:00
|
|
|
if result:
|
|
|
|
result = json.loads(result.decode('utf-8'))
|
|
|
|
return result
|
|
|
|
|
2019-04-30 21:51:17 +02:00
|
|
|
# another test for TDLib execute method
|
2020-11-13 21:49:33 +01:00
|
|
|
print(str(td_execute({'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0, 'ä']})).encode('utf-8'))
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-11-13 19:23:32 +01:00
|
|
|
# start the client by sending request to it
|
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
|
2019-04-30 19:27:31 +02:00
|
|
|
if event['@type'] == 'updateAuthorizationState':
|
2018-09-11 21:12:00 +02:00
|
|
|
auth_state = event['authorization_state']
|
2019-04-30 19:27:31 +02:00
|
|
|
|
2018-09-11 21:12:00 +02:00
|
|
|
# if client is closed, we need to destroy it and create new client
|
|
|
|
if auth_state['@type'] == 'authorizationStateClosed':
|
2019-04-30 19:27:31 +02:00
|
|
|
break
|
2018-03-02 01:34:02 +01:00
|
|
|
|
2019-05-14 16:26:13 +02:00
|
|
|
# set TDLib parameters
|
2019-04-30 19:27:31 +02:00
|
|
|
# you MUST obtain your own api_id and api_hash at https://my.telegram.org
|
|
|
|
# and use them in the setTdlibParameters call
|
2018-09-11 21:12:00 +02:00
|
|
|
if auth_state['@type'] == 'authorizationStateWaitTdlibParameters':
|
2019-04-30 21:51:17 +02:00
|
|
|
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',
|
|
|
|
'application_version': '1.0',
|
|
|
|
'enable_storage_optimizer': True}})
|
2018-09-11 21:12:00 +02:00
|
|
|
|
2019-05-14 16:26:13 +02:00
|
|
|
# set an encryption key for database to let know TDLib how to open the database
|
2018-09-11 21:12:00 +02:00
|
|
|
if auth_state['@type'] == 'authorizationStateWaitEncryptionKey':
|
2020-03-24 22:59:38 +01:00
|
|
|
td_send({'@type': 'checkDatabaseEncryptionKey', 'encryption_key': ''})
|
2018-09-11 21:12:00 +02:00
|
|
|
|
2019-05-23 02:46:02 +02:00
|
|
|
# enter phone number to log in
|
2019-04-30 19:27:31 +02:00
|
|
|
if auth_state['@type'] == 'authorizationStateWaitPhoneNumber':
|
2019-05-23 02:46:02 +02:00
|
|
|
phone_number = input('Please enter your phone number: ')
|
2019-04-30 21:51:17 +02:00
|
|
|
td_send({'@type': 'setAuthenticationPhoneNumber', 'phone_number': phone_number})
|
2018-09-11 21:12:00 +02:00
|
|
|
|
|
|
|
# wait for authorization code
|
|
|
|
if auth_state['@type'] == 'authorizationStateWaitCode':
|
2019-05-23 02:46:02 +02:00
|
|
|
code = input('Please enter the authentication code you received: ')
|
2019-04-30 21:51:17 +02:00
|
|
|
td_send({'@type': 'checkAuthenticationCode', 'code': code})
|
2018-09-11 21:12:00 +02:00
|
|
|
|
2019-07-16 21:08:34 +02:00
|
|
|
# wait for first and last name for new users
|
|
|
|
if 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})
|
|
|
|
|
2018-09-11 21:12:00 +02:00
|
|
|
# wait for password if present
|
2019-04-30 19:27:31 +02:00
|
|
|
if auth_state['@type'] == 'authorizationStateWaitPassword':
|
2019-05-23 02:46:02 +02:00
|
|
|
password = input('Please enter your password: ')
|
2019-04-30 21:51:17 +02:00
|
|
|
td_send({'@type': 'checkAuthenticationPassword', 'password': password})
|
2018-09-11 21:12:00 +02:00
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# handle an incoming update or an answer to a previously sent request
|
2020-11-13 21:49:33 +01:00
|
|
|
print(str(event).encode('utf-8'))
|
2018-12-31 20:04:05 +01:00
|
|
|
sys.stdout.flush()
|