From e31edb62b86709676f6ec49c575e9cdd187eedeb Mon Sep 17 00:00:00 2001 From: KurimuzonAkuma Date: Fri, 5 Apr 2024 12:11:20 +0300 Subject: [PATCH] Add new check_gift_code method --- compiler/docs/compiler.py | 5 + pyrogram/methods/__init__.py | 2 + pyrogram/methods/payments/__init__.py | 24 +++++ pyrogram/methods/payments/check_giftcode.py | 67 ++++++++++++++ pyrogram/types/messages_and_media/__init__.py | 3 +- .../messages_and_media/checked_gift_code.py | 92 +++++++++++++++++++ 6 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 pyrogram/methods/payments/__init__.py create mode 100644 pyrogram/methods/payments/check_giftcode.py create mode 100644 pyrogram/types/messages_and_media/checked_gift_code.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 2e8dcd2b..2029ade7 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -309,6 +309,10 @@ def pyrogram_api(): get_contacts get_contacts_count """, + payments=""" + Payments + check_gift_code + """, password=""" Password enable_cloud_password @@ -486,6 +490,7 @@ def pyrogram_api(): Giveaway GiveawayResult GiftCode + CheckedGiftCode """, bot_keyboards=""" Bot keyboards diff --git a/pyrogram/methods/__init__.py b/pyrogram/methods/__init__.py index f3d164b5..ce7fc1ec 100644 --- a/pyrogram/methods/__init__.py +++ b/pyrogram/methods/__init__.py @@ -25,6 +25,7 @@ from .decorators import Decorators from .invite_links import InviteLinks from .messages import Messages from .password import Password +from .payments import Payments from .premium import Premium from .users import Users from .stories import Stories @@ -37,6 +38,7 @@ class Methods( Bots, Contacts, Password, + Payments, Premium, Chats, Users, diff --git a/pyrogram/methods/payments/__init__.py b/pyrogram/methods/payments/__init__.py new file mode 100644 index 00000000..d45ec785 --- /dev/null +++ b/pyrogram/methods/payments/__init__.py @@ -0,0 +1,24 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present 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 .check_giftcode import CheckGiftCode + +class Payments( + CheckGiftCode +): + pass diff --git a/pyrogram/methods/payments/check_giftcode.py b/pyrogram/methods/payments/check_giftcode.py new file mode 100644 index 00000000..61359591 --- /dev/null +++ b/pyrogram/methods/payments/check_giftcode.py @@ -0,0 +1,67 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present 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 . +import re + +import pyrogram +from pyrogram import raw, types + + +class CheckGiftCode: + async def check_gift_code( + self: "pyrogram.Client", + link: str, + ) -> "types.CheckedGiftCode": + """Get information about a gift code. + + .. include:: /_includes/usable-by/users.rst + + Parameters: + link (``str``): + The gift code link. + + Returns: + :obj:`~pyrogram.types.CheckedGiftCode`: On success, a checked gift code is returned. + + Raises: + ValueError: In case the folder invite link is invalid. + + Example: + .. code-block:: python + + # get information about a gift code + app.check_gift_code("t.me/giftcode/abc1234567def") + """ + match = re.match(r"^(?:https?://)?(?:www\.)?(?:t(?:elegram)?\.(?:org|me|dog)/(?:giftcode/|\+))([\w-]+)$", link) + + if match: + slug = match.group(1) + elif isinstance(link, str): + slug = link + else: + raise ValueError("Invalid gift code link") + + r = await self.invoke( + raw.functions.payments.CheckGiftCode( + slug=slug + ) + ) + + users = {i.id: i for i in r.users} + chats = {i.id: i for i in r.chats} + + return types.CheckedGiftCode._parse(self, r, users, chats) diff --git a/pyrogram/types/messages_and_media/__init__.py b/pyrogram/types/messages_and_media/__init__.py index 73cd07dc..4c90c5bb 100644 --- a/pyrogram/types/messages_and_media/__init__.py +++ b/pyrogram/types/messages_and_media/__init__.py @@ -19,6 +19,7 @@ from .animation import Animation from .audio import Audio from .boosts_status import BoostsStatus +from .checked_gift_code import CheckedGiftCode from .contact import Contact from .dice import Dice from .document import Document @@ -54,7 +55,7 @@ from .message_reactions import MessageReactions from .my_boost import MyBoost __all__ = [ - "Animation", "Audio", "BoostsStatus", "Contact", "Document", "ForumTopic", "ForumTopicCreated", + "Animation", "Audio", "BoostsStatus", "CheckedGiftCode", "Contact", "Document", "ForumTopic", "ForumTopicCreated", "ForumTopicClosed", "ForumTopicReopened", "ForumTopicEdited", "GeneralTopicHidden", "GeneralTopicUnhidden", "Game", "GiftCode", "Giveaway", "GiveawayResult", "Location", "Message", "MessageEntity", "Photo", "Thumbnail", "StrippedThumbnail", "Story", "Poll", diff --git a/pyrogram/types/messages_and_media/checked_gift_code.py b/pyrogram/types/messages_and_media/checked_gift_code.py new file mode 100644 index 00000000..7900c39f --- /dev/null +++ b/pyrogram/types/messages_and_media/checked_gift_code.py @@ -0,0 +1,92 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present 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 datetime import datetime + +from pyrogram import raw, types, utils +from ..object import Object + + +class CheckedGiftCode(Object): + """Contains checked gift code data. + + Parameters: + date (:py:obj:`~datetime.datetime`): + Date when the giveaway was launched. + + months (``int``): + Number of months of subscription. + + via_giveaway (``bool``, *optional*): + True if the gift code is received via giveaway. + + from_chat (:obj:`~pyrogram.types.Chat`, *optional*): + The channel where the gift code was won. + + winner (:obj:`~pyrogram.types.User`, *optional*): + The user who won the giveaway. + + giveaway_message_id (``int``, *optional*): + Identifier of the message from chat where the giveaway was launched. + + used_date (:py:obj:`~datetime.datetime`, *optional*): + Date when the gift code was used. + """ + + def __init__( + self, + *, + date: datetime, + months: int, + via_giveaway: bool = None, + from_chat: "types.Chat" = None, + winner: "types.User" = None, + giveaway_message_id: int = None, + used_date: datetime = None + ): + super().__init__() + + self.date = date + self.months = months + self.via_giveaway = via_giveaway + self.from_chat = from_chat + self.winner = winner + self.giveaway_message_id = giveaway_message_id + self.used_date = used_date + + @staticmethod + def _parse(client, checked_gift_code: "raw.types.payments.CheckedGiftCode", users, chats): + from_chat = None + winner = None + + if getattr(checked_gift_code, "from_id", None): + from_chat = types.Chat._parse_chat( + client, chats.get(utils.get_raw_peer_id(checked_gift_code.from_id)) + ) + if getattr(checked_gift_code, "to_id", None): + winner = types.User._parse(client, users.get(checked_gift_code.to_id)) + + return CheckedGiftCode( + date=utils.timestamp_to_datetime(checked_gift_code.date), + months=checked_gift_code.months, + via_giveaway=getattr(checked_gift_code, "via_giveaway", None), + from_chat=from_chat, + winner=winner, + giveaway_message_id=getattr(checked_gift_code, "giveaway_msg_id", None), + used_date=utils.timestamp_to_datetime(checked_gift_code.used_date) if getattr(checked_gift_code, "used_date") else None, + )