pytglib package

Subpackages

Submodules

pytglib.client module

class pytglib.client.Telegram(library_path, api_id: int, api_hash: str, database_encryption_key: str, phone: str = None, bot_token: str = None, worker: Optional[Type[pytglib.worker.BaseWorker]] = None, files_directory: str = None, use_test_dc: bool = False, use_message_database: bool = True, device_model: str = 'python-telegram', application_version: str = '0.4.2', system_version: str = 'unknown', system_language_code: str = 'en', login: bool = False, default_workers_queue_size=1000, tdlib_verbosity: int = 2)

Bases: object

add_delete_messages_handler(func: Callable) → None

Adds function to handle deleted messages Args:

func (Callable):
Deleted messages handler function
add_message_handler(func: Callable) → None

Adds function to handle all incoming messages Args:

func (Callable):
Message handler function
add_raw_update_handler(func: Callable) → None

Adds function to handle all incoming updates Args:

func (Callable):
Update handler function
add_update_handler(handler_type: str, func: Callable) → None

Adds function to handle custom type of updates Args:

handler_type (str):
Update type name For example updateNewMessage
func (Callable):
Handler function
call_method(method_name: str, params: Optional[Dict[str, Any]] = None)
execute(data: pytglib.api.utils.object.Object) → pytglib.api.utils.object.Object
idle(stop_signals=(<Signals.SIGINT: 2>, <Signals.SIGTERM: 15>, <Signals.SIGABRT: 6>))

Blocks until one of the signals are received and stops

login()

Login process (blocking)

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

send(data: pytglib.api.utils.object.Object, result_id: str = None) → pytglib.utils.AsyncResult
stop() → None

Stops the client

pytglib.functions module

class pytglib.functions.Function(client)

Bases: object

add_contact(phone_number, first_name, last_name, vcard='', user_id=0)

Adds new contact or edits existing contact; contact’s user identifiers are ignored

phone_number (str):
Phone number of the user
first_name (str):
First name of the user; 1-255 characters in length
last_name (str):
Last name of the user
vcard (str):
Additional data about the user in a form of vCard; 0-2048 bytes in length
user_id (int):
Identifier of the user, if known; otherwise 0
Returns:
AsyncResult

Checks the validity of an invite link for a chat and returns information about the corresponding chat

Args:
invite_link (str):
Invite link to import. Should begin with “https://t.me/joinchat/”, “https://pytglib.me/joinchat/”, or “https://pytglib.dog/joinchat/
Returns:
AsyncResult
delete_messages(chat_id, message_ids, revoke=True)

Deletes messages

Args:
chat_id (int):
Chat identifier
message_ids (List of int):
Identifiers of the messages to be deleted
revoke (bool):
Pass true to try to delete outgoing messages for all chat members (may fail if messages are too old) Always true for supergroups, channels and secret chats
Returns:
AsyncResult
edit_message_text(chat_id, message_id, text, markup=None, parse_mode=None)

Edits the text of a message (or a text of a game message). Returns the edited message after the edit is completed on the server side

Args:
chat_id (int):
The chat the message belongs to
message_id (int):
Identifier of the message
text (str):
New message text
markup (pytglib.api.types.ReplyMarkup):
The new message reply markup; for bots only
parse_mode (str):
Text parse mode. Markdown, HTML or None
Returns:
AsyncResult
forward_messages(chat_id, from_chat_id, message_ids, disable_notification=False, from_background=False, as_album=False)
Forwards previously sent messages.
Returns the forwarded messages in the same order as the message identifiers passed in message_ids.

If a message can’t be forwarded, null will be returned instead of the message

Args:
chat_id (int):
Identifier of the chat to which to forward messages
from_chat_id (int):
Identifier of the chat from which to forward messages
message_ids (List of int):
Identifiers of the messages to forward
disable_notification (bool):
Pass true to disable notification for the message,
doesn’t work if messages are forwarded to a secret chat
from_background (bool):
Pass true if the message is sent from the background
as_album (bool):

True, if the messages should be grouped into an album after forwarding For this to work, no more than 10 messages may be forwarded,

and all of them must be photo or video messages
Returns:
AsyncResult
get_chat(chat_id: int)

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.

Args:
chat_id (int):
Target chat id
Returns:
AsyncResult
get_chat_history(chat_id: int, limit: int = 1000, from_message_id: int = 0, offset: int = 0, only_local: bool = False)
Returns messages in a chat. The messages are returned in a reverse chronological order
(i.e., in order of decreasing message_id).
For optimal performance the number of returned messages is chosen by the library.
This is an offline request if only_local is true
Args:
chat_id (int):
Chat identifier
from_message_id (int):
Identifier of the message starting from which history must be fetched;
use 0 to get results from the last message
offset (int):
Specify 0 to get results from exactly the from_message_id or a negative offset to get the specified message and some newer messages
limit (int):
The maximum number of messages to be returned; must be positive and can’t be greater than 100 If the offset is negative, the limit must be greater than -offsetFewer messages may be returned than specified by the limit, Even if the end of the message history has not been reached
only_local (bool):
If true, returns only messages that are available locally without sending network requests
Returns:
AsyncResult
get_chat_members(chat_id, offset=0, limit=200, filter=<pytglib.api.types.supergroup_members_filter_recent.SupergroupMembersFilterRecent object>)

Returns information about members or banned users in a supergroup or channel. Can be used only if SupergroupFullInfo.can_get_members == true; Additionally, administrator privileges may be required for some filters

