Skip to content

Commit

Permalink
pyrofork: Refactor request_chat/channel/user
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Jul 31, 2024
1 parent 407e3d6 commit 9262d1c
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 45 deletions.
3 changes: 3 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,9 @@ def get_title_list(s: str) -> list:
RequestPeerTypeChannel
RequestPeerTypeChat
RequestPeerTypeUser
RequestedChats
RequestedChat
RequestedUser
LoginUrl
ForceReply
CallbackQuery
Expand Down
7 changes: 2 additions & 5 deletions pyrogram/enums/message_service_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ class MessageServiceType(AutoName):
GAME_HIGH_SCORE = auto()
"Game high score"

ChannelShared = auto()
"a shared chat/channel"

UserShared = auto()
"a shared user"
ChatShared = auto()
"a shared chat/channel/user"

FORUM_TOPIC_CREATED = auto()
"a new forum topic created in the chat"
Expand Down
6 changes: 6 additions & 0 deletions pyrogram/types/bots_and_keyboards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
from .request_peer_type_channel import RequestPeerTypeChannel
from .request_peer_type_chat import RequestPeerTypeChat
from .request_peer_type_user import RequestPeerTypeUser
from .requested_chat import RequestedChat
from .requested_chats import RequestedChats
from .requested_user import RequestedUser
from .sent_web_app_message import SentWebAppMessage
from .shipping_address import ShippingAddress
from .shipping_option import ShippingOption
Expand All @@ -77,6 +80,9 @@
"RequestPeerTypeChannel",
"RequestPeerTypeChat",
"RequestPeerTypeUser",
"RequestedChats",
"RequestedChat",
"RequestedUser",
"LoginUrl",
"BotCommand",
"BotCommandScope",
Expand Down
42 changes: 30 additions & 12 deletions pyrogram/types/bots_and_keyboards/keyboard_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ def read(b):
request_chat=types.RequestPeerTypeChannel(
is_creator=b.peer_type.creator,
is_username=b.peer_type.has_username,
max=b.max_quantity
max=b.max_quantity,
is_name_requested=b.name_requested,
is_username_requested=b.username_requested,
is_photo_requested=b.photo_requested
)
)
if isinstance(b.peer_type, raw.types.RequestPeerTypeChat):
Expand All @@ -115,7 +118,10 @@ def read(b):
is_bot_participant=b.peer_type.bot_participant,
is_username=b.peer_type.has_username,
is_forum=b.peer_type.forum,
max=b.max_quantity
max=b.max_quantity,
is_name_requested=b.name_requested,
is_username_requested=b.username_requested,
is_photo_requested=b.photo_requested
)
)

Expand All @@ -125,7 +131,10 @@ def read(b):
request_user=types.RequestPeerTypeUser(
is_bot=b.peer_type.bot,
is_premium=b.peer_type.premium,
max=b.max_quantity
max=b.max_quantity,
is_name_requested=b.name_requested,
is_username_requested=b.username_requested,
is_photo_requested=b.photo_requested
)
)

