From 182768a5d389f87320767993fcc55f2d2d65cde0 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Wed, 17 Mar 2021 17:13:55 +0100 Subject: [PATCH] Add support for LoginUrl buttons --- compiler/docs/compiler.py | 1 + pyrogram/methods/bots/send_game.py | 2 +- .../methods/messages/edit_inline_media.py | 2 +- .../messages/edit_inline_reply_markup.py | 2 +- pyrogram/methods/messages/edit_inline_text.py | 2 +- .../methods/messages/edit_message_media.py | 2 +- .../messages/edit_message_reply_markup.py | 2 +- .../methods/messages/edit_message_text.py | 2 +- pyrogram/methods/messages/send_animation.py | 2 +- pyrogram/methods/messages/send_audio.py | 2 +- .../methods/messages/send_cached_media.py | 2 +- pyrogram/methods/messages/send_contact.py | 2 +- pyrogram/methods/messages/send_dice.py | 2 +- pyrogram/methods/messages/send_document.py | 2 +- pyrogram/methods/messages/send_location.py | 2 +- pyrogram/methods/messages/send_message.py | 2 +- pyrogram/methods/messages/send_photo.py | 2 +- pyrogram/methods/messages/send_poll.py | 2 +- pyrogram/methods/messages/send_sticker.py | 2 +- pyrogram/methods/messages/send_venue.py | 2 +- pyrogram/methods/messages/send_video.py | 2 +- pyrogram/methods/messages/send_video_note.py | 2 +- pyrogram/methods/messages/send_voice.py | 2 +- pyrogram/methods/messages/stop_poll.py | 2 +- pyrogram/types/bots_and_keyboards/__init__.py | 13 ++- .../types/bots_and_keyboards/force_reply.py | 7 +- .../inline_keyboard_button.py | 95 +++++++++++++------ .../inline_keyboard_markup.py | 5 +- .../bots_and_keyboards/keyboard_button.py | 16 ++-- .../types/bots_and_keyboards/login_url.py | 89 +++++++++++++++++ .../reply_keyboard_markup.py | 3 +- .../reply_keyboard_remove.py | 7 +- 32 files changed, 208 insertions(+), 74 deletions(-) create mode 100644 pyrogram/types/bots_and_keyboards/login_url.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 0a34b206..f7583df3 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -385,6 +385,7 @@ def pyrogram_api(): ReplyKeyboardRemove InlineKeyboardMarkup InlineKeyboardButton + LoginUrl ForceReply CallbackQuery GameHighScore diff --git a/pyrogram/methods/bots/send_game.py b/pyrogram/methods/bots/send_game.py index 72fc84f4..8f6d10ab 100644 --- a/pyrogram/methods/bots/send_game.py +++ b/pyrogram/methods/bots/send_game.py @@ -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 ) ) diff --git a/pyrogram/methods/messages/edit_inline_media.py b/pyrogram/methods/messages/edit_inline_media.py index e941c131..35a1f2fd 100644 --- a/pyrogram/methods/messages/edit_inline_media.py +++ b/pyrogram/methods/messages/edit_inline_media.py @@ -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 diff --git a/pyrogram/methods/messages/edit_inline_reply_markup.py b/pyrogram/methods/messages/edit_inline_reply_markup.py index 50c10312..5966d4b7 100644 --- a/pyrogram/methods/messages/edit_inline_reply_markup.py +++ b/pyrogram/methods/messages/edit_inline_reply_markup.py @@ -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 ) diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py index 223b0422..e9a79e14 100644 --- a/pyrogram/methods/messages/edit_inline_text.py +++ b/pyrogram/methods/messages/edit_inline_text.py @@ -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 diff --git a/pyrogram/methods/messages/edit_message_media.py b/pyrogram/methods/messages/edit_message_media.py index bd8ba606..a0d365dc 100644 --- a/pyrogram/methods/messages/edit_message_media.py +++ b/pyrogram/methods/messages/edit_message_media.py @@ -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 ) diff --git a/pyrogram/methods/messages/edit_message_reply_markup.py b/pyrogram/methods/messages/edit_message_reply_markup.py index 501dc480..aefb87e9 100644 --- a/pyrogram/methods/messages/edit_message_reply_markup.py +++ b/pyrogram/methods/messages/edit_message_reply_markup.py @@ -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, ) ) diff --git a/pyrogram/methods/messages/edit_message_text.py b/pyrogram/methods/messages/edit_message_text.py index 44e3f48a..443d71f6 100644 --- a/pyrogram/methods/messages/edit_message_text.py +++ b/pyrogram/methods/messages/edit_message_text.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/send_animation.py b/pyrogram/methods/messages/send_animation.py index 303af575..d365ae56 100644 --- a/pyrogram/methods/messages/send_animation.py +++ b/pyrogram/methods/messages/send_animation.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/send_audio.py b/pyrogram/methods/messages/send_audio.py index 39a3b95d..4fa683a9 100644 --- a/pyrogram/methods/messages/send_audio.py +++ b/pyrogram/methods/messages/send_audio.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/send_cached_media.py b/pyrogram/methods/messages/send_cached_media.py index 2d8d9f5b..1f1517e7 100644 --- a/pyrogram/methods/messages/send_cached_media.py +++ b/pyrogram/methods/messages/send_cached_media.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/send_contact.py b/pyrogram/methods/messages/send_contact.py index 6cc32c48..6317a829 100644 --- a/pyrogram/methods/messages/send_contact.py +++ b/pyrogram/methods/messages/send_contact.py @@ -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 ) ) diff --git a/pyrogram/methods/messages/send_dice.py b/pyrogram/methods/messages/send_dice.py index 89a8a538..4b7422b3 100644 --- a/pyrogram/methods/messages/send_dice.py +++ b/pyrogram/methods/messages/send_dice.py @@ -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="" ) ) diff --git a/pyrogram/methods/messages/send_document.py b/pyrogram/methods/messages/send_document.py index 22023536..a7e34285 100644 --- a/pyrogram/methods/messages/send_document.py +++ b/pyrogram/methods/messages/send_document.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/send_location.py b/pyrogram/methods/messages/send_location.py index a08c0657..e70d9661 100644 --- a/pyrogram/methods/messages/send_location.py +++ b/pyrogram/methods/messages/send_location.py @@ -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 ) ) diff --git a/pyrogram/methods/messages/send_message.py b/pyrogram/methods/messages/send_message.py index fdffe79f..982e4bbb 100644 --- a/pyrogram/methods/messages/send_message.py +++ b/pyrogram/methods/messages/send_message.py @@ -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 ) diff --git a/pyrogram/methods/messages/send_photo.py b/pyrogram/methods/messages/send_photo.py index 01d2e733..fcc3c04c 100644 --- a/pyrogram/methods/messages/send_photo.py +++ b/pyrogram/methods/messages/send_photo.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/send_poll.py b/pyrogram/methods/messages/send_poll.py index 9def8afe..dc0ccf17 100644 --- a/pyrogram/methods/messages/send_poll.py +++ b/pyrogram/methods/messages/send_poll.py @@ -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 ) ) diff --git a/pyrogram/methods/messages/send_sticker.py b/pyrogram/methods/messages/send_sticker.py index 2e12b0db..c7ae122a 100644 --- a/pyrogram/methods/messages/send_sticker.py +++ b/pyrogram/methods/messages/send_sticker.py @@ -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="" ) ) diff --git a/pyrogram/methods/messages/send_venue.py b/pyrogram/methods/messages/send_venue.py index 5d969e2e..7dbae7a0 100644 --- a/pyrogram/methods/messages/send_venue.py +++ b/pyrogram/methods/messages/send_venue.py @@ -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 ) ) diff --git a/pyrogram/methods/messages/send_video.py b/pyrogram/methods/messages/send_video.py index d0ff5911..a46439ea 100644 --- a/pyrogram/methods/messages/send_video.py +++ b/pyrogram/methods/messages/send_video.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/send_video_note.py b/pyrogram/methods/messages/send_video_note.py index 9d4a8a71..91ba93fe 100644 --- a/pyrogram/methods/messages/send_video_note.py +++ b/pyrogram/methods/messages/send_video_note.py @@ -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="" ) ) diff --git a/pyrogram/methods/messages/send_voice.py b/pyrogram/methods/messages/send_voice.py index 03967b69..c534767b 100644 --- a/pyrogram/methods/messages/send_voice.py +++ b/pyrogram/methods/messages/send_voice.py @@ -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) ) ) diff --git a/pyrogram/methods/messages/stop_poll.py b/pyrogram/methods/messages/stop_poll.py index 85241d88..8853c5d6 100644 --- a/pyrogram/methods/messages/stop_poll.py +++ b/pyrogram/methods/messages/stop_poll.py @@ -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 ) ) diff --git a/pyrogram/types/bots_and_keyboards/__init__.py b/pyrogram/types/bots_and_keyboards/__init__.py index 2f85dff1..9e486cb5 100644 --- a/pyrogram/types/bots_and_keyboards/__init__.py +++ b/pyrogram/types/bots_and_keyboards/__init__.py @@ -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" ] diff --git a/pyrogram/types/bots_and_keyboards/force_reply.py b/pyrogram/types/bots_and_keyboards/force_reply.py index 0456bf48..c52bb766 100644 --- a/pyrogram/types/bots_and_keyboards/force_reply.py +++ b/pyrogram/types/bots_and_keyboards/force_reply.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +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 diff --git a/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py b/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py index b790c1a2..444eb5da 100644 --- a/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py +++ b/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py @@ -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 `_. + 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=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=o.text, - switch_inline_query_current_chat=o.query + 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 + ) diff --git a/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py b/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py index 107b2e2f..8b5ec13b 100644 --- a/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py +++ b/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py @@ -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] ) diff --git a/pyrogram/types/bots_and_keyboards/keyboard_button.py b/pyrogram/types/bots_and_keyboards/keyboard_button.py index 85adaf20..204c696a 100644 --- a/pyrogram/types/bots_and_keyboards/keyboard_button.py +++ b/pyrogram/types/bots_and_keyboards/keyboard_button.py @@ -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: diff --git a/pyrogram/types/bots_and_keyboards/login_url.py b/pyrogram/types/bots_and_keyboards/login_url.py new file mode 100644 index 00000000..21c7a45d --- /dev/null +++ b/pyrogram/types/bots_and_keyboards/login_url.py @@ -0,0 +1,89 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 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 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 `_. + + 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 `_ 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 `_ + 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 + ) diff --git a/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py b/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py index dfbdc9ea..21fa6ceb 100644 --- a/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py +++ b/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py @@ -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=[ diff --git a/pyrogram/types/bots_and_keyboards/reply_keyboard_remove.py b/pyrogram/types/bots_and_keyboards/reply_keyboard_remove.py index 020412ae..9bf16528 100644 --- a/pyrogram/types/bots_and_keyboards/reply_keyboard_remove.py +++ b/pyrogram/types/bots_and_keyboards/reply_keyboard_remove.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +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 )