Skip to content

Commit

Permalink
pyrofork: Add send_invoice method
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Jun 8, 2024
1 parent dcbbbb2 commit 374bfe5
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 4 deletions.
2 changes: 2 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def get_title_list(s: str) -> list:
send_photo
send_audio
send_document
send_invoice
send_sticker
send_video
send_animation
Expand Down Expand Up @@ -470,6 +471,7 @@ def get_title_list(s: str) -> list:
AvailableEffect
Document
Animation
LabeledPrice
Video
Voice
VideoNote
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from .send_contact import SendContact
from .send_dice import SendDice
from .send_document import SendDocument
from .send_invoice import SendInvoice
from .send_location import SendLocation
from .send_media_group import SendMediaGroup
from .send_message import SendMessage
Expand Down Expand Up @@ -86,6 +87,7 @@ class Messages(
SendContact,
SendDocument,
SendAnimation,
SendInvoice,
SendLocation,
SendMediaGroup,
SendMessage,
Expand Down
214 changes: 214 additions & 0 deletions pyrogram/methods/messages/send_invoice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
# 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 types, raw, utils
from typing import Union, List

class SendInvoice:
async def send_invoice(
self: "pyrogram.Client",
chat_id: Union[int, str],
title: str,
description: str,
currency: str,
prices: List["types.LabeledPrice"],
is_test: bool = None,
is_name_requested: bool = None,
is_phone_requested: bool = None,
is_email_requested: bool = None,
is_shipping_address_requested: bool = None,
is_flexible: bool = None,
is_recurring: bool = None,
max_tip_amount: int = None,
suggested_tip_amounts: List[int] = None,
terms_url: str = None,
provider: str = None,
provider_data: str = None,
photo_url: str = None,
photo_size: int = None,
photo_mime_type: str = None,
start_parameter: str = None,
extended_media: "types.InputMedia" = None,
reply_to_message_id: int = None,
message_thread_id: int = None,
quote_text: str = None,
quote_entities: List["types.MessageEntity"] = None,
):
"""Use this method to send invoices.
.. include:: /_includes/usable-by/bots.rst
Parameters:
chat_id (``int`` | ``str``):
Unique identifier for the target private chat or username of the target private chat.
title (``str``):
Product name.
description (``str``):
Product description.
currency (``str``):
Three-letter ISO 4217 currency code.
prices (List of :obj:`~pyrogram.types.LabeledPrice`):
Price with label.
is_test (``bool``, *optional*):
True, if the invoice is a test invoice.
is_name_requested (``bool``, *optional*):
True, if the name is requested.
is_phone_requested (``bool``, *optional*):
True, if the phone number is requested.
is_email_requested (``bool``, *optional*):
True, if the email address is requested.
is_shipping_address_requested (``bool``, *optional*):
True, if the shipping address is requested.
is_flexible (``bool``, *optional*):
True, if the final price depends on the shipping method.
is_recurring (``bool``, *optional*):
True, if the payment is recurring.
max_tip_amount (``int``, *optional*):
Maximum amount of the tip.
suggested_tip_amounts (List of ``int``, *optional*):
List of suggested tip amounts.
terms_url (``str``, *optional*):
URL for the terms of service.
provider (``str``, *optional*):
Payment provider.
provider_data (``str``, *optional*):
Provider data in json format.
photo_url (``str``, *optional*):
Photo URL.
photo_size (``int``, *optional*):
Photo size.
photo_mime_type (``str``, *optional*):
Photo MIME type.
start_parameter (``str``, *optional*):
Unique bot deep-linking parameter that can be used to generate this invoice.
extended_media (:obj:`~pyrogram.types.InputMedia`, *optional*):
Additional media.
reply_to_message_id (``int``, *optional*):
If the message is a reply, ID of the original message.
message_thread_id (``int``, *optional*):
Unique identifier for the target message thread (topic) of the forum.
for forum supergroups only.
quote_text (``str``, *optional*):
Text to quote.
for reply_to_message only.
quote_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
List of special entities that appear in quote_text, which can be specified instead of *parse_mode*.
for reply_to_message only.
Returns:
:obj:`~pyrogram.types.Message`: On success, the sent message is returned.
Example:
.. code-block:: python
app.send_invoice(chat_id, types.InputMediaInvoice(
title="Product Name",
description="Product Description",
currency="USD",
prices=[types.LabeledPrice("Product", 1000)],
provider="Stripe",
provider_data="{}"
))
"""

reply_to = await utils.get_reply_to(
client=self,
chat_id=chat_id,
reply_to_message_id=reply_to_message_id,
message_thread_id=message_thread_id,
quote_text=quote_text,
quote_entities=quote_entities
)

r = await self.invoke(
raw.functions.messages.SendMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaInvoice(
title=title,
description=description,
invoice=raw.types.Invoice(
currency=currency,
prices=[price.write() for price in prices],
test=is_test,
name_requested=is_name_requested,
phone_requested=is_phone_requested,
email_requested=is_email_requested,
shipping_address_requested=is_shipping_address_requested,
flexible=is_flexible,
recurring=is_recurring,
max_tip_amount=max_tip_amount,
suggested_tip_amounts=suggested_tip_amounts,
terms_url=terms_url
),
payload=f"{(title)}".encode(),
provider=provider,
provider_data=raw.types.DataJSON(data=provider_data if provider_data else "{}"),
photo=raw.types.InputWebDocument(
url=photo_url,
size=photo_size or 0,
mime_type=photo_mime_type or "image/jpeg",
attributes=[]
) if photo_url else None,
start_param=start_parameter,
extended_media=extended_media
),
random_id=self.rnd_id(),
reply_to=reply_to,
message=""
)
)

