mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 22:45:09 +01:00
Add Convenience Properties for Service Chats and Anonymous Admins (#2147)
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
This commit is contained in:
parent
88440079e3
commit
165a24e13d
5 changed files with 57 additions and 3 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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::
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue