Add Invoice type

This commit is contained in:
KurimuzonAkuma 2024-06-01 01:45:31 +03:00
parent a46e4d593b
commit 3566ee12df
6 changed files with 108 additions and 1 deletions

View File

@ -506,6 +506,7 @@ def pyrogram_api():
BoostsStatus BoostsStatus
Giveaway Giveaway
GiveawayResult GiveawayResult
Invoice
GiftCode GiftCode
CheckedGiftCode CheckedGiftCode
SuccessfulPayment SuccessfulPayment

View File

@ -77,3 +77,6 @@ class MessageMediaType(AutoName):
STORY = auto() STORY = auto()
"Story media" "Story media"
INVOICE = auto()
"Invoice media"

View File

@ -161,6 +161,11 @@ class InlineKeyboardButton(Object):
) )
) )
if isinstance(b, raw.types.KeyboardButtonBuy):
return InlineKeyboardButton(
text=b.text
)
async def write(self, client: "pyrogram.Client"): async def write(self, client: "pyrogram.Client"):
if self.callback_data is not None: if self.callback_data is not None:
# Telegram only wants bytes, but we are allowed to pass strings too, for convenience. # Telegram only wants bytes, but we are allowed to pass strings too, for convenience.

View File

@ -34,6 +34,7 @@ from .game import Game
from .general_forum_topic_hidden import GeneralTopicHidden from .general_forum_topic_hidden import GeneralTopicHidden
from .general_forum_topic_unhidden import GeneralTopicUnhidden from .general_forum_topic_unhidden import GeneralTopicUnhidden
from .gift_code import GiftCode from .gift_code import GiftCode
from .invoice import Invoice
from .giveaway import Giveaway from .giveaway import Giveaway
from .giveaway_result import GiveawayResult from .giveaway_result import GiveawayResult
from .location import Location from .location import Location
@ -76,6 +77,7 @@ __all__ = [
"GeneralTopicUnhidden", "GeneralTopicUnhidden",
"GiftCode", "GiftCode",
"Giveaway", "Giveaway",
"Invoice",
"GiveawayResult", "GiveawayResult",
"Location", "Location",
"Message", "Message",

View File

@ -0,0 +1,85 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present 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 typing import Optional
from pyrogram import raw
from ..object import Object
class Invoice(Object):
"""This object contains basic information about an invoice.
Parameters:
title (``str``):
Product name.
description (``str``):
Product description.
start_parameter (``str``):
Unique bot deep-linking parameter that can be used to generate this invoice.
currency (``str``):
Three-letter ISO 4217 `currency <https://core.telegram.org/bots/payments#supported-currencies>`_ code.
total_amount (``int``):
Total price in the smallest units of the currency (integer, **not** float/double). For example, for a price of ``US$ 1.45`` pass ``amount = 145``. See the exp parameter in `currencies.json <https://core.telegram.org/bots/payments/currencies.json>`_, it shows the number of digits past the decimal point for each currency (2 for the majority of currencies).
is_shipping_address_requested (``bool``, *optional*):
True, if the shipping address should be specified.
is_test (``bool``, *optional*):
True, if the invoice is a test invoice.
"""
def __init__(
self,
*,
client: "pyrogram.Client" = None,
title: str,
description: str,
currency: str,
total_amount: int,
start_parameter: Optional[str] = None,
is_shipping_address_requested: Optional[bool] = None,
is_test: Optional[bool] = None
):
super().__init__(client)
self.title = title
self.description = description
self.is_shipping_address_requested = is_shipping_address_requested
self.currency = currency
self.start_parameter = start_parameter
self.total_amount = total_amount
self.is_test = is_test
@staticmethod
def _parse(client, invoice: "raw.types.MessageMediaInvoice") -> "Invoice":
return Invoice(
title=invoice.title,
description=invoice.description,
currency=invoice.currency,
total_amount=invoice.total_amount,
start_parameter=invoice.start_param or None,
is_shipping_address_requested=getattr(invoice, "shipping_address_requested", None),
is_test=getattr(invoice, "test", None),
client=client
# TODO: Add photo and extended media
)

View File

@ -210,6 +210,10 @@ class Message(Object, Update):
giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*): giveaway (:obj:`~pyrogram.types.Giveaway`, *optional*):
Message is a giveaway, information about the giveaway. Message is a giveaway, information about the giveaway.
invoice (:obj:`~pyrogram.types.Invoice`, *optional*):
Message is a invoice, information about the invoice.
`More about payments » <https://core.telegram.org/bots/api#payments>`_
story (:obj:`~pyrogram.types.Story`, *optional*): story (:obj:`~pyrogram.types.Story`, *optional*):
Message is a story, information about the story. Message is a story, information about the story.
@ -401,7 +405,7 @@ class Message(Object, Update):
Generate a link to this message, only for groups and channels. Generate a link to this message, only for groups and channels.
""" """
# TODO: Add game missing field. Also invoice, successful_payment, connected_website # TODO: Add game missing field. Also successful_payment, connected_website
def __init__( def __init__(
self, self,
@ -453,6 +457,7 @@ class Message(Object, Update):
game: "types.Game" = None, game: "types.Game" = None,
giveaway: "types.Giveaway" = None, giveaway: "types.Giveaway" = None,
giveaway_result: "types.GiveawayResult" = None, giveaway_result: "types.GiveawayResult" = None,
invoice: "types.Invoice" = None,
story: "types.Story" = None, story: "types.Story" = None,
video: "types.Video" = None, video: "types.Video" = None,
voice: "types.Voice" = None, voice: "types.Voice" = None,
@ -560,6 +565,7 @@ class Message(Object, Update):
self.game = game self.game = game
self.giveaway = giveaway self.giveaway = giveaway
self.giveaway_result = giveaway_result self.giveaway_result = giveaway_result
self.invoice = invoice
self.story = story self.story = story
self.video = video self.video = video
self.voice = voice self.voice = voice
@ -900,6 +906,7 @@ class Message(Object, Update):
game = None game = None
giveaway = None giveaway = None
giveaway_result = None giveaway_result = None
invoice = None
story = None story = None
audio = None audio = None
voice = None voice = None
@ -939,6 +946,9 @@ class Message(Object, Update):
elif isinstance(media, raw.types.MessageMediaGiveawayResults): elif isinstance(media, raw.types.MessageMediaGiveawayResults):
giveaway_result = await types.GiveawayResult._parse(client, media, users, chats) giveaway_result = await types.GiveawayResult._parse(client, media, users, chats)
media_type = enums.MessageMediaType.GIVEAWAY_RESULT media_type = enums.MessageMediaType.GIVEAWAY_RESULT
elif isinstance(media, raw.types.MessageMediaInvoice):
invoice = types.Invoice._parse(client, media)
media_type = enums.MessageMediaType.INVOICE
elif isinstance(media, raw.types.MessageMediaStory): elif isinstance(media, raw.types.MessageMediaStory):
if media.story: if media.story:
story = await types.Story._parse(client, media.story, users, chats, media.peer) story = await types.Story._parse(client, media.story, users, chats, media.peer)
@ -1087,6 +1097,7 @@ class Message(Object, Update):
game=game, game=game,
giveaway=giveaway, giveaway=giveaway,
giveaway_result=giveaway_result, giveaway_result=giveaway_result,
invoice=invoice,
story=story, story=story,
video=video, video=video,
video_note=video_note, video_note=video_note,