for i in r.updates:
if isinstance(
i,
(
raw.types.UpdateNewMessage,
raw.types.UpdateNewChannelMessage
)
):
return types.Message._parse(
self,
i,
users={i.id: i for i in r.users},
chats={i.id: i for i in r.chats}
)
3 changes: 2 additions & 1 deletion pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .giveaway import Giveaway
from .giveaway_launched import GiveawayLaunched
from .giveaway_result import GiveawayResult
from .labeled_price import LabeledPrice
from .location import Location
from .media_area import MediaArea
from .media_area_channel_post import MediaAreaChannelPost
Expand Down Expand Up @@ -66,7 +67,7 @@
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",
"Animation", "Audio", "AvailableEffect", "Contact", "Document", "Game", "GiftedPremium", "Giveaway", "GiveawayLaunched", "GiveawayResult", "LabeledPrice", "Location", "MediaArea", "MediaAreaChannelPost", "MediaAreaCoordinates", "Message", "MessageEntity", "Photo", "Thumbnail",
"StrippedThumbnail", "Poll", "PollOption", "Sticker", "StickerSet", "Venue", "Video", "VideoNote", "Voice", "WebPage", "WebPageEmpty", "WebPagePreview", "Dice",
"Reaction", "WebAppData", "MessageInvoice", "MessageReactions", "ReactionCount", "ReactionType", "MessageReactionUpdated", "MessageReactionCountUpdated", "MessageStory", "Story", "StoryDeleted", "StorySkipped", "StoryViews", "StoryForwardHeader", "StoriesPrivacyRules", "ExportedStoryLink"
]
45 changes: 45 additions & 0 deletions pyrogram/types/messages_and_media/labeled_price.py
Original file line number Diff line number Diff line change
@@ -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 pyrogram import raw
from ..object import Object

class LabeledPrice(Object):
"""This object represents a price for goods or services.
Parameters:
label (``str``):
Portion label.
amount (``int``):
Price of the product in the smallest units of the currency (integer, not float/double).
"""

def __init__(
self,
label: str,
amount: int
):
self.label = label
self.amount = amount

def write(self):
return raw.types.LabeledPrice(
label=self.label,
amount=self.amount
)
6 changes: 3 additions & 3 deletions pyrogram/types/messages_and_media/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,19 +686,19 @@ async def _parse(
if isinstance(message, raw.types.MessageEmpty):
return Message(id=message.id, empty=True, client=client, raw=message)

from_id = utils.get_raw_peer_id(message.from_id)
from_id = utils.get_raw_peer_id(message.from_id) if "from_id" in message else None
peer_id = utils.get_raw_peer_id(message.peer_id)
user_id = from_id or peer_id

if isinstance(message.from_id, raw.types.PeerUser) and isinstance(message.peer_id, raw.types.PeerUser):
if from_id not in users or peer_id not in users:
if (from_id and from_id not in users) or peer_id not in users:
try:
r = await client.invoke(
raw.functions.users.GetUsers(
id=[
await client.resolve_peer(from_id),
await client.resolve_peer(peer_id)
]
] if from_id else [await client.resolve_peer(peer_id)]
)
)
except PeerIdInvalid:
Expand Down

0 comments on commit 374bfe5

Please sign in to comment.