2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-24 10:58:29 +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 CallbackQuery
GameHighScore GameHighScore
CallbackGame CallbackGame
WebAppInfo
""", """,
bot_commands=""" bot_commands="""
Bot commands Bot commands

View File

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

View File

@ -39,6 +39,12 @@ class InlineKeyboardButton(Object):
url (``str``, *optional*): url (``str``, *optional*):
HTTP url to be opened when button is pressed. 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*): login_url (:obj:`~pyrogram.types.LoginUrl`, *optional*):
An HTTP URL used to automatically authorize the user. Can be used as a replacement for 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>`_. the `Telegram Login Widget <https://core.telegram.org/widgets/login>`_.
@ -70,6 +76,7 @@ class InlineKeyboardButton(Object):
text: str, text: str,
callback_data: Union[str, bytes] = None, callback_data: Union[str, bytes] = None,
url: str = None, url: str = None,
web_app: "types.WebAppInfo" = None,
login_url: "types.LoginUrl" = None, login_url: "types.LoginUrl" = None,
user_id: int = None, user_id: int = None,
switch_inline_query: str = None, switch_inline_query: str = None,
@ -81,6 +88,7 @@ class InlineKeyboardButton(Object):
self.text = str(text) self.text = str(text)
self.callback_data = callback_data self.callback_data = callback_data
self.url = url self.url = url
self.web_app = web_app
self.login_url = login_url self.login_url = login_url
self.user_id = user_id self.user_id = user_id
self.switch_inline_query = switch_inline_query self.switch_inline_query = switch_inline_query
@ -139,6 +147,14 @@ class InlineKeyboardButton(Object):
callback_game=types.CallbackGame() 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"): async def write(self, client: "pyrogram.Client"):
if self.callback_data is not None: if self.callback_data is not None:
# Telegram only wants bytes, but we are allowed to pass strings too, for convenience. # 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( return raw.types.KeyboardButtonGame(
text=self.text 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 # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from pyrogram import raw from pyrogram import raw, types
from ..object import Object from ..object import Object
@ -37,19 +37,27 @@ class KeyboardButton(Object):
request_location (``bool``, *optional*): request_location (``bool``, *optional*):
If True, the user's current location will be sent when the button is pressed. If True, the user's current location will be sent when the button is pressed.
Available in private chats only. 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__( def __init__(
self, self,
text: str, text: str,
request_contact: bool = None, request_contact: bool = None,
request_location: bool = None request_location: bool = None,
web_app: "types.WebAppInfo" = None
): ):
super().__init__() super().__init__()
self.text = str(text) self.text = str(text)
self.request_contact = request_contact self.request_contact = request_contact
self.request_location = request_location self.request_location = request_location
self.web_app = web_app
@staticmethod @staticmethod
def read(b): def read(b):
@ -68,10 +76,20 @@ class KeyboardButton(Object):
request_location=True request_location=True
) )
if isinstance(b, raw.types.KeyboardButtonSimpleWebView):
return KeyboardButton(
text=b.text,
web_app=types.WebAppInfo(
url=b.url
)
)
def write(self): def write(self):
if self.request_contact: if self.request_contact:
return raw.types.KeyboardButtonRequestPhone(text=self.text) return raw.types.KeyboardButtonRequestPhone(text=self.text)
elif self.request_location: elif self.request_location:
return raw.types.KeyboardButtonRequestGeoLocation(text=self.text) return raw.types.KeyboardButtonRequestGeoLocation(text=self.text)
elif self.web_app:
return raw.types.KeyboardButtonSimpleWebView(text=self.text, url=self.web_app.url)
else: else:
return raw.types.KeyboardButton(text=self.text) 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