pytdbot.client

class pytdbot.client.Client(token=None, api_id=None, api_hash=None, rabbitmq_url=None, instance_id=None, lib_path=None, plugins=None, default_parse_mode=None, system_language_code='en', device_model=None, files_directory=None, database_encryption_key=None, use_test_dc=False, use_file_database=True, use_chat_info_database=True, use_message_database=True, loop=None, options=None, workers=5, queue_size=1000, default_handlers_timeout=None, no_updates=False, load_messages_before_reply=False, td_verbosity=2, td_log=None, user_bot=False, server_ack=True)[source]

Bases: Decorators, Methods

Pytdbot, a TDLib client

Parameters:
  • token (str, optional) – Bot token

  • api_id (int, optional) – Identifier for Telegram API access, which can be obtained at https://my.telegram.org

  • api_hash (str, optional) – Identifier hash for Telegram API access, which can be obtained at https://my.telegram.org

  • rabbitmq_url (str, optional) – URL for RabbitMQ server connection

  • instance_id (str, optional) – Instance ID for RabbitMQ connections and queues. Default is None (random)

  • lib_path (str, optional) – Path to TDLib library. Default is None (auto-detect)

  • plugins (Plugins, optional) – Plugins to load

  • default_parse_mode (str, optional) – The default parse_mode for methods: sendTextMessage(), sendPhoto(), sendAudio(), sendVideo(), sendDocument(), sendAnimation(), sendVoice(), sendCopy(), editTextMessage(); Default is None (Don't parse) Supported values: markdown, markdownv2, html

  • system_language_code (str, optional) – System language code. Default is en

  • device_model (str, optional) – Device model. Default is None (auto-detect)

  • files_directory (str, optional) – Directory for storing files and database

  • database_encryption_key (str | bytes) – Encryption key for database encryption

  • use_test_dc (bool, optional) – If set to true, the Telegram test environment will be used instead of the production environment. Default is False

  • use_file_database (bool, optional) – If set to true, information about downloaded and uploaded files will be saved between application restarts. Default is True

  • use_chat_info_database (bool, optional) – If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use_file_database. Default is True

  • use_message_database (bool, optional) – If set to true, the library will maintain a cache of chats and messages. Implies use_chat_info_database. Default is True

  • loop (asyncio.AbstractEventLoop, optional) – Event loop. Default is None (auto-detect)

  • options (dict, optional) – Pass key-value dictionary to set TDLib options. Check the list of available options at https://core.telegram.org/tdlib/options

  • workers (int, optional) – Number of workers to handle updates. Default is 5. If set to None, updates will be immediately handled instead of being queued, which can impact performance.

  • default_handlers_timeout (float, optional) – Default timeout for handlers. If set, each handler will be awaited with this timeout (ignored if timeout is set when registering handler). Default is None (no timeout)

  • no_updates (bool, optional) – Whether the client should handle updates or not. Applicable only when using [TDLib Server](https://github.com/pytdbot/tdlib-server). Default is False

  • td_verbosity (int, optional) – Verbosity level of TDLib. Default is 2

  • td_log (LogStream, optional) – Log stream. Default is None (Log to stdout)

  • user_bot (bool, optional) – Pass True if this is a user-bot. Default is False

  • queue_size (int)

  • load_messages_before_reply (bool)

  • server_ack (bool)

__init__(token=None, api_id=None, api_hash=None, rabbitmq_url=None, instance_id=None, lib_path=None, plugins=None, default_parse_mode=None, system_language_code='en', device_model=None, files_directory=None, database_encryption_key=None, use_test_dc=False, use_file_database=True, use_chat_info_database=True, use_message_database=True, loop=None, options=None, workers=5, queue_size=1000, default_handlers_timeout=None, no_updates=False, load_messages_before_reply=False, td_verbosity=2, td_log=None, user_bot=False, server_ack=True)[source]
Parameters:
  • token (str | None)

  • api_id (int | None)

  • api_hash (str | None)

  • rabbitmq_url (str | None)

  • instance_id (str | None)

  • lib_path (str | None)

  • plugins (Plugins | None)

  • default_parse_mode (str | None)

  • system_language_code (str)

  • device_model (str | None)

  • files_directory (str | None)

  • database_encryption_key (str | bytes | None)

  • use_test_dc (bool)

  • use_file_database (bool)

  • use_chat_info_database (bool)

  • use_message_database (bool)

  • loop (AbstractEventLoop | None)

  • options (dict | None)

  • workers (int)

  • queue_size (int)

  • default_handlers_timeout (float | None)

  • no_updates (bool)

  • load_messages_before_reply (bool)

  • td_verbosity (int)

  • td_log (LogStream | None)

  • user_bot (bool)

  • server_ack (bool)

Return type:

None

add_handler(update_type, func, filters=None, position=None, inner_object=False, timeout=None, is_from_plugin=False)[source]

Add an update handler

Parameters:
  • update_type (str || Update) – An update type

  • func (Callable) – A callable function

  • filters (Filter, optional) – message filter

  • position (int, optional) – The function position in handlers list. Default is None (append)

  • inner_object (bool, optional) – Wether to pass an inner object of update or not; for example UpdateNewMessage.message. Default is False

  • timeout (float, optional) – Max execution time for the handler before it timeout. If None, Client.default_handlers_timeout is preferred. Default is None

  • is_from_plugin (bool, optional) – Wether this handler is from a loaded plugin (this can help reloading plugin during runtime; for development only). Default is False

Raises:

TypeError

Return type:

None

async call_method(method, **kwargs)[source]

Call a method. with keyword arguments (kwargs) support

Example

from pytdbot import Client

async with Client(...) as client:
    res = await client.call_method("getOption", name="version"})
    if not isinstance(res, types.Error):
        print(res)
Parameters:

method (str) – TDLib method name

Return type:

TlObject

Returns:

Any TlObject

async cancelScheduledEvent(event_id)[source]

Cancel a scheduled event

Parameters:

event_id (str) – Event ID to cancel

Return type:

Union[Ok, Error]

async getServerStats()[source]

Returns TDLib Server stats

Return type:

Union[ServerStats, Error]

get_inner_object(update)[source]
Parameters:

update (TlObject)

async idle()[source]

Idle and wait until the client is stopped.

async invoke(request)[source]

Invoke a new TDLib request

Example

from pytdbot import Client

async with Client(...) as client:
    res = await client.invoke({"@type": "getOption", "name": "version"})
    if not isinstance(res, types.Error):
        print(res)
Parameters:

request (dict) – The request to be sent

Return type:

TlObject

Returns:

Result

is_coro_filter(func)[source]
Return type:

bool

Parameters:

func (Callable)

async process_update(update)[source]
reload_plugins()[source]

Reload all plugins, non-plugin handlers are not reloaded .. note:

This is for ``development purposes only`` and should not be used
in production environments
remove_handler(func)[source]

Remove an update handler

Parameters:

func (Callable) – A callable function

Raises:

TypeError

Returns:

True if handler was removed, False otherwise

Return type:

bool

run()[source]

Start the client and block until the client is stopped

Return type:

None

Example

from pytdbot import Client

client = Client(...)

@client.on_updateNewMessage()
async def new_message(c,update):
    await update.reply_text('Hello!')

client.run()
async scheduleEvent(name, payload, send_at)[source]

Schedule an event

Parameters:
  • name (str) – Event name

  • payload (str) – The event payload to be scheduled

  • send_at (int) – Unix timestamp when the event should be sent

Return type:

Union[ScheduledEvent, Error]

async set_td_parameters()[source]

Make a call to setTdlibParameters() with the current client init parameters

Raises:

AuthorizationError

async start()[source]

Start pytdbot client

Return type:

None

async stop()[source]

Stop the client

Raises:

RuntimeError – If the instance is already stopped

Returns:

True on success

Return type:

bool

property authorization_state: str

Current authorization state

pytdbot.client.deepdiff(self, d1, d2)[source]