Add Convenience Properties for Service Chats and Anonymous Admins (#2147)

Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
This commit is contained in:
NikitaPirate 2020-10-18 17:15:56 +03:00 committed by GitHub
parent 88440079e3
commit 165a24e13d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 3 deletions

View file

@ -150,6 +150,21 @@ class Chat(TelegramObject):
return "https://t.me/{}".format(self.username) return "https://t.me/{}".format(self.username)
return None 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 @classmethod
def de_json(cls, data: JSONDict, bot: 'Bot') -> Optional['Chat']: def de_json(cls, data: JSONDict, bot: 'Bot') -> Optional['Chat']:
data = cls.parse_data(data) data = cls.parse_data(data)

View file

@ -38,6 +38,9 @@ The following constant have been found by experimentation:
Attributes: Attributes:
MAX_MESSAGE_ENTITIES (:obj:`int`): 100 (Beyond this cap telegram will simply ignore further MAX_MESSAGE_ENTITIES (:obj:`int`): 100 (Beyond this cap telegram will simply ignore further
formatting styles) 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 The following constants are related to specific classes and are also available
as attributes of those classes: as attributes of those classes:
@ -128,6 +131,8 @@ from typing import List
MAX_MESSAGE_LENGTH: int = 4096 MAX_MESSAGE_LENGTH: int = 4096
MAX_CAPTION_LENGTH: int = 1024 MAX_CAPTION_LENGTH: int = 1024
ANONYMOUS_ADMIN_ID: int = 1087968824
SERVICE_CHAT_ID: int = 777000
# constants above this line are tested # constants above this line are tested

View file

@ -19,7 +19,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains an object that represents a Telegram User.""" """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_html as util_mention_html
from telegram.utils.helpers import mention_markdown as util_mention_markdown 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 "https://t.me/{}".format(self.username)
return None 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': def get_profile_photos(self, *args: Any, **kwargs: Any) -> 'UserProfilePhotos':
""" """
Shortcut for:: Shortcut for::

View file

@ -19,7 +19,7 @@
import pytest import pytest
from telegram import Chat, ChatAction, ChatPermissions from telegram import Chat, ChatAction, ChatPermissions, constants
from telegram import User from telegram import User
@ -95,6 +95,16 @@ class TestChat:
chat.username = None chat.username = None
assert chat.link is 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_send_action(self, monkeypatch, chat):
def test(*args, **kwargs): def test(*args, **kwargs):
id_ = args[0] == chat.id id_ = args[0] == chat.id

View file

@ -18,7 +18,7 @@
# along with this program. If not, see [http://www.gnu.org/licenses/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
import pytest import pytest
from telegram import Update, User from telegram import Update, User, constants
from telegram.utils.helpers import escape_markdown from telegram.utils.helpers import escape_markdown
@ -127,6 +127,16 @@ class TestUser:
user.username = None user.username = None
assert user.link is 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_get_profile_photos(self, monkeypatch, user):
def test(*args, **kwargs): def test(*args, **kwargs):
return args[0] == user.id return args[0] == user.id