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),
|
2022-12-31 22:28:08 +01:00
|
|
|
# Pellegrino Prevete (pellegrinoprevete@gmail.com) 2014-2023
|
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:
|
2021-11-18 12:39:02 +01:00
|
|
|
sys.exit("Can't find 'tdjson' library")
|
2018-12-31 20:04:05 +01:00
|
|
|
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
|
|
|
|
2021-05-18 14:49:21 +02:00
|
|
|
log_message_callback_type = CFUNCTYPE(None, c_int, c_char_p)
|
2018-01-24 17:11:39 +01:00
|
|
|
|
2021-05-18 14:49:21 +02:00
|
|
|
_td_set_log_message_callback = tdjson.td_set_log_message_callback
|
|
|
|
_td_set_log_message_callback.restype = None
|
|
|
|
_td_set_log_message_callback.argtypes = [c_int, log_message_callback_type]
|
2018-01-24 17:11:39 +01:00
|
|
|
|
2018-03-02 01:34:02 +01:00
|
|
|
# initialize TDLib log with desired parameters
|
2021-12-28 02:01:04 +01:00
|
|
|
@log_message_callback_type
|
2021-05-18 14:49:21 +02:00
|
|
|
def on_log_message_callback(verbosity_level, message):
|
|
|
|
if verbosity_level == 0:
|
2021-11-18 12:39:02 +01:00
|
|
|
sys.exit('TDLib fatal error: %r' % message)
|
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
|
|
|
|
|
2021-12-28 02:01:04 +01:00
|
|
|
_td_set_log_message_callback(2, on_log_message_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
|
2021-12-28 02:06:45 +01:00
|
|
|
print(str(td_execute({'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', '@extra': ['5', 7.0, 'a']})).encode('utf-8'))
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-01-12 20:25:47 +01:00
|
|
|
# start the client by sending a request to it
|
|
|
|
td_send({'@type': 'getOption', 'name': 'version', '@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':
|
2022-09-07 21:19:08 +02:00
|
|
|
td_send({'@type': 'setTdlibParameters',
|
|
|
|
'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-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
|
|
|
|
2022-09-08 12:11:08 +02:00
|
|
|
# enter email address to log in
|
|
|
|
if auth_state['@type'] == 'authorizationStateWaitEmailAddress':
|
|
|
|
email_address = input('Please enter your email address: ')
|
|
|
|
td_send({'@type': 'setAuthenticationEmailAddress', 'email_address': email_address})
|
|
|
|
|
|
|
|
# wait for email authorization code
|
|
|
|
if auth_state['@type'] == 'authorizationStateWaitEmailCode':
|
|
|
|
code = input('Please enter the email authentication code you received: ')
|
|
|
|
td_send({'@type': 'checkAuthenticationEmailCode',
|
2023-01-25 05:06:14 +01:00
|
|
|
'code': {'@type': 'emailAddressAuthenticationCode', 'code' : code}})
|
2022-09-08 12:11:08 +02:00
|
|
|
|
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()
|