- class telegram.TelegramObject(*, api_kwargs=None)[source]¶
Bases:
objectBase class for most Telegram objects.
Objects of this type are subscriptable with strings. See
__getitem__()for more details. Thepickleanddeepcopy()behavior of objects of this type are defined by__getstate__(),__setstate__()and__deepcopy__().Tip
Objects of this type can be serialized via Python’s
picklemodule and pickled objects from one version of PTB are usually loadable in future versions. However, we can not guarantee that this compatibility will always be provided. At least a manual one-time conversion of the data may be needed on major updates of the library.Changed in version 20.0:
Removed argument and attribute
botfor several subclasses. Useset_bot()andget_bot()instead.Removed the possibility to pass arbitrary keyword arguments for several subclasses.
String representations objects of this type was overhauled. See
__repr__()for details. As this class doesn’t implementobject.__str__(), the default implementation will be used, which is equivalent to__repr__().Objects of this class (or subclasses) are now immutable. This means that you can’t set or delete attributes anymore. Moreover, attributes that were formerly of type
listare now of typetuple.
- Parameters:
api_kwargs (dict[
str, any], optional) –Arbitrary keyword arguments. Can be used to store data for which there are no dedicated attributes. These arguments are also considered by
to_dict()andto_json(), i.e. when passing objects to Telegram. Passing them to Telegram is however not guaranteed to work for all kinds of objects, e.g. this will fail for objects that can not directly be JSON serialized.Added in version 20.0.
- api_kwargs[source]¶
Optional. Arbitrary keyword arguments. Used to store data for which there are no dedicated attributes. These arguments are also considered by
to_dict()andto_json(), i.e. when passing objects to Telegram. Passing them to Telegram is however not guaranteed to work for all kinds of objects, e.g. this will fail for objects that can not directly be JSON serialized.Added in version 20.0.
- Type:
types.MappingProxyType[str, any]
- __deepcopy__(memodict)[source]¶
Customizes how
copy.deepcopy()processes objects of this type. The only difference to the default implementation is that thetelegram.Botinstance set viaset_bot()(if any) is not copied, but shared between the original and the copy, i.e.:assert telegram_object.get_bot() is copy.deepcopy(telegram_object).get_bot()
- __delattr__(key)[source]¶
Overrides
object.__delattr__()to prevent the deletion of attributes.- Raises:
- __eq__(other)[source]¶
Compares this object with
otherin terms of equality. If this object andotherare not objects of the same class, this comparison will fall back to Python’s default implementation ofobject.__eq__(). Otherwise, both objects may be compared in terms of equality, if the corresponding subclass ofTelegramObjecthas defined a set of attributes to compare and the objects are considered to be equal, if all of these attributes are equal. If the subclass has not defined a set of attributes to compare, a warning will be issued.Tip
If instances of a class in the
telegrammodule are comparable in terms of equality, the documentation of the class will state the attributes that will be used for this comparison.
- __getitem__(item)[source]¶
Objects of this type are subscriptable with strings, where
telegram_object["attribute_name"]is equivalent totelegram_object.attribute_name.Tip
This is useful for dynamic attribute lookup, i.e.
telegram_object[arg]where the value ofargis determined at runtime. In all other cases, it’s recommended to use the dot notation instead, i.e.telegram_object.attribute_name.Changed in version 20.0:
telegram_object['from']will look up the keyfrom_user. This is to account for special cases likeMessage.from_userthat deviate from the official Bot API.
- __getstate__()[source]¶
Overrides
object.__getstate__()to customize the pickling process of objects of this type. The returned state does not contain thetelegram.Botinstance set withset_bot()(if any), as it can’t be pickled.
- __hash__()[source]¶
Builds a hash value for this object such that the hash of two objects is equal if and only if the objects are equal in terms of
__eq__().- Returns:
- __repr__()[source]¶
Gives a string representation of this object in the form
ClassName(attr_1=value_1, attr_2=value_2, ...), where attributes are omitted if they have the valueNoneor are empty instances ofcollections.abc.Sized(e.g.list,dict,set,str, etc.).As this class doesn’t implement
object.__str__(), the default implementation will be used, which is equivalent to__repr__().- Returns:
- __setattr__(key, value)[source]¶
Overrides
object.__setattr__()to prevent the overriding of attributes.- Raises:
- __setstate__(state)[source]¶
Overrides
object.__setstate__()to customize the unpickling process of objects of this type. Modifies the object in-place.If any data was stored in the
api_kwargsof the pickled object, this method checks if the class now has dedicated attributes for those keys and moves the values fromapi_kwargsto the dedicated attributes. This can happen, if serialized data is loaded with a new version of this library, where the new version was updated to account for updates of the Telegram Bot API.If on the contrary an attribute was removed from the class, the value is not discarded but made available via
api_kwargs.
- classmethod de_list(data, bot=None)[source]¶
Converts a list of JSON objects to a tuple of Telegram objects.
Changed in version 20.0:
Returns a tuple instead of a list.
Filters out any
Nonevalues.
- get_bot()[source]¶
Returns the
telegram.Botinstance associated with this object.- Raises:
RuntimeError – If no
telegram.Botinstance was set for this object.
- set_bot(bot)[source]¶
Sets the
telegram.Botinstance associated with this object.- Parameters:
bot (
telegram.Bot|None) – The bot instance.
- to_dict(recursive=True)[source]¶
Gives representation of object as
dict.Changed in version 20.0:
Now includes all entries of
api_kwargs.Attributes whose values are empty sequences are no longer included.
- to_json()[source]¶
Gives a JSON representation of object.
Changed in version 20.0: Now includes all entries of
api_kwargs.- Returns:
TelegramObject - python-telegram-bot v22.6