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 tokenapi_id (
int
, optional) – Identifier for Telegram API access, which can be obtained at https://my.telegram.orgapi_hash (
str
, optional) – Identifier hash for Telegram API access, which can be obtained at https://my.telegram.orgrabbitmq_url (
str
, optional) – URL for RabbitMQ server connectioninstance_id (
str
, optional) – Instance ID for RabbitMQ connections and queues. Default isNone
(random)lib_path (
str
, optional) – Path to TDLib library. Default isNone
(auto-detect)plugins (
Plugins
, optional) – Plugins to loaddefault_parse_mode (
str
, optional) – The defaultparse_mode
for methods:sendTextMessage()
,sendPhoto()
,sendAudio()
,sendVideo()
,sendDocument()
,sendAnimation()
,sendVoice()
,sendCopy()
,editTextMessage()
; Default isNone
(Don't parse) Supported values:markdown
,markdownv2
,html
system_language_code (
str
, optional) – System language code. Default isen
device_model (
str
, optional) – Device model. Default isNone
(auto-detect)files_directory (
str
, optional) – Directory for storing files and databasedatabase_encryption_key (
str
|bytes
) – Encryption key for database encryptionuse_test_dc (
bool
, optional) – If set to true, the Telegram test environment will be used instead of the production environment. Default isFalse
use_file_database (
bool
, optional) – If set to true, information about downloaded and uploaded files will be saved between application restarts. Default isTrue
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. Impliesuse_file_database
. Default isTrue
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 isTrue
loop (
asyncio.AbstractEventLoop
, optional) – Event loop. Default isNone
(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/optionsworkers (
int
, optional) – Number of workers to handle updates. Default is5
. If set toNone
, 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 iftimeout
is set when registering handler). Default isNone
(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 isFalse
td_verbosity (
int
, optional) – Verbosity level of TDLib. Default is2
td_log (
LogStream
, optional) – Log stream. Default isNone
(Log tostdout
)user_bot (
bool
, optional) – PassTrue
if this is a user-bot. Default isFalse
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 typefunc (
Callable
) – A callable functionfilters (
Filter
, optional) – message filterposition (
int
, optional) – The function position in handlers list. Default isNone
(append)inner_object (
bool
, optional) – Wether to pass an inner object of update or not; for exampleUpdateNewMessage.message
. Default isFalse
timeout (
float
, optional) – Max execution time for the handler before it timeout. IfNone
,Client.default_handlers_timeout
is preferred. Default isNone
is_from_plugin (
bool
, optional) – Wether this handler is from a loaded plugin (this can help reloading plugin during runtime; for development only). Default isFalse
- Raises:
TypeError –
- Return type:
None
- async call_method(method, **kwargs)[source]
Call a method. with keyword arguments (
kwargs
) supportExample
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)
- async getServerStats()[source]
Returns TDLib Server stats
- Return type:
Union
[ServerStats
,Error
]
- 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:
- Returns:
Result
- 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 namepayload (
str
) – The event payload to be scheduledsend_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:
- 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