-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tdlib |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import sys | ||
from tdlib import TDLib | ||
|
||
class TelegramClient: | ||
def __init__(self): | ||
self.tdlib = TDLib() | ||
self.client_id = self.tdlib.create_client_id() | ||
|
||
def send(self, query): | ||
self.tdlib.send(self.client_id, query) | ||
|
||
def receive(self): | ||
return self.tdlib.receive() | ||
|
||
|
||
def main(): | ||
client = TelegramClient() | ||
|
||
# Setting TDLib log verbosity level to 1 (errors) | ||
print(client.tdlib.td_execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': 1, '@extra': 1.01234})) | ||
|
||
# Example request | ||
print(client.tdlib.td_execute( | ||
{'@type': 'getTextEntities', 'text': '@telegram /test_command https://telegram.org telegram.me', | ||
'@extra': ['5', 7.0, 'a']})) | ||
|
||
# Start the client by sending a request to it | ||
client.send({'@type': 'getOption', 'name': 'version', '@extra': 1.01234}) | ||
|
||
while True: | ||
event = client.receive() | ||
if event: | ||
if event['@type'] == 'updateAuthorizationState': | ||
auth_state = event['authorization_state'] | ||
|
||
if auth_state['@type'] == 'authorizationStateClosed': | ||
break | ||
|
||
if auth_state['@type'] == 'authorizationStateWaitTdlibParameters': | ||
client.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'}) | ||
|
||
if auth_state['@type'] == 'authorizationStateWaitPhoneNumber': | ||
phone_number = input('Please enter your phone number: ') | ||
client.send({'@type': 'setAuthenticationPhoneNumber', 'phone_number': phone_number}) | ||
|
||
if auth_state['@type'] == 'authorizationStateWaitEmailAddress': | ||
email_address = input('Please enter your email address: ') | ||
client.send({'@type': 'setAuthenticationEmailAddress', 'email_address': email_address}) | ||
|
||
if auth_state['@type'] == 'authorizationStateWaitEmailCode': | ||
code = input('Please enter the email authentication code you received: ') | ||
client.send({'@type': 'checkAuthenticationEmailCode', | ||
'code': {'@type': 'emailAddressAuthenticationCode', 'code': code}}) | ||
|
||
if auth_state['@type'] == 'authorizationStateWaitCode': | ||
code = input('Please enter the authentication code you received: ') | ||
client.send({'@type': 'checkAuthenticationCode', 'code': code}) | ||
|
||
if auth_state['@type'] == 'authorizationStateWaitRegistration': | ||
first_name = input('Please enter your first name: ') | ||
last_name = input('Please enter your last name: ') | ||
client.send({'@type': 'registerUser', 'first_name': first_name, 'last_name': last_name}) | ||
|
||
if auth_state['@type'] == 'authorizationStateWaitPassword': | ||
password = input('Please enter your password: ') | ||
client.send({'@type': 'checkAuthenticationPassword', 'password': password}) | ||
|
||
print(str(event).encode('utf-8')) | ||
sys.stdout.flush() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from ctypes.util import find_library | ||
from ctypes import * | ||
import json | ||
import os | ||
import sys | ||
import platform | ||
|
||
_log_message_callback_type = CFUNCTYPE(None, c_int, c_char_p) | ||
|
||
class TDLib: | ||
_instance = None | ||
_tdjson_path = None | ||
_tdjson = None | ||
|
||
def __new__(cls): | ||
if cls._instance is None: | ||
cls._instance = super(TDLib, cls).__new__(cls) | ||
cls._instance._load_tdjson() | ||
cls._instance._initialize_tdjson() | ||
cls._instance._set_log_message_callback() | ||
return cls._instance | ||
|
||
def _load_tdjson(self): | ||
if self._tdjson_path is None: | ||
self._tdjson_path = find_library('tdjson') | ||
if self._tdjson_path is None: | ||
if os.name == 'nt': | ||
self._tdjson_path = os.path.join(os.path.dirname(__file__), "natives", platform.system().lower(), "tdjson.dll") | ||
else: | ||
sys.exit("Can't find 'tdjson' library") | ||
self._tdjson = CDLL(self._tdjson_path) | ||
|
||
def _initialize_tdjson(self): | ||
self._td_create_client_id = self._tdjson.td_create_client_id | ||
self._td_create_client_id.restype = c_int | ||
self._td_create_client_id.argtypes = [] | ||
|
||
self._td_receive = self._tdjson.td_receive | ||
self._td_receive.restype = c_char_p | ||
self._td_receive.argtypes = [c_double] | ||
|
||
self._td_send = self._tdjson.td_send | ||
self._td_send.restype = None | ||
self._td_send.argtypes = [c_int, c_char_p] | ||
|
||
self._td_execute = self._tdjson.td_execute | ||
self._td_execute.restype = c_char_p | ||
self._td_execute.argtypes = [c_char_p] | ||
|
||
self._td_set_log_message_callback = self._tdjson.td_set_log_message_callback | ||
self._td_set_log_message_callback.restype = None | ||
self._td_set_log_message_callback.argtypes = [c_int, _log_message_callback_type] | ||
|
||
@_log_message_callback_type | ||
@staticmethod | ||
def _on_log_message_callback(verbosity_level, message): | ||
if verbosity_level == 0: | ||
sys.exit('TDLib fatal error: %r' % message) | ||
|
||
def _set_log_message_callback(self): | ||
self._td_set_log_message_callback(2, self._on_log_message_callback) | ||
|
||
def td_execute(self, query): | ||
query = json.dumps(query).encode('utf-8') | ||
result = self._td_execute(query) | ||
if result: | ||
result = json.loads(result.decode('utf-8')) | ||
return result | ||
|
||
def create_client_id(self): | ||
return self._td_create_client_id() | ||
|
||
def send(self, client_id, query): | ||
query = json.dumps(query).encode('utf-8') | ||
self._td_send(client_id, query) | ||
|
||
def receive(self): | ||
result = self._td_receive(1.0) | ||
if result: | ||
result = json.loads(result.decode('utf-8')) | ||
return result |