Skip to content

Commit

Permalink
Pyrogram: Add request_chat and request_user parameters
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Aug 2, 2023
1 parent 4b0b6f6 commit 0a5e240
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pyrogram/types/bots_and_keyboards/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
from .menu_button_web_app import MenuButtonWebApp
from .reply_keyboard_markup import ReplyKeyboardMarkup
from .reply_keyboard_remove import ReplyKeyboardRemove
from .request_peer_type_chat import RequestPeerTypeChat
from .request_peer_type_user import RequestPeerTypeUser
from .sent_web_app_message import SentWebAppMessage
from .web_app_info import WebAppInfo

Expand All @@ -53,6 +55,8 @@
"KeyboardButton",
"ReplyKeyboardMarkup",
"ReplyKeyboardRemove",
"RequestPeerTypeChat",
"RequestPeerTypeUser",
"LoginUrl",
"BotCommand",
"BotCommandScope",
Expand Down
52 changes: 52 additions & 0 deletions pyrogram/types/bots_and_keyboards/keyboard_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ class KeyboardButton(Object):
If True, the user's current location will be sent when the button is pressed.
Available in private chats only.
request_chat ("obj:`~pyrogram.types.RequestPeerTypeChat`, *optional*):
If specified, defines the criteria used to request a suitable chats.
The identifier of the selected chats will be shared with the bot when the corresponding button is pressed.
request_user ("obj:`~pyrogram.types.RequestPeerTypeUser`, *optional*):
If specified, defines the criteria used to request a suitable users.
The identifier of the selected users will be shared with the bot when the corresponding button is pressed.
web_app (:obj:`~pyrogram.types.WebAppInfo`, *optional*):
If specified, the described `Web App <https://core.telegram.org/bots/webapps>`_ will be launched when the
button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private
Expand All @@ -50,13 +58,17 @@ def __init__(
text: str,
request_contact: bool = None,
request_location: bool = None,
request_chat: "types.RequestPeerTypeChat" = None,
request_user: "types.RequestPeerTypeUser" = None,
web_app: "types.WebAppInfo" = None
):
super().__init__()

self.text = str(text)
self.request_contact = request_contact
self.request_location = request_location
self.request_chat = request_chat
self.request_user = request_user
self.web_app = web_app

@staticmethod
Expand Down Expand Up @@ -84,11 +96,51 @@ def read(b):
)
)

if isinstance(b, raw.types.RequestPeerTypeChat):
return KeyboardButton(
text=b.text,
request_chat=types.RequestPeerTypeChat(
is_creator=b.creator,
is_bot_participant=b.bot_participant,
is_username=b.has_username,
is_forum=b.forum
)
)

if isinstance(b, raw.types.RequestPeerTypeUser):
return KeyboardButton(
text=b.text,
request_user=types.RequestPeerTypeUser(
is_bot=b.bot,
is_premium=b.premium
)
)

def write(self):
if self.request_contact:
return raw.types.KeyboardButtonRequestPhone(text=self.text)
elif self.request_location:
return raw.types.KeyboardButtonRequestGeoLocation(text=self.text)
elif self.request_chat:
return raw.types.KeyboardButtonRequestPeer(
text=self.text,
button_id=0,
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
)
)
elif self.request_user:
return raw.types.KeyboardButtonRequestPeer(
text=self.text,
button_id=0,
peer_type=raw.types.RequestPeerTypeUser(
bot=self.request_user.is_bot,
premium=self.request_user.is_premium
)
)
elif self.web_app:
return raw.types.KeyboardButtonSimpleWebView(text=self.text, url=self.web_app.url)
else:
Expand Down
53 changes: 53 additions & 0 deletions pyrogram/types/bots_and_keyboards/request_peer_type_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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 pyrogram import raw

from ..object import Object


class RequestPeerTypeChat(Object):
"""A bot Information.
Parameters:
is_creator (``bool``, *optional*):
If True, show only Chat which user is the owner.
is_bot_participant (``bool``, *optional*):
If True, show only Chat where bot is a participant.
is_username (``bool``, *optional*):
If True, show only Chat which has username.
is_forum (``bool``, *optional*):
If True, show only Chat which is a forum.
""" # TODO user_admin_rights, bot_admin_rights

def __init__(
self,
is_creator: bool=None,
is_bot_participant: bool=None,
is_username: bool=None,
is_forum: bool=None
):
super().__init__()

self.is_creator = is_creator
self.is_bot_participant = is_bot_participant
self.is_username = is_username
self.is_forum = is_forum
43 changes: 43 additions & 0 deletions pyrogram/types/bots_and_keyboards/request_peer_type_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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 pyrogram import raw

from ..object import Object


class RequestPeerTypeUser(Object):
"""A bot Information.
Parameters:
is_bot (``bool``, *optional*):
If True, show only Bots.
is_premium (``bool``, *optional*):
If True, show only Premium Users.
"""

def __init__(
self,
is_bot: bool=None,
is_premium: bool=None
):
super().__init__()

self.is_bot = is_bot
self.is_premium = is_premium

0 comments on commit 0a5e240

Please sign in to comment.