pyrofork: add InlineKeyboardButtonBuy and MessageInvoice

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2024-06-01 23:14:52 +07:00
parent fac571aff9
commit 59461adc55
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
10 changed files with 161 additions and 3 deletions

View file

@ -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

View file

@ -78,3 +78,6 @@ class MessageMediaType(AutoName):
STORY = auto()
"Forwarded story media"
INVOICE = auto()
"Invoice media"

View file

@ -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",

View file

@ -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.

View file

@ -0,0 +1,45 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/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 <http://www.gnu.org/licenses/>.
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

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.
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

View file

@ -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(

View file

@ -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"
]

View file

@ -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,

View file

@ -0,0 +1,91 @@
# Pyrofork - Telegram MTProto API Client Library for Python
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/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 <http://www.gnu.org/licenses/>.
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
)