From 59461adc55a317f3de828cf9811be4804d6d2eec Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sat, 1 Jun 2024 23:14:52 +0700 Subject: [PATCH] pyrofork: add InlineKeyboardButtonBuy and MessageInvoice Signed-off-by: wulan17 --- compiler/docs/compiler.py | 2 + pyrogram/enums/message_media_type.py | 3 + pyrogram/types/bots_and_keyboards/__init__.py | 2 + .../inline_keyboard_button.py | 3 + .../inline_keyboard_button_buy.py | 45 +++++++++ .../inline_keyboard_markup.py | 4 +- .../reply_keyboard_markup.py | 1 + pyrogram/types/messages_and_media/__init__.py | 3 +- pyrogram/types/messages_and_media/message.py | 10 ++ .../messages_and_media/message_invoice.py | 91 +++++++++++++++++++ 10 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 pyrogram/types/bots_and_keyboards/inline_keyboard_button_buy.py create mode 100644 pyrogram/types/messages_and_media/message_invoice.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 2536e1bb..3cdf311f 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -481,6 +481,7 @@ def pyrogram_api(): Giveaway GiveawayLaunched GiveawayResult + MessageInvoice MessageStory WebPage WebPageEmpty @@ -536,6 +537,7 @@ def pyrogram_api(): ReplyKeyboardRemove InlineKeyboardMarkup InlineKeyboardButton + InlineKeyboardButtonBuy RequestPeerTypeChannel RequestPeerTypeChat RequestPeerTypeUser diff --git a/pyrogram/enums/message_media_type.py b/pyrogram/enums/message_media_type.py index d26c4172..10b11cc7 100644 --- a/pyrogram/enums/message_media_type.py +++ b/pyrogram/enums/message_media_type.py @@ -78,3 +78,6 @@ class MessageMediaType(AutoName): STORY = auto() "Forwarded story media" + + INVOICE = auto() + "Invoice media" diff --git a/pyrogram/types/bots_and_keyboards/__init__.py b/pyrogram/types/bots_and_keyboards/__init__.py index f37624f2..bbe957ed 100644 --- a/pyrogram/types/bots_and_keyboards/__init__.py +++ b/pyrogram/types/bots_and_keyboards/__init__.py @@ -36,6 +36,7 @@ from .game_high_score import GameHighScore from .inline_keyboard_button import InlineKeyboardButton from .inline_keyboard_markup import InlineKeyboardMarkup from .keyboard_button import KeyboardButton +from .inline_keyboard_button_buy import InlineKeyboardButtonBuy from .login_url import LoginUrl from .menu_button import MenuButton from .menu_button_commands import MenuButtonCommands @@ -61,6 +62,7 @@ __all__ = [ "ForceReply", "GameHighScore", "InlineKeyboardButton", + "InlineKeyboardButtonBuy", "InlineKeyboardMarkup", "KeyboardButton", "ReplyKeyboardMarkup", diff --git a/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py b/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py index e5542eb8..a6bbb795 100644 --- a/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py +++ b/pyrogram/types/bots_and_keyboards/inline_keyboard_button.py @@ -162,6 +162,9 @@ class InlineKeyboardButton(Object): ) ) + if isinstance(b, raw.types.KeyboardButtonBuy): + return types.InlineKeyboardButtonBuy.read(b) + 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. diff --git a/pyrogram/types/bots_and_keyboards/inline_keyboard_button_buy.py b/pyrogram/types/bots_and_keyboards/inline_keyboard_button_buy.py new file mode 100644 index 00000000..3f74f2a1 --- /dev/null +++ b/pyrogram/types/bots_and_keyboards/inline_keyboard_button_buy.py @@ -0,0 +1,45 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2022-present Mayuri-Chan +# +# This file is part of Pyrofork. +# +# Pyrofork 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. +# +# Pyrofork 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 Pyrofork. If not, see . + +from ..object import Object + +class InlineKeyboardButtonBuy(Object): + """One button of the inline keyboard. + For simple invoice buttons. + + Parameters: + text (``str``): + Text of the button. If none of the optional fields are used, it will be sent as a message when + the button is pressed. + """ + + def __init__( + self, + text: str + ): + super().__init__() + + self.text = str(text) + + @staticmethod + def read(b): + return InlineKeyboardButtonBuy( + text=b.text + ) + + # TODO: Implement write method diff --git a/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py b/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py index c9f0c82d..e91d1e0d 100644 --- a/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py +++ b/pyrogram/types/bots_and_keyboards/inline_keyboard_markup.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrofork. If not, see . -from typing import List +from typing import List, Union import pyrogram from pyrogram import raw @@ -33,7 +33,7 @@ class InlineKeyboardMarkup(Object): List of button rows, each represented by a List of InlineKeyboardButton objects. """ - def __init__(self, inline_keyboard: List[List["types.InlineKeyboardButton"]]): + def __init__(self, inline_keyboard: List[List[Union["types.InlineKeyboardButton", "types.InlineKeyboardButtonBuy"]]]): super().__init__() self.inline_keyboard = inline_keyboard diff --git a/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py b/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py index ca630f7c..d949ffbd 100644 --- a/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py +++ b/pyrogram/types/bots_and_keyboards/reply_keyboard_markup.py @@ -96,6 +96,7 @@ class ReplyKeyboardMarkup(Object): placeholder=kb.placeholder ) + # TODO: Implement KeyboardButtonBuy.write method async def write(self, _: "pyrogram.Client"): return raw.types.ReplyKeyboardMarkup( rows=[raw.types.KeyboardButtonRow( diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index ef1a6223..6348f7a6 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -53,6 +53,7 @@ from .web_app_data import WebAppData from .web_page import WebPage from .web_page_empty import WebPageEmpty from .web_page_preview import WebPagePreview +from .message_invoice import MessageInvoice from .message_reactions import MessageReactions from .message_reaction_updated import MessageReactionUpdated from .message_reaction_count_updated import MessageReactionCountUpdated @@ -67,5 +68,5 @@ from .exported_story_link import ExportedStoryLink __all__ = [ "Animation", "Audio", "AvailableEffect", "Contact", "Document", "Game", "GiftedPremium", "Giveaway", "GiveawayLaunched", "GiveawayResult", "Location", "MediaArea", "MediaAreaChannelPost", "MediaAreaCoordinates", "Message", "MessageEntity", "Photo", "Thumbnail", "StrippedThumbnail", "Poll", "PollOption", "Sticker", "StickerSet", "Venue", "Video", "VideoNote", "Voice", "WebPage", "WebPageEmpty", "WebPagePreview", "Dice", - "Reaction", "WebAppData", "MessageReactions", "ReactionCount", "ReactionType", "MessageReactionUpdated", "MessageReactionCountUpdated", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink" + "Reaction", "WebAppData", "MessageInvoice", "MessageReactions", "ReactionCount", "ReactionType", "MessageReactionUpdated", "MessageReactionCountUpdated", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink" ] diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index e9d7d004..3ae28039 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -223,6 +223,9 @@ class Message(Object, Update): giveaway_result (:obj:`~pyrogram.types.GiveawayResult`, *optional*): Message is a giveaway result, information about the giveaway result. + invoice (:obj:`~pyrogram.types.MessageInvoice`, *optional*): + Message is an invoice for a payment, information about the invoice. + story (:obj:`~pyrogram.types.MessageStory` | :obj:`~pyrogram.types.Story`, *optional*): Message is a forwarded story, information about the forwarded story. @@ -472,6 +475,7 @@ class Message(Object, Update): giveaway: "types.Giveaway" = None, giveaway_result: "types.GiveawayResult" = None, boosts_applied: int = None, + invoice: "types.MessageInvoice" = None, story: Union["types.MessageStory", "types.Story"] = None, video: "types.Video" = None, voice: "types.Voice" = None, @@ -581,6 +585,7 @@ class Message(Object, Update): self.giveaway = giveaway self.giveaway_result = giveaway_result self.boosts_applied = boosts_applied + self.invoice = invoice self.story = story self.video = video self.voice = voice @@ -958,6 +963,7 @@ class Message(Object, Update): game = None giveaway = None giveaway_result = None + invoice = None story = None audio = None voice = None @@ -1054,6 +1060,9 @@ class Message(Object, Update): elif isinstance(media, raw.types.MessageMediaDice): dice = types.Dice._parse(client, media) media_type = enums.MessageMediaType.DICE + elif isinstance(media, raw.types.MessageMediaInvoice): + invoice = types.MessageInvoice._parse(media) + media = enums.MessageMediaType.INVOICE else: media = None @@ -1137,6 +1146,7 @@ class Message(Object, Update): game=game, giveaway=giveaway, giveaway_result=giveaway_result, + invoice=invoice, story=story, video=video, video_note=video_note, diff --git a/pyrogram/types/messages_and_media/message_invoice.py b/pyrogram/types/messages_and_media/message_invoice.py new file mode 100644 index 00000000..847e2f80 --- /dev/null +++ b/pyrogram/types/messages_and_media/message_invoice.py @@ -0,0 +1,91 @@ +# Pyrofork - Telegram MTProto API Client Library for Python +# Copyright (C) 2022-present Mayuri-Chan +# +# This file is part of Pyrofork. +# +# Pyrofork 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. +# +# Pyrofork 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 Pyrofork. If not, see . + +import pyrogram + +from pyrogram import raw, types, utils +from ..object import Object + + +class MessageInvoice(Object): + """Contains information about an Invoice. + + Parameters: + title (``str``): + Product name. + + description (``str``): + Product description. + + currency (``str``): + Currency code. + + total_amount (``int``): + Total price in the smallest units of the currency. + + start_parameter (``str``): + Unique bot deep-linking parameter that can be used to generate this invoice. + + shipping_address_requested (``bool``, *optional*): + True, if the the shipping address is requested. + + test (``bool``, *optional*): + True, if the invoice is a test invoice. + + receipt_message_id (``int``, *optional*): + The message_id of the message sent to the chat when the invoice is paid. + """ + + def __init__( + self, + *, + title: str, + description : str, + currency: str, + total_amount: int, + start_parameter: str, + shipping_address_requested: bool = None, + test: bool = None, + receipt_message_id: int = None, + # TODO: Implement photo, extended_media parameters + ): + super().__init__() + + self.title = title + self.description = description + self.currency = currency + self.total_amount = total_amount + self.start_parameter = start_parameter + self.shipping_address_requested = shipping_address_requested + self.test = test + self.receipt_message_id = receipt_message_id + + @staticmethod + def _parse( + message_invoice: "raw.types.MessageMediaInvoice" + ) -> "MessageInvoice": + return MessageInvoice( + title=message_invoice.title, + description=message_invoice.description, + currency=message_invoice.currency, + total_amount=message_invoice.total_amount, + start_parameter=message_invoice.start_param, + shipping_address_requested=message_invoice.shipping_address_requested, + test=message_invoice.test, + receipt_message_id=message_invoice.receipt_msg_id + )