From 165a24e13ddda1f262605ee9a9c9fd46e45a129b Mon Sep 17 00:00:00 2001 From: NikitaPirate <59086300+NikitaPirate@users.noreply.github.com> Date: Sun, 18 Oct 2020 17:15:56 +0300 Subject: [PATCH] Add Convenience Properties for Service Chats and Anonymous Admins (#2147) Co-authored-by: Bibo-Joshi --- telegram/chat.py | 15 +++++++++++++++ telegram/constants.py | 5 +++++ telegram/user.py | 16 +++++++++++++++- tests/test_chat.py | 12 +++++++++++- tests/test_user.py | 12 +++++++++++- 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/telegram/chat.py b/telegram/chat.py index cdec2284d..d39b674f4 100644 --- a/telegram/chat.py +++ b/telegram/chat.py @@ -150,6 +150,21 @@ class Chat(TelegramObject): return "https://t.me/{}".format(self.username) return None + @property + def is_anonymous_admin(self) -> bool: + """:obj:`bool`: Convenience property. Returns :obj:`True`, if this chat is with is the bot + representing anonymous admins. This behaviour is undocumented and might be changed + by Telegram. """ + + return self.id == constants.ANONYMOUS_ADMIN_ID + + @property + def is_service_chat(self) -> bool: + """:obj:`bool`: Convenience property. Returns :obj:`True`, if this chat is the Telegram + service chat. This behaviour is undocumented and might be changed by Telegram. """ + + return self.id == constants.SERVICE_CHAT_ID + @classmethod def de_json(cls, data: JSONDict, bot: 'Bot') -> Optional['Chat']: data = cls.parse_data(data) diff --git a/telegram/constants.py b/telegram/constants.py index 669ce0ba4..dafde23a5 100644 --- a/telegram/constants.py +++ b/telegram/constants.py @@ -38,6 +38,9 @@ The following constant have been found by experimentation: Attributes: MAX_MESSAGE_ENTITIES (:obj:`int`): 100 (Beyond this cap telegram will simply ignore further formatting styles) + ANONYMOUS_ADMIN_ID (:obj:`int`): ``1087968824`` (User id in groups for anonymous admin) + SERVICE_CHAT_ID (:obj:`int`): ``777000`` (Telegram service chat, that also acts as sender of + channel posts forwarded to discussion groups) The following constants are related to specific classes and are also available as attributes of those classes: @@ -128,6 +131,8 @@ from typing import List MAX_MESSAGE_LENGTH: int = 4096 MAX_CAPTION_LENGTH: int = 1024 +ANONYMOUS_ADMIN_ID: int = 1087968824 +SERVICE_CHAT_ID: int = 777000 # constants above this line are tested diff --git a/telegram/user.py b/telegram/user.py index d4b5c8588..f9ac340ff 100644 --- a/telegram/user.py +++ b/telegram/user.py @@ -19,7 +19,7 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains an object that represents a Telegram User.""" -from telegram import TelegramObject +from telegram import TelegramObject, constants from telegram.utils.helpers import mention_html as util_mention_html from telegram.utils.helpers import mention_markdown as util_mention_markdown @@ -122,6 +122,20 @@ class User(TelegramObject): return "https://t.me/{}".format(self.username) return None + @property + def is_anonymous_admin(self) -> bool: + """:obj:`bool`: Convenience property. Returns :obj:`True`, if this user is + an anonymous admin. This behaviour is undocumented and might be changed by Telegram.""" + + return self.id == constants.ANONYMOUS_ADMIN_ID + + @property + def is_service_chat(self) -> bool: + """:obj:`bool`: Convenience property. Returns :obj:`True`, if this user is + the telegram service. This behaviour is undocumented and might be changed by Telegram.""" + + return self.id == constants.SERVICE_CHAT_ID + def get_profile_photos(self, *args: Any, **kwargs: Any) -> 'UserProfilePhotos': """ Shortcut for:: diff --git a/tests/test_chat.py b/tests/test_chat.py index 60bea2a3d..337c209c8 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -19,7 +19,7 @@ import pytest -from telegram import Chat, ChatAction, ChatPermissions +from telegram import Chat, ChatAction, ChatPermissions, constants from telegram import User @@ -95,6 +95,16 @@ class TestChat: chat.username = None assert chat.link is None + def test_anonymous_admin(self, chat): + assert chat.is_anonymous_admin is False + chat.id = constants.ANONYMOUS_ADMIN_ID + assert chat.is_anonymous_admin + + def test_service_chat(self, chat): + assert chat.is_service_chat is False + chat.id = constants.SERVICE_CHAT_ID + assert chat.is_service_chat + def test_send_action(self, monkeypatch, chat): def test(*args, **kwargs): id_ = args[0] == chat.id diff --git a/tests/test_user.py b/tests/test_user.py index 44bffe977..15fc159df 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -18,7 +18,7 @@ # along with this program. If not, see [http://www.gnu.org/licenses/]. import pytest -from telegram import Update, User +from telegram import Update, User, constants from telegram.utils.helpers import escape_markdown @@ -127,6 +127,16 @@ class TestUser: user.username = None assert user.link is None + def test_anonymous_admin(self, user): + assert user.is_anonymous_admin is False + user.id = constants.ANONYMOUS_ADMIN_ID + assert user.is_anonymous_admin + + def test_service_chat(self, user): + assert user.is_service_chat is False + user.id = constants.SERVICE_CHAT_ID + assert user.is_service_chat + def test_get_profile_photos(self, monkeypatch, user): def test(*args, **kwargs): return args[0] == user.id