2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-23 10:28:00 +00:00

Add WebAppInfo and field web_app to (Inline)KeyboardButton.

This commit is contained in:
Dan 2022-04-24 11:56:07 +02:00
parent f6b8d78672
commit 6b0dca09de
5 changed files with 82 additions and 2 deletions

View File

@ -416,6 +416,7 @@ def pyrogram_api():
CallbackQuery
GameHighScore
CallbackGame
WebAppInfo
""",
bot_commands="""
Bot commands

View File

@ -35,6 +35,7 @@ from .keyboard_button import KeyboardButton
from .login_url import LoginUrl
from .reply_keyboard_markup import ReplyKeyboardMarkup
from .reply_keyboard_remove import ReplyKeyboardRemove
from .web_app_info import WebAppInfo
__all__ = [
"CallbackGame",
@ -56,4 +57,5 @@ __all__ = [
"BotCommandScopeChatAdministrators",
"BotCommandScopeChatMember",
"BotCommandScopeDefault",
"WebAppInfo"
]

View File

@ -39,6 +39,12 @@ class InlineKeyboardButton(Object):
url (``str``, *optional*):
HTTP url to be opened when button is pressed.
web_app (:obj:`~pyrogram.types.WebAppInfo`, *optional*):
Description of the `Web App <https://core.telegram.org/bots/webapps>`_ that will be launched when the user
presses the button. The Web App will be able to send an arbitrary message on behalf of the user using the
method :meth:`~pyrogram.Client.answer_web_app_query`. Available only in private chats between a user and the
bot.
login_url (:obj:`~pyrogram.types.LoginUrl`, *optional*):
An HTTP URL used to automatically authorize the user. Can be used as a replacement for
the `Telegram Login Widget <https://core.telegram.org/widgets/login>`_.
@ -70,6 +76,7 @@ class InlineKeyboardButton(Object):
text: str,
callback_data: Union[str, bytes] = None,
url: str = None,
web_app: "types.WebAppInfo" = None,
login_url: "types.LoginUrl" = None,
user_id: int = None,
switch_inline_query: str = None,
@ -81,6 +88,7 @@ class InlineKeyboardButton(Object):
self.text = str(text)
self.callback_data = callback_data
self.url = url
self.web_app = web_app
self.login_url = login_url
self.user_id = user_id
self.switch_inline_query = switch_inline_query
@ -139,6 +147,14 @@ class InlineKeyboardButton(Object):
callback_game=types.CallbackGame()
)
if isinstance(b, raw.types.KeyboardButtonWebView):
return InlineKeyboardButton(
text=b.text,
web_app=types.WebAppInfo(
url=b.url
)
)
async def write(self, client: "pyrogram.Client"):
if self.callback_data is not None:
# Telegram only wants bytes, but we are allowed to pass strings too, for convenience.
@ -184,3 +200,9 @@ class InlineKeyboardButton(Object):
return raw.types.KeyboardButtonGame(
text=self.text
)
if self.web_app is not None:
return raw.types.KeyboardButtonWebView(
text=self.text,
url=self.web_app.url
)

View File

@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram import raw
from pyrogram import raw, types
from ..object import Object
@ -37,19 +37,27 @@ class KeyboardButton(Object):
request_location (``bool``, *optional*):
If True, the user's current location will be sent when the button is pressed.
Available in private chats only.
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
chats only.
"""
def __init__(
self,
text: str,
request_contact: bool = None,
request_location: bool = None
request_location: bool = None,
web_app: "types.WebAppInfo" = None
):
super().__init__()
self.text = str(text)
self.request_contact = request_contact
self.request_location = request_location
self.web_app = web_app
@staticmethod
def read(b):
@ -68,10 +76,20 @@ class KeyboardButton(Object):
request_location=True
)
if isinstance(b, raw.types.KeyboardButtonSimpleWebView):
return KeyboardButton(
text=b.text,
web_app=types.WebAppInfo(
url=b.url
)
)
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.web_app:
return raw.types.KeyboardButtonSimpleWebView(text=self.text, url=self.web_app.url)
else:
return raw.types.KeyboardButton(text=self.text)

View File

@ -0,0 +1,37 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram 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.
#
# Pyrogram 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 Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from ..object import Object
class WebAppInfo(Object):
"""Contains information about a `Web App <https://core.telegram.org/bots/webapps>`_.
Parameters:
url (``str``):
An HTTPS URL of a Web App to be opened with additional data as specified in
`Initializing Web Apps <https://core.telegram.org/bots/webapps#initializing-web-apps>`_.
"""
def __init__(
self, *,
url: str,
):
super().__init__()
self.url = url