telegram.TelegramObject

class telegram.TelegramObject(*args, **kwargs)[source]

Bases: object

Base class for most Telegram objects.

Objects of this type are subscriptable with strings, where telegram_object[attribute_name] is equivalent to telegram_object.attribute_name. If the object does not have an attribute with the appropriate name, a KeyError will be raised.

When objects of this type are pickled, the Bot attribute associated with the object will be removed. However, when copying the object via copy.deepcopy(), the copy will have the same bot instance associated with it, i.e:

assert telegram_object.get_bot() is copy.deepcopy(telegram_object).get_bot()

Changed in version 20.0: telegram_object['from'] will look up the key from_user. This is to account for special cases like Message.from_user that deviate from the official Bot API.

classmethod de_json(data, bot)[source]

Converts JSON data to a Telegram object.

Parameters
Returns

The Telegram object.

classmethod de_list(data, bot)[source]

Converts JSON data to a list of Telegram objects.

Parameters
Returns

A list of Telegram objects.

get_bot()[source]

Returns the telegram.Bot instance associated with this object.

See also

set_bot()

Raises

RuntimeError – If no telegram.Bot instance was set for this object.

set_bot(bot)[source]

Sets the telegram.Bot instance associated with this object.

See also

get_bot()

Parameters

bot (telegram.Bot | None) – The bot instance.

to_dict()[source]

Gives representation of object as dict.

Returns

dict

to_json()[source]

Gives a JSON representation of object.

Returns

str