Add support for LoginUrl buttons

This commit is contained in:
Dan 2021-03-17 17:13:55 +01:00
parent a94c3bb465
commit 182768a5d3
32 changed files with 208 additions and 74 deletions

View File

@ -385,6 +385,7 @@ def pyrogram_api():
ReplyKeyboardRemove
InlineKeyboardMarkup
InlineKeyboardButton
LoginUrl
ForceReply
CallbackQuery
GameHighScore

View File

@ -80,7 +80,7 @@ class SendGame(Scaffold):
silent=disable_notification or None,
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
reply_markup=reply_markup.write() if reply_markup else None
reply_markup=await reply_markup.write(self) if reply_markup else None
)
)

View File

@ -116,7 +116,7 @@ class EditInlineMedia(Scaffold):
raw.functions.messages.EditInlineBotMessage(
id=unpacked,
media=media,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await self.parser.parse(caption, parse_mode)
),
sleep_threshold=self.sleep_threshold

View File

@ -61,7 +61,7 @@ class EditInlineReplyMarkup(Scaffold):
return await session.send(
raw.functions.messages.EditInlineBotMessage(
id=unpacked,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
),
sleep_threshold=self.sleep_threshold
)

View File

@ -82,7 +82,7 @@ class EditInlineText(Scaffold):
raw.functions.messages.EditInlineBotMessage(
id=unpacked,
no_webpage=disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await self.parser.parse(text, parse_mode)
),
sleep_threshold=self.sleep_threshold

View File

@ -257,7 +257,7 @@ class EditMessageMedia(Scaffold):
peer=await self.resolve_peer(chat_id),
id=message_id,
media=media,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
message=message,
entities=entities
)

View File

@ -62,7 +62,7 @@ class EditMessageReplyMarkup(Scaffold):
raw.functions.messages.EditMessage(
peer=await self.resolve_peer(chat_id),
id=message_id,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
)
)

View File

@ -85,7 +85,7 @@ class EditMessageText(Scaffold):
peer=await self.resolve_peer(chat_id),
id=message_id,
no_webpage=disable_web_page_preview or None,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, text, parse_mode, entities)
)
)

View File

@ -222,7 +222,7 @@ class SendAnimation(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
)

View File

@ -217,7 +217,7 @@ class SendAudio(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
)

View File

@ -102,7 +102,7 @@ class SendCachedMedia(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
)

View File

@ -97,7 +97,7 @@ class SendContact(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None
reply_markup=await reply_markup.write(self) if reply_markup else None
)
)

View File

@ -91,7 +91,7 @@ class SendDice(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
message=""
)
)

View File

@ -194,7 +194,7 @@ class SendDocument(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
)

View File

@ -89,7 +89,7 @@ class SendLocation(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None
reply_markup=await reply_markup.write(self) if reply_markup else None
)
)

View File

@ -130,7 +130,7 @@ class SendMessage(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
message=message,
entities=entities
)

View File

@ -172,7 +172,7 @@ class SendPhoto(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
)

View File

@ -117,7 +117,7 @@ class SendPoll(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None
reply_markup=await reply_markup.write(self) if reply_markup else None
)
)

View File

@ -150,7 +150,7 @@ class SendSticker(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
message=""
)
)

View File

@ -113,7 +113,7 @@ class SendVenue(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None
reply_markup=await reply_markup.write(self) if reply_markup else None
)
)

View File

@ -228,7 +228,7 @@ class SendVideo(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
)

View File

@ -174,7 +174,7 @@ class SendVideoNote(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
message=""
)
)

View File

@ -178,7 +178,7 @@ class SendVoice(Scaffold):
reply_to_msg_id=reply_to_message_id,
random_id=self.rnd_id(),
schedule_date=schedule_date,
reply_markup=reply_markup.write() if reply_markup else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
**await utils.parse_text_entities(self, caption, parse_mode, caption_entities)
)
)

View File

@ -68,7 +68,7 @@ class StopPoll(Scaffold):
answers=[]
)
),
reply_markup=reply_markup.write() if reply_markup else None
reply_markup=await reply_markup.write(self) if reply_markup else None
)
)

View File

@ -23,10 +23,19 @@ from .game_high_score import GameHighScore
from .inline_keyboard_button import InlineKeyboardButton
from .inline_keyboard_markup import InlineKeyboardMarkup
from .keyboard_button import KeyboardButton
from .login_url import LoginUrl
from .reply_keyboard_markup import ReplyKeyboardMarkup
from .reply_keyboard_remove import ReplyKeyboardRemove
__all__ = [
"CallbackGame", "CallbackQuery", "ForceReply", "GameHighScore", "InlineKeyboardButton", "InlineKeyboardMarkup",
"KeyboardButton", "ReplyKeyboardMarkup", "ReplyKeyboardRemove"
"CallbackGame",
"CallbackQuery",
"ForceReply",
"GameHighScore",
"InlineKeyboardButton",
"InlineKeyboardMarkup",
"KeyboardButton",
"ReplyKeyboardMarkup",
"ReplyKeyboardRemove",
"LoginUrl"
]

