Add new check_gift_code method

This commit is contained in:
KurimuzonAkuma 2024-04-05 12:11:20 +03:00
parent a9ac50caed
commit e31edb62b8
6 changed files with 192 additions and 1 deletions

View File

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

View File

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

View File

@ -0,0 +1,24 @@
# 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 .check_giftcode import CheckGiftCode
class Payments(
CheckGiftCode
):
pass

View File

@ -0,0 +1,67 @@
# 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/>.
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)

View File

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

View File

@ -0,0 +1,92 @@
# 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 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,
)