Args:
chat_id (int):
Identifier of the supergroup or channel
filter (pytglib.api.types.SupergroupMembersFilter):
The type of users to return By default, supergroupMembersRecent
offset (int):
Number of users to skip
limit (int):
The maximum number of users be returned; up to 200
Returns:
AsyncResult
get_chats(chat_list: pytglib.api.types.chat_list.ChatList, offset_order: int = 9223372036854775807, offset_chat_id: int = 0, limit: int = 10000)

Returns an ordered list of chats. Chats are sorted newest to oldest. Don’t send the values and it’ll return full chats.

chat_list (pytglib.api.types.ChatList):
ChatListMain or ChatListArchive
offset_order (int):
Chat order to return chats from
offset_chat_id (int):
Chat identifier to return chats from
limit (int):
The maximum number of chats to be returned It is possible that fewer chats than the limit are returned even if the end of the list is not reached
Returns:
AsyncResult
get_contacts()

Returns all user contacts

Returns:
AsyncResult
get_me()

Requests information of the current user (getMe method)

Returns:
AsyncResult
join_chat(invite_link)

Uses an invite link to add the current user to the chat if possible. The new member will not be added until the chat state has been synchronized with the server

Args:
invite_link (str):
Invite link to import. Should begin with “https://t.me/joinchat/”, “https://pytglib.me/joinchat/”, or “https://pytglib.dog/joinchat/
Returns:
AsyncResult
leave_chat(chat_id)

Removes current user from chat members. Private and secret chats can’t be left using this method

Args:
chat_id (int):
Chat identifier
Returns:
AsyncResult
press_inline_button(chat_id: int, message_id: int, button_data: str)

Presses an Inline Button in the chat.

Args:
chat_id (int):
Target chat
message_id (int):
Message which the button is attached to
button_data (str):
Button data
Returns:
AsyncResult
send_chat_action(chat_id, action=<pytglib.api.types.chat_action_typing.ChatActionTyping object>)

Sends a notification about user activity in a chat

Args:
chat_id (int):
Chat identifier
action (pytglib.api.types.ChatAction):
The action description (Default: Typing)
Returns:
AsyncResult
send_document(chat_id: int, document: str, caption=None, reply_to_message_id=0, disable_notification=False, from_background=False, markup=None, parse_mode=None)

Sends a document 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 (int):
Target chat
document (str):
Document path in system
caption (str):
Document caption
reply_to_message_id (int):
Identifier of the message to reply to or 0
disable_notification (bool):
Pass true to disable notification for the message Not supported in secret chats
from_background (bool):
Pass true if the message is sent from the background
markup (pytglib.api.types.ReplyMarkup):
Markup for replying to the message; for bots only
parse_mode (str):
Caption parse mode MarkDown, HTML or None
Returns:
AsyncResult
send_inline_query_result(chat_id, query_id, result_id, reply_to_message_id=0, disable_notification=False, from_background=False)

Sends the result of an inline query as a message. Returns the sent message. Always clears a chat draft message

Args:
chat_id (int):
Target chat
query_id (int):
Identifier of the inline query
result_id (str):
Identifier of the inline result
reply_to_message_id (int):
Identifier of a message to reply to or 0
disable_notification (bool):
Pass true to disable notification for the messageNot supported in secret chats
from_background (bool):
Pass true if the message is sent from background
Returns:
AsyncResult
send_message(chat_id: int, text: str, reply_to_message_id=0, disable_notification=False, from_background=False, scheduling_state=False, markup=None, parse_mode=None, disable_web_page_preview=False, clear_draft=True)

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 (int):
Target chat
text (str):
Text to send
reply_to_message_id (int):
Identifier of the message to reply to or 0
disable_notification (bool):
Pass true to disable notification for the message Not supported in secret chats
from_background (bool):
Pass true if the message is sent from the background
scheduling_state (pytglib.api.types.MessageSchedulingState):
Pass a MessageSchedulingState if you want message to be scheduled
markup (pytglib.api.types.ReplyMarkup):
Markup for replying to the message; for bots only
parse_mode (str):
Text parse mode MarkDown, HTML or None
disable_web_page_preview (bool):
True, if rich web page previews for URLs in the message text should be disabled
clear_draft (bool):
True, if a chat message draft should be deleted
Returns:
AsyncResult
send_photo(chat_id: int, photo: str, caption='', reply_to_message_id=0, disable_notification=False, from_background=False, scheduling_state=False, markup=None, parse_mode=None)

Sends a photo 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 (int):
Target chat
photo (str):
Path of photo to send
caption (str):
Caption of message to send
reply_to_message_id (int):
Identifier of the message to reply to or 0
disable_notification (bool):
Pass true to disable notification for the message Not supported in secret chats
from_background (bool):
Pass true if the message is sent from the background
scheduling_state (pytglib.api.types.MessageSchedulingState):
Pass a MessageSchedulingState if you want message to be scheduled
markup (pytglib.api.types.ReplyMarkup):
Markup for replying to the message; for bots only
parse_mode (str):
Text parse mode MarkDown, HTML or None
Returns:
AsyncResult
view_messages(chat_id, latest_message_id)

Marks the incomming messages as “READ”

Returns:
AsyncResult

pytglib.tdjson module

class pytglib.tdjson.TDJson(library_path, verbosity: int = 2)

Bases: object

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

pytglib.utils module

class pytglib.utils.AsyncResult(client: Telegram, result_id=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) → None
wait(timeout: int = None, raise_exc: bool = False) → None

Blocking method to wait for the result

pytglib.worker module

class pytglib.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
class pytglib.worker.SimpleWorker(queue: queue.Queue)

Bases: pytglib.worker.BaseWorker

Simple one-thread worker

run() → None

Module contents