2021-03-14 16:41:35 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2021-03-14 16:41:35 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""This module contains an object that represents a Telegram ChatMemberUpdated."""
|
|
|
|
import datetime
|
2022-10-07 11:51:53 +02:00
|
|
|
from typing import TYPE_CHECKING, Dict, Optional, Tuple, Union
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._chat import Chat
|
|
|
|
from telegram._chatinvitelink import ChatInviteLink
|
|
|
|
from telegram._chatmember import ChatMember
|
|
|
|
from telegram._telegramobject import TelegramObject
|
|
|
|
from telegram._user import User
|
2023-04-18 16:16:23 +02:00
|
|
|
from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._utils.types import JSONDict
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Bot
|
|
|
|
|
|
|
|
|
|
|
|
class ChatMemberUpdated(TelegramObject):
|
|
|
|
"""This object represents changes in the status of a chat member.
|
|
|
|
|
|
|
|
Objects of this class are comparable in terms of equality. Two objects of this class are
|
|
|
|
considered equal, if their :attr:`chat`, :attr:`from_user`, :attr:`date`,
|
|
|
|
:attr:`old_chat_member` and :attr:`new_chat_member` are equal.
|
|
|
|
|
|
|
|
.. versionadded:: 13.4
|
|
|
|
|
|
|
|
Note:
|
2023-02-05 18:09:55 +01:00
|
|
|
In Python :keyword:`from` is a reserved word. Use :paramref:`from_user` instead.
|
2021-03-14 16:41:35 +01:00
|
|
|
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
Examples:
|
|
|
|
:any:`Chat Member Bot <examples.chatmemberbot>`
|
|
|
|
|
2021-03-14 16:41:35 +01:00
|
|
|
Args:
|
|
|
|
chat (:class:`telegram.Chat`): Chat the user belongs to.
|
|
|
|
from_user (:class:`telegram.User`): Performer of the action, which resulted in the change.
|
|
|
|
date (:class:`datetime.datetime`): Date the change was done in Unix time. Converted to
|
|
|
|
:class:`datetime.datetime`.
|
2023-04-18 16:16:23 +02:00
|
|
|
|
2023-05-07 15:31:23 +02:00
|
|
|
.. versionchanged:: 20.3
|
2023-04-18 16:16:23 +02:00
|
|
|
|datetime_localization|
|
2021-03-14 16:41:35 +01:00
|
|
|
old_chat_member (:class:`telegram.ChatMember`): Previous information about the chat member.
|
|
|
|
new_chat_member (:class:`telegram.ChatMember`): New information about the chat member.
|
|
|
|
invite_link (:class:`telegram.ChatInviteLink`, optional): Chat invite link, which was used
|
|
|
|
by the user to join the chat. For joining by invite link events only.
|
2023-05-07 13:44:34 +02:00
|
|
|
via_chat_folder_invite_link (:obj:`bool`, optional): :obj:`True`, if the user joined the
|
|
|
|
chat via a chat folder invite link
|
|
|
|
|
2023-05-07 15:31:23 +02:00
|
|
|
.. versionadded:: 20.3
|
2024-05-20 15:25:25 +02:00
|
|
|
via_join_request (:obj:`bool`, optional): :obj:`True`, if the user joined the chat after
|
|
|
|
sending a direct join request without using an invite link and being approved by
|
|
|
|
an administrator
|
|
|
|
|
2024-05-20 17:23:38 +02:00
|
|
|
.. versionadded:: 21.2
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
Attributes:
|
|
|
|
chat (:class:`telegram.Chat`): Chat the user belongs to.
|
|
|
|
from_user (:class:`telegram.User`): Performer of the action, which resulted in the change.
|
|
|
|
date (:class:`datetime.datetime`): Date the change was done in Unix time. Converted to
|
|
|
|
:class:`datetime.datetime`.
|
2023-04-18 16:16:23 +02:00
|
|
|
|
2023-05-07 15:31:23 +02:00
|
|
|
.. versionchanged:: 20.3
|
2023-04-18 16:16:23 +02:00
|
|
|
|datetime_localization|
|
2021-03-14 16:41:35 +01:00
|
|
|
old_chat_member (:class:`telegram.ChatMember`): Previous information about the chat member.
|
|
|
|
new_chat_member (:class:`telegram.ChatMember`): New information about the chat member.
|
|
|
|
invite_link (:class:`telegram.ChatInviteLink`): Optional. Chat invite link, which was used
|
2023-01-01 16:24:00 +01:00
|
|
|
by the user to join the chat. For joining by invite link events only.
|
2023-05-07 13:44:34 +02:00
|
|
|
via_chat_folder_invite_link (:obj:`bool`): Optional. :obj:`True`, if the user joined the
|
|
|
|
chat via a chat folder invite link
|
|
|
|
|
2023-05-07 15:31:23 +02:00
|
|
|
.. versionadded:: 20.3
|
2024-05-20 15:25:25 +02:00
|
|
|
via_join_request (:obj:`bool`): Optional. :obj:`True`, if the user joined the chat after
|
|
|
|
sending a direct join request without using an invite link and being approved
|
|
|
|
by an administrator
|
|
|
|
|
2024-05-20 17:23:38 +02:00
|
|
|
.. versionadded:: 21.2
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = (
|
2022-05-05 17:40:22 +02:00
|
|
|
"chat",
|
|
|
|
"date",
|
2024-02-05 19:24:00 +01:00
|
|
|
"from_user",
|
2022-05-05 17:40:22 +02:00
|
|
|
"invite_link",
|
2024-02-05 19:24:00 +01:00
|
|
|
"new_chat_member",
|
|
|
|
"old_chat_member",
|
2023-05-07 13:44:34 +02:00
|
|
|
"via_chat_folder_invite_link",
|
2024-05-20 15:25:25 +02:00
|
|
|
"via_join_request",
|
2021-05-29 16:18:16 +02:00
|
|
|
)
|
|
|
|
|
2021-03-14 16:41:35 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
chat: Chat,
|
|
|
|
from_user: User,
|
|
|
|
date: datetime.datetime,
|
|
|
|
old_chat_member: ChatMember,
|
|
|
|
new_chat_member: ChatMember,
|
2023-05-18 07:57:59 +02:00
|
|
|
invite_link: Optional[ChatInviteLink] = None,
|
|
|
|
via_chat_folder_invite_link: Optional[bool] = None,
|
2024-05-20 15:25:25 +02:00
|
|
|
via_join_request: Optional[bool] = None,
|
2022-10-07 11:51:53 +02:00
|
|
|
*,
|
2023-05-18 07:57:59 +02:00
|
|
|
api_kwargs: Optional[JSONDict] = None,
|
2021-03-14 16:41:35 +01:00
|
|
|
):
|
2022-10-07 11:51:53 +02:00
|
|
|
super().__init__(api_kwargs=api_kwargs)
|
2021-03-14 16:41:35 +01:00
|
|
|
# Required
|
2023-02-02 18:55:07 +01:00
|
|
|
self.chat: Chat = chat
|
|
|
|
self.from_user: User = from_user
|
|
|
|
self.date: datetime.datetime = date
|
|
|
|
self.old_chat_member: ChatMember = old_chat_member
|
|
|
|
self.new_chat_member: ChatMember = new_chat_member
|
2023-05-07 13:44:34 +02:00
|
|
|
self.via_chat_folder_invite_link: Optional[bool] = via_chat_folder_invite_link
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
# Optionals
|
2023-02-02 18:55:07 +01:00
|
|
|
self.invite_link: Optional[ChatInviteLink] = invite_link
|
2024-05-20 15:25:25 +02:00
|
|
|
self.via_join_request: Optional[bool] = via_join_request
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
self._id_attrs = (
|
|
|
|
self.chat,
|
|
|
|
self.from_user,
|
|
|
|
self.date,
|
|
|
|
self.old_chat_member,
|
|
|
|
self.new_chat_member,
|
|
|
|
)
|
|
|
|
|
2022-12-15 15:00:36 +01:00
|
|
|
self._freeze()
|
|
|
|
|
2021-03-14 16:41:35 +01:00
|
|
|
@classmethod
|
2022-05-05 17:40:22 +02:00
|
|
|
def de_json(cls, data: Optional[JSONDict], bot: "Bot") -> Optional["ChatMemberUpdated"]:
|
2021-05-27 09:38:17 +02:00
|
|
|
"""See :meth:`telegram.TelegramObject.de_json`."""
|
|
|
|
data = cls._parse_data(data)
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
2023-04-18 16:16:23 +02:00
|
|
|
# Get the local timezone from the bot if it has defaults
|
|
|
|
loc_tzinfo = extract_tzinfo_from_defaults(bot)
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
data["chat"] = Chat.de_json(data.get("chat"), bot)
|
2022-10-07 11:51:53 +02:00
|
|
|
data["from_user"] = User.de_json(data.pop("from", None), bot)
|
2023-04-18 16:16:23 +02:00
|
|
|
data["date"] = from_timestamp(data.get("date"), tzinfo=loc_tzinfo)
|
2022-05-05 17:40:22 +02:00
|
|
|
data["old_chat_member"] = ChatMember.de_json(data.get("old_chat_member"), bot)
|
|
|
|
data["new_chat_member"] = ChatMember.de_json(data.get("new_chat_member"), bot)
|
|
|
|
data["invite_link"] = ChatInviteLink.de_json(data.get("invite_link"), bot)
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-10-07 11:51:53 +02:00
|
|
|
return super().de_json(data=data, bot=bot)
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-04-27 21:54:57 +02:00
|
|
|
def _get_attribute_difference(self, attribute: str) -> Tuple[object, object]:
|
|
|
|
try:
|
|
|
|
old = self.old_chat_member[attribute]
|
|
|
|
except KeyError:
|
|
|
|
old = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
new = self.new_chat_member[attribute]
|
|
|
|
except KeyError:
|
|
|
|
new = None
|
|
|
|
|
|
|
|
return old, new
|
|
|
|
|
2021-04-30 10:14:41 +02:00
|
|
|
def difference(
|
|
|
|
self,
|
|
|
|
) -> Dict[
|
|
|
|
str,
|
|
|
|
Tuple[
|
|
|
|
Union[str, bool, datetime.datetime, User], Union[str, bool, datetime.datetime, User]
|
|
|
|
],
|
|
|
|
]:
|
|
|
|
"""Computes the difference between :attr:`old_chat_member` and :attr:`new_chat_member`.
|
|
|
|
|
|
|
|
Example:
|
2022-04-24 12:38:09 +02:00
|
|
|
.. code:: pycon
|
2021-04-30 10:14:41 +02:00
|
|
|
|
|
|
|
>>> chat_member_updated.difference()
|
|
|
|
{'custom_title': ('old title', 'new title')}
|
|
|
|
|
|
|
|
Note:
|
|
|
|
To determine, if the :attr:`telegram.ChatMember.user` attribute has changed, *every*
|
|
|
|
attribute of the user will be checked.
|
|
|
|
|
|
|
|
.. versionadded:: 13.5
|
|
|
|
|
|
|
|
Returns:
|
2022-02-09 17:30:16 +01:00
|
|
|
Dict[:obj:`str`, Tuple[:class:`object`, :class:`object`]]: A dictionary mapping
|
2022-06-09 17:08:54 +02:00
|
|
|
attribute names to tuples of the form ``(old_value, new_value)``
|
2021-04-30 10:14:41 +02:00
|
|
|
"""
|
|
|
|
# we first get the names of the attributes that have changed
|
|
|
|
# user.to_dict() is unhashable, so that needs some special casing further down
|
|
|
|
old_dict = self.old_chat_member.to_dict()
|
2022-05-05 17:40:22 +02:00
|
|
|
old_user_dict = old_dict.pop("user")
|
2021-04-30 10:14:41 +02:00
|
|
|
new_dict = self.new_chat_member.to_dict()
|
2022-05-05 17:40:22 +02:00
|
|
|
new_user_dict = new_dict.pop("user")
|
2021-04-30 10:14:41 +02:00
|
|
|
|
|
|
|
# Generator for speed: we only need to iterate over it once
|
|
|
|
# we can't directly use the values from old_dict ^ new_dict b/c that set is unordered
|
|
|
|
attributes = (entry[0] for entry in set(old_dict.items()) ^ set(new_dict.items()))
|
|
|
|
|
2022-04-27 21:54:57 +02:00
|
|
|
result = {attribute: self._get_attribute_difference(attribute) for attribute in attributes}
|
2021-04-30 10:14:41 +02:00
|
|
|
if old_user_dict != new_user_dict:
|
2022-05-05 17:40:22 +02:00
|
|
|
result["user"] = (self.old_chat_member.user, self.new_chat_member.user)
|
2021-04-30 10:14:41 +02:00
|
|
|
|
|
|
|
return result # type: ignore[return-value]
|