telegram package

Submodules

telegram.client module

class telegram.client.AuthorizationState

Bases: enum.Enum

An enumeration.

CLOSED = 'authorizationStateClosed'
CLOSING = 'authorizationStateClosing'
NONE = None
READY = 'authorizationStateReady'
WAIT_CODE = 'authorizationStateWaitCode'
WAIT_ENCRYPTION_KEY = 'authorizationStateWaitEncryptionKey'
WAIT_PASSWORD = 'authorizationStateWaitPassword'
WAIT_PHONE_NUMBER = 'authorizationStateWaitPhoneNumber'
WAIT_TDLIB_PARAMETERS = 'authorizationStateWaitTdlibParameters'
class telegram.client.Telegram(api_id: int, api_hash: str, database_encryption_key: Union[str, bytes], phone: Optional[str] = None, bot_token: Optional[str] = None, library_path: Optional[str] = None, worker: Optional[Type[telegram.worker.BaseWorker]] = None, files_directory: Optional[str] = None, use_test_dc: bool = False, use_message_database: bool = True, device_model: str = 'python-telegram', application_version: str = '0.13.0', system_version: str = 'unknown', system_language_code: str = 'en', login: bool = False, default_workers_queue_size: int = 1000, tdlib_verbosity: int = 2, proxy_server: str = '', proxy_port: int = 0, proxy_type: Optional[Dict[str, str]] = None, use_secret_chats: bool = True)

Bases: object

add_message_handler(func: Callable) → None
add_update_handler(handler_type: str, func: Callable) → None
call_method(method_name: str, params: Optional[Dict[str, Any]] = None, block: bool = False) → telegram.utils.AsyncResult

Use this method to call any other method of the tdlib

Args:
method_name: Name of the method params: parameters
create_basic_group_chat(basic_group_id: int) → telegram.utils.AsyncResult

Create a chat from a basic group

Args:
basic_group_id
delete_messages(chat_id: int, message_ids: List[int], revoke: bool = True) → telegram.utils.AsyncResult

Delete a list of messages in a chat

Args:
chat_id message_ids revoke
get_authorization_state() → telegram.utils.AsyncResult
get_chat(chat_id: int) → telegram.utils.AsyncResult

This is offline request, if there is no chat in your database it will not be found tdlib saves chat to the database when it receives a new message or when you call get_chats method.

get_chat_history(chat_id: int, limit: int = 1000, from_message_id: int = 0, offset: int = 0, only_local: bool = False) → telegram.utils.AsyncResult

Returns history of a chat

Args:
chat_id limit from_message_id offset only_local
get_chats(offset_order: int = 0, offset_chat_id: int = 0, limit: int = 100) → telegram.utils.AsyncResult

Returns a list of chats:

Returns:
{

@type’: ‘chats’, ‘chat_ids’: […], @extra’: {

‘request_id’: ‘…’

}

}

get_me() → telegram.utils.AsyncResult

Requests information of the current user (getMe method)

https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1get_me.html

get_message(chat_id: int, message_id: int) → telegram.utils.AsyncResult

Return a message via its message_id

Args:
chat_id message_id
Returns:

AsyncResult The update will be:

{
@type’: ‘message’, ‘id’: 1, ‘sender_user_id’: 2, ‘chat_id’: 3, ‘content’: {…}, …

}

get_supergroup_full_info(supergroup_id: int) → telegram.utils.AsyncResult

Get the full info of a supergroup

Args:
supergroup_id
get_user(user_id: int) → telegram.utils.AsyncResult

Requests information about a user with id = user_id.

https://core.telegram.org/tdlib/docs/classtd_1_1td__api_1_1get_user.html

get_web_page_instant_view(url: str, force_full: bool = False) → telegram.utils.AsyncResult

Use this method to request instant preview of a webpage. Returns error with 404 if there is no preview for this webpage.

Args:
url: URL of a webpage force_full: If true, the full instant view for the web page will be returned
idle(stop_signals: Tuple = (<Signals.SIGINT: 2>, <Signals.SIGTERM: 15>, <Signals.SIGABRT: 6>, <Signals.SIGQUIT: 3>)) → None

Blocks until one of the exit signals is received. When a signal is received, calls stop.

login(blocking: bool = True) → telegram.client.AuthorizationState

Login process.

Must be called before any other call. It sends initial params to the tdlib, sets database encryption key, etc.

args:
blocking [bool]: If True, the process is blocking and the client
expects password and code from stdin. If False, login call returns next AuthorizationState and the login process can be continued (with calling login(blocking=False) again) after the necessary action is completed.
Returns:
  • AuthorizationState.WAIT_CODE if a telegram code is required. The caller should ask the telegram code to the end user then call send_code(code)
  • AuthorizationState.WAIT_PASSWORD if a telegram password is required. The caller should ask the telegram password to the end user and then call send_password(password)
  • AuthorizationState.READY if the login process scceeded.
remove_update_handler(handler_type: str, func: Callable) → None

Remove a handler with the specified type

send_code(code: str) → telegram.client.AuthorizationState

Verifies a telegram code and continues the authorization process

Args:
code: the code to be verified. If code is None, it will be asked to the user using the input() function
Returns
  • AuthorizationState. The called have to call login to continue the login process.
Raises:
  • RuntimeError if the login failed
send_message(chat_id: int, text: str) → telegram.utils.AsyncResult

Sends a message to a chat. The chat must be in the tdlib’s database. If there is no chat in the DB, tdlib returns an error. Chat is being saved to the database when the client receives a message or when you call the get_chats method.

Args:
chat_id text
Returns:

AsyncResult The update will be:

{
@type’: ‘message’, ‘id’: 1, ‘sender_user_id’: 2, ‘chat_id’: 3, …

}

send_password(password: str) → telegram.client.AuthorizationState

Verifies a telegram password and continues the authorization process

Args:
password the password to be verified. If password is None, it will be asked to the user using the getpass.getpass() function
Returns
  • AuthorizationState. The called have to call login to continue the login process.
Raises:
  • RuntimeError if the login failed
stop() → None

Stops the client

telegram.tdjson module

class telegram.tdjson.TDJson(library_path: Optional[str] = None, verbosity: int = 2)

Bases: object

receive() → Union[None, Dict[Any, Any]]
send(query: Dict[Any, Any]) → None
stop() → None
td_execute(query: Dict[Any, Any]) → Union[Dict[Any, Any], Any]

telegram.utils module

class telegram.utils.AsyncResult(client: Telegram, result_id: Optional[str] = None)

Bases: object

tdlib is asynchronous, and this class helps you get results back. After each API call, you receive AsyncResult object, which you can use to get results back.

parse_update(update: Dict[Any, Any]) → bool
wait(timeout: Optional[int] = None, raise_exc: bool = False) → None

Blocking method to wait for the result

telegram.worker module

class telegram.worker.BaseWorker(queue: queue.Queue)

Bases: object

Base worker class. Each worker must implement the run method to start listening to the queue and calling handler functions

run() → None
stop() → None
class telegram.worker.SimpleWorker(queue: queue.Queue)

Bases: telegram.worker.BaseWorker

Simple one-thread worker

run() → None
stop() → None

Module contents