diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 01acf632..9b9d2322 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -416,6 +416,7 @@ def pyrogram_api(): CallbackQuery GameHighScore CallbackGame + WebAppInfo """, bot_commands=""" Bot commands diff --git a/pyrogram/types/bots_and_keyboards/__init__.py b/pyrogram/types/bots_and_keyboards/__init__.py index 37e6dfe8..44fd5894 100644 --- a/pyrogram/types/bots_and_keyboards/__init__.py +++ b/pyrogram/types/bots_and_keyboards/__init__.py @@ -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" ] diff --git a/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py b/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py index de6a0420..a1d8a7ad 100644 --- a/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py +++ b/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py @@ -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 `_ 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 `_. @@ -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 + ) diff --git a/pyrogram/types/bots_and_keyboards/keyboard_button.py b/pyrogram/types/bots_and_keyboards/keyboard_button.py index 626df749..5c8d4b73 100644 --- a/pyrogram/types/bots_and_keyboards/keyboard_button.py +++ b/pyrogram/types/bots_and_keyboards/keyboard_button.py @@ -16,7 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . -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 `_ 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) diff --git a/pyrogram/types/bots_and_keyboards/web_app_info.py b/pyrogram/types/bots_and_keyboards/web_app_info.py new file mode 100644 index 00000000..1dbb0a78 --- /dev/null +++ b/pyrogram/types/bots_and_keyboards/web_app_info.py @@ -0,0 +1,37 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# 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 . + +from ..object import Object + + +class WebAppInfo(Object): + """Contains information about a `Web App `_. + + Parameters: + url (``str``): + An HTTPS URL of a Web App to be opened with additional data as specified in + `Initializing Web Apps `_. + """ + + def __init__( + self, *, + url: str, + ): + super().__init__() + + self.url = url