Expand All @@ -136,35 +145,44 @@ def write(self):
return raw.types.KeyboardButtonRequestGeoLocation(text=self.text)
elif self.request_chat:
if isinstance(self.request_chat, types.RequestPeerTypeChannel):
return raw.types.KeyboardButtonRequestPeer(
return raw.types.InputKeyboardButtonRequestPeer(
text=self.text,
button_id=0,
button_id=self.request_chat.button_id,
peer_type=raw.types.RequestPeerTypeBroadcast(
creator=self.request_chat.is_creator,
has_username=self.request_chat.is_username
),
max_quantity=self.request_chat.max
max_quantity=self.request_chat.max,
name_requested=self.request_chat.is_name_requested,
username_requested=self.request_chat.is_username_requested,
photo_requested=self.request_chat.is_photo_requested
)
return raw.types.KeyboardButtonRequestPeer(
return raw.types.InputKeyboardButtonRequestPeer(
text=self.text,
button_id=0,
button_id=self.request_chat.button_id,
peer_type=raw.types.RequestPeerTypeChat(
creator=self.request_chat.is_creator,
bot_participant=self.request_chat.is_bot_participant,
has_username=self.request_chat.is_username,
forum=self.request_chat.is_forum
),
max_quantity=self.request_chat.max
max_quantity=self.request_chat.max,
name_requested=self.request_chat.is_name_requested,
username_requested=self.request_chat.is_username_requested,
photo_requested=self.request_chat.is_photo_requested
)
elif self.request_user:
return raw.types.KeyboardButtonRequestPeer(
return raw.types.InputKeyboardButtonRequestPeer(
text=self.text,
button_id=0,
button_id=self.request_chat.button_id,
peer_type=raw.types.RequestPeerTypeUser(
bot=self.request_user.is_bot,
premium=self.request_user.is_premium
),
max_quantity=self.request_user.max
max_quantity=self.request_user.max,
name_requested=self.request_user.is_name_requested,
username_requested=self.request_user.is_username_requested,
photo_requested=self.request_user.is_photo_requested
)
elif self.web_app:
return raw.types.KeyboardButtonSimpleWebView(text=self.text, url=self.web_app.url)
Expand Down
29 changes: 28 additions & 1 deletion pyrogram/types/bots_and_keyboards/request_peer_type_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,48 @@ class RequestPeerTypeChannel(Object):
"""Object used to request clients to send a channel identifier.
Parameters:
button_id (``int``, *optional*):
Button identifier.
is_creator (``bool``, *optional*):
If True, show only Channel which user is the owner.
is_username (``bool``, *optional*):
If True, show only Channel which has username.
max (``int``, *optional*):
Maximum number of channels to be returned.
default 1.
is_name_requested (``bool``, *optional*):
If True, Channel name is requested.
default True.
is_username_requested (``bool``, *optional*):
If True, Channel username is requested.
default True.
is_photo_requested (``bool``, *optional*):
If True, Channel photo is requested.
default True.
""" # TODO user_admin_rights, bot_admin_rights

def __init__(
self,
button_id: int=0,
is_creator: bool=None,
is_username: bool=None,
max: int=1
max: int=1,
is_name_requested: bool=True,
is_username_requested: bool=True,
is_photo_requested: bool=True
):
super().__init__()

self.button_id = button_id
self.is_creator = is_creator
self.is_username = is_username
self.max = max
self.is_name_requested = is_name_requested
self.is_username_requested = is_username_requested
self.is_photo_requested = is_photo_requested
29 changes: 28 additions & 1 deletion pyrogram/types/bots_and_keyboards/request_peer_type_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class RequestPeerTypeChat(Object):
"""Object used to request clients to send a chat identifier.
Parameters:
button_id (``int``, *optional*):
Button identifier.
is_creator (``bool``, *optional*):
If True, show only Chat which user is the owner.
Expand All @@ -34,20 +37,44 @@ class RequestPeerTypeChat(Object):
is_forum (``bool``, *optional*):
If True, show only Chat which is a forum.
max (``int``, *optional*):
Maximum number of chats to be returned.
default 1.
is_name_requested (``bool``, *optional*):
If True, Chat name is requested.
default True.
is_username_requested (``bool``, *optional*):
If True, Chat username is requested.
default True.
is_photo_requested (``bool``, *optional*):
If True, Chat photo is requested.
default True.
""" # TODO user_admin_rights, bot_admin_rights

def __init__(
self,
button_id: int=0,
is_creator: bool=None,
is_bot_participant: bool=None,
is_username: bool=None,
is_forum: bool=None,
max: int=1
max: int=1,
is_name_requested: bool=True,
is_username_requested: bool=True,
is_photo_requested: bool=True
):
super().__init__()

self.button_id = button_id
self.is_creator = is_creator
self.is_bot_participant = is_bot_participant
self.is_username = is_username
self.is_forum = is_forum
self.max = max
self.is_name_requested = is_name_requested
self.is_username_requested = is_username_requested
self.is_photo_requested = is_photo_requested
29 changes: 28 additions & 1 deletion pyrogram/types/bots_and_keyboards/request_peer_type_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,48 @@ class RequestPeerTypeUser(Object):
"""Object used to request clients to send a user identifier.
Parameters:
button_id (``int``, *optional*):
Button identifier.
is_bot (``bool``, *optional*):
If True, show only Bots.
is_premium (``bool``, *optional*):
If True, show only Premium Users.
max (``int``, *optional*):
Maximum number of users to be returned.
default 1.
is_name_requested (``bool``, *optional*):
If True, User name is requested.
default True.
is_username_requested (``bool``, *optional*):
If True, User username is requested.
default True.
is_photo_requested (``bool``, *optional*):
If True, User photo is requested.
default True.
"""

def __init__(
self,
button_id: int=0,
is_bot: bool=None,
is_premium: bool=None,
max: int=1
max: int=1,
is_name_requested: bool=True,
is_username_requested: bool=True,
is_photo_requested: bool=True
):
super().__init__()

self.button_id = button_id
self.is_bot = is_bot
self.is_premium = is_premium
self.max = max
self.is_name_requested = is_name_requested
self.is_username_requested = is_username_requested
self.is_photo_requested = is_photo_requested
73 changes: 73 additions & 0 deletions pyrogram/types/bots_and_keyboards/requested_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
#
# This file is part of Pyrofork.
#
# Pyrofork is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrofork 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 General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.

from ..object import Object
from pyrogram import enums, raw, types, utils
from typing import Union

class RequestedChat(Object):
"""Contains information about a requested chat.
Parameters:
chat_type (``enums.ChatType``):
Type of the chat.
name (``str``, *optional*):
Name of the chat.
username (``str``, *optional*):
Username of the chat.
photo (``types.ChatPhoto``, *optional*):
Chat photo.
"""
def __init__(
self,
chat_id: int,
chat_type: enums.ChatType,
name: str = None,
username: str = None,
photo: "types.ChatPhoto" = None,
):
super().__init__()

self.chat_id = chat_id
self.chat_type = chat_type
self.name = name
self.username = username
self.photo = photo

@staticmethod
async def _parse(
client,
request: Union[
"raw.types.RequestedPeerChat",
"raw.types.RequestedPeerChannel"
]
) -> "RequestedChat":
if isinstance(request, raw.types.RequestedPeerChannel):
type = enums.ChatType.CHANNEL
else:
type = enums.ChatType.GROUP
return RequestedChat(
chat_id=utils.get_channel_id(utils.get_raw_peer_id(request.peer)),
chat_type=type,
name=getattr(request, "title", None),
username=getattr(request, "username", None),
photo=await types.ChatPhoto._parse(client, getattr(request, "photo", None)),
)
Loading

0 comments on commit 9262d1c

Please sign in to comment.