View File

@ -16,6 +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/>.
import pyrogram
from pyrogram import raw
from ..object import Object
@ -46,12 +47,12 @@ class ForceReply(Object):
self.selective = selective
@staticmethod
def read(o):
def read(b):
return ForceReply(
selective=o.selective
selective=b.selective
)
def write(self):
async def write(self, _: "pyrogram.Client"):
return raw.types.ReplyKeyboardForceReply(
single_use=True,
selective=self.selective or None

View File

@ -18,6 +18,7 @@
from typing import Union
import pyrogram
from pyrogram import raw
from pyrogram import types
from ..object import Object
@ -38,6 +39,10 @@ class InlineKeyboardButton(Object):
url (``str``, *optional*):
HTTP url to be opened when button is pressed.
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>`_.
switch_inline_query (``str``, *optional*):
If set, pressing the button will prompt the user to select one of their chats, open that chat and insert
the bot's username and the specified inline query in the input field. Can be empty, in which case just
@ -51,15 +56,18 @@ class InlineKeyboardButton(Object):
chat's input field. Can be empty, in which case only the bot's username will be inserted.This offers a
quick way for the user to open your bot in inline mode in the same chat good for selecting something
from multiple options.
"""
# TODO: Add callback_game and pay fields
callback_game (:obj:`~pyrogram.types.CallbackGame`, *optional*):
Description of the game that will be launched when the user presses the button.
**NOTE**: This type of button **must** always be the first button in the first row.
"""
def __init__(
self,
text: str,
callback_data: Union[str, bytes] = None,
url: str = None,
login_url: "types.LoginUrl" = None,
switch_inline_query: str = None,
switch_inline_query_current_chat: str = None,
callback_game: "types.CallbackGame" = None
@ -68,6 +76,7 @@ class InlineKeyboardButton(Object):
self.text = str(text)
self.url = url
self.login_url = login_url
self.callback_data = callback_data
self.switch_inline_query = switch_inline_query
self.switch_inline_query_current_chat = switch_inline_query_current_chat
@ -75,62 +84,86 @@ class InlineKeyboardButton(Object):
# self.pay = pay
@staticmethod
def read(o):
if isinstance(o, raw.types.KeyboardButtonUrl):
return InlineKeyboardButton(
text=o.text,
url=o.url
)
if isinstance(o, raw.types.KeyboardButtonCallback):
def read(b: "raw.base.KeyboardButton"):
if isinstance(b, raw.types.KeyboardButtonCallback):
# Try decode data to keep it as string, but if fails, fallback to bytes so we don't lose any information,
# instead of decoding by ignoring/replacing errors.
try:
data = o.data.decode()
data = b.data.decode()
except UnicodeDecodeError:
data = o.data
data = b.data
return InlineKeyboardButton(
text=o.text,
text=b.text,
callback_data=data
)
if isinstance(o, raw.types.KeyboardButtonSwitchInline):
if o.same_peer:
if isinstance(b, raw.types.KeyboardButtonUrl):
return InlineKeyboardButton(
text=o.text,
switch_inline_query_current_chat=o.query
text=b.text,
url=b.url
)
if isinstance(b, raw.types.KeyboardButtonUrlAuth):
return InlineKeyboardButton(
text=b.text,
login_url=types.LoginUrl.read(b)
)
if isinstance(b, raw.types.KeyboardButtonSwitchInline):
if b.same_peer:
return InlineKeyboardButton(
text=b.text,
switch_inline_query_current_chat=b.query
)
else:
return InlineKeyboardButton(
text=o.text,
switch_inline_query=o.query
text=b.text,
switch_inline_query=b.query
)
if isinstance(o, raw.types.KeyboardButtonGame):
if isinstance(b, raw.types.KeyboardButtonGame):
return InlineKeyboardButton(
text=o.text,
text=b.text,
callback_game=types.CallbackGame()
)
def write(self):
if self.callback_data is not None:
async def write(self, client: "pyrogram.Client"):
if self.callback_data:
# Telegram only wants bytes, but we are allowed to pass strings too, for convenience.
data = bytes(self.callback_data, "utf-8") if isinstance(self.callback_data, str) else self.callback_data
return raw.types.KeyboardButtonCallback(text=self.text, data=data)
if self.url is not None:
return raw.types.KeyboardButtonUrl(text=self.text, url=self.url)
return raw.types.KeyboardButtonCallback(
text=self.text,
data=data
)
if self.switch_inline_query is not None:
return raw.types.KeyboardButtonSwitchInline(text=self.text, query=self.switch_inline_query)
if self.url:
return raw.types.KeyboardButtonUrl(
text=self.text,
url=self.url
)
if self.switch_inline_query_current_chat is not None:
if self.login_url:
return self.login_url.write(
text=self.text,
bot=await client.resolve_peer(self.login_url.bot_username)
)
if self.switch_inline_query:
return raw.types.KeyboardButtonSwitchInline(
text=self.text,
query=self.switch_inline_query
)
if self.switch_inline_query_current_chat:
return raw.types.KeyboardButtonSwitchInline(
text=self.text,
query=self.switch_inline_query_current_chat,
same_peer=True
)
if self.callback_game is not None:
return raw.types.KeyboardButtonGame(text=self.text)
if self.callback_game:
return raw.types.KeyboardButtonGame(
text=self.text
)

View File

@ -18,6 +18,7 @@
from typing import List
import pyrogram
from pyrogram import raw
from pyrogram import types
from ..object import Object
@ -52,9 +53,9 @@ class InlineKeyboardMarkup(Object):
inline_keyboard=inline_keyboard
)
def write(self):
async def write(self, client: "pyrogram.Client"):
return raw.types.ReplyInlineMarkup(
rows=[raw.types.KeyboardButtonRow(
buttons=[j.write() for j in i]
buttons=[await j.write(client) for j in i]
) for i in self.inline_keyboard]
)

View File

@ -52,25 +52,23 @@ class KeyboardButton(Object):
self.request_location = request_location
@staticmethod
def read(o):
if isinstance(o, raw.types.KeyboardButton):
return o.text
def read(b):
if isinstance(b, raw.types.KeyboardButton):
return b.text
if isinstance(o, raw.types.KeyboardButtonRequestPhone):
if isinstance(b, raw.types.KeyboardButtonRequestPhone):
return KeyboardButton(
text=o.text,
text=b.text,
request_contact=True
)
if isinstance(o, raw.types.KeyboardButtonRequestGeoLocation):
if isinstance(b, raw.types.KeyboardButtonRequestGeoLocation):
return KeyboardButton(
text=o.text,
text=b.text,
request_location=True
)
def write(self):
# TODO: Enforce optional args mutual exclusiveness
if self.request_contact:
return raw.types.KeyboardButtonRequestPhone(text=self.text)
elif self.request_location:

View File

@ -0,0 +1,89 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 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 pyrogram import raw
from ..object import Object
class LoginUrl(Object):
"""Represents a parameter of the inline keyboard button used to automatically authorize a user.
Serves as a great replacement for the Telegram Login Widget when the user is coming from Telegram.
All the user needs to do is tap/click a button and confirm that they want to log in.
Parameters:
url (``str``):
An HTTP URL to be opened with user authorization data added to the query string when the button is pressed.
If the user refuses to provide authorization data, the original URL without information about the user will
be opened. The data added is the same as described in Receiving authorization data.
**NOTE**: You **must** always check the hash of the received data to verify the authentication and the
integrity of the data as described in
`Checking authorization <https://core.telegram.org/widgets/login#checking-authorization>`_.
forward_text (``str``, *optional*):
New text of the button in forwarded messages.
bot_username (``str``, *optional*):
Username of a bot, which will be used for user authorization.
See `Setting up <https://core.telegram.org/widgets/login#setting-up-a-bot>`_ a bot for more details.
If not specified, the current bot's username will be assumed. The url's domain must be the same as the
domain linked with the bot.
See `Linking your domain to the bot <https://core.telegram.org/widgets/login#linking-your-domain-to-the-bot>`_
for more details.
request_write_access (``str``, *optional*):
Pass True to request the permission for your bot to send messages to the user.
button_id (``int``):
Button identifier.
"""
def __init__(
self, *,
url: str,
forward_text: str = None,
bot_username: str = None,
request_write_access: str = None,
button_id: int = None
):
super().__init__()
self.url = url
self.forward_text = forward_text
self.bot_username = bot_username
self.request_write_access = request_write_access
self.button_id = button_id
@staticmethod
def read(b: "raw.types.KeyboardButtonUrlAuth") -> "LoginUrl":
return LoginUrl(
url=b.url,
forward_text=b.fwd_text,
button_id=b.button_id
)
def write(self, text: str, bot: "raw.types.InputUser"):
return raw.types.InputKeyboardButtonUrlAuth(
text=text,
url=self.url,
bot=bot,
fwd_text=self.forward_text,
request_write_access=self.request_write_access
)

View File

@ -18,6 +18,7 @@
from typing import List, Union
import pyrogram
from pyrogram import raw
from pyrogram import types
from ..object import Object
@ -81,7 +82,7 @@ class ReplyKeyboardMarkup(Object):
selective=kb.selective
)
def write(self):
async def write(self, _: "pyrogram.Client"):
return raw.types.ReplyKeyboardMarkup(
rows=[raw.types.KeyboardButtonRow(
buttons=[

View File

@ -16,6 +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/>.
import pyrogram
from pyrogram import raw
from ..object import Object
@ -46,12 +47,12 @@ class ReplyKeyboardRemove(Object):
self.selective = selective
@staticmethod
def read(o):
def read(b):
return ReplyKeyboardRemove(
selective=o.selective
selective=b.selective
)
def write(self):
async def write(self, _: "pyrogram.Client"):
return raw.types.ReplyKeyboardHide(
selective=self.selective or None
)