Skip to content

Commit

Permalink
Add UpgradedGift and change return type of some methods. (#134)
Browse files Browse the repository at this point in the history
Closes #133
  • Loading branch information
SpEcHiDe authored Jan 5, 2025
1 parent a7d7f99 commit 4bc566e
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 36 deletions.
1 change: 1 addition & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ def get_title_list(s: str) -> list:
Venue
Gift
UserGift
UpgradedGift
WebAppData
MessageAutoDeleteTimerChanged
ChatBoostAdded
Expand Down
1 change: 1 addition & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Changes in this Fork
| Scheme layer used: 196 |
+------------------------+

- Added the :obj:`~pyrogram.types.UpgradedGift` and changed return type :meth:`~pyrogram.Client.get_available_gifts` and :meth:`~pyrogram.Client.get_user_gifts`.
- Added the ``pay_for_upgrade`` in the :meth:`~pyrogram.Client.send_gift`.
- Added the parameters ``upgrade_star_count`` and ``is_for_birthday`` in :obj:`~pyrogram.types.Gift`.
- Added the :meth:`~pyrogram.Client.on_bot_purchased_paid_media` and :meth:`~pyrogram.Client.on_bot_business_connection`.
Expand Down
6 changes: 4 additions & 2 deletions pyrogram/methods/business/get_available_gifts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,22 @@
# 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 Union

import pyrogram
from pyrogram import raw, types


class GetAvailableGifts:
async def get_available_gifts(
self: "pyrogram.Client",
) -> list["types.Gift"]:
) -> list[Union["types.Gift", "types.UpgradedGift"]]:
"""Get all gifts that can be sent to other users.
.. include:: /_includes/usable-by/users-bots.rst
Returns:
List of :obj:`~pyrogram.types.Gift`: On success, a list of star gifts is returned.
List of :obj:`~pyrogram.types.Gift` | :obj:`~pyrogram.types.UpgradedGift`: On success, a list of star gifts is returned.
Example:
.. code-block:: python
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/types/messages_and_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
from .sponsored_message import SponsoredMessage
from .gift import Gift
from .user_gift import UserGift
from .upgraded_gift import UpgradedGift
from .sticker import Sticker
from .stripped_thumbnail import StrippedThumbnail
from .thumbnail import Thumbnail
Expand Down Expand Up @@ -111,6 +112,7 @@
"SponsoredMessage",
"Gift",
"UserGift",
"UpgradedGift",
"Sticker",
"Story",
"Venue",
Expand Down
9 changes: 6 additions & 3 deletions pyrogram/types/messages_and_media/gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from datetime import datetime
from typing import Optional
from typing import Optional, Union

import pyrogram
from pyrogram import raw, types, utils
Expand Down Expand Up @@ -101,8 +101,11 @@ def __init__(
@staticmethod
async def _parse(
client,
star_gift: "raw.types.StarGift",
) -> "Gift":
star_gift: "raw.base.StarGift",
) -> Union["Gift", "types.UpgradedGift"]:
if isinstance(star_gift, raw.types.StarGiftUnique):
return types.UpgradedGift._parse(client, star_gift)

doc = star_gift.sticker
attributes = {type(i): i for i in doc.attributes}

Expand Down
87 changes: 87 additions & 0 deletions pyrogram/types/messages_and_media/upgraded_gift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 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

import pyrogram
from pyrogram import raw, utils
from ..object import Object


class UpgradedGift(Object):
"""Describes an upgraded gift that can be gifted to another user or transferred to TON blockchain as an NFT.
Parameters:
id (``int``):
Unique identifier of the gift.
title (``str``):
The title of the upgraded gift.
number (``int``):
Unique number of the upgraded gift among gifts upgraded from the same gift.
total_upgraded_count (``int``):
Total number of gifts that were upgraded from the same gift.
max_upgraded_count (``int``):
The maximum number of gifts that can be upgraded from the same gift.
owner_user_id (``int``, *optional*):
User identifier of the user that owns the upgraded gift.
"""

def __init__(
self,
*,
client: "pyrogram.Client" = None,
id: int,
title: str,
number: int,
total_upgraded_count: int,
max_upgraded_count: int,
owner_user_id: Optional[int] = None,
_raw: "raw.types.StarGiftUnique" = None,
):
super().__init__(client)

self.id = id
self.title = title
self.number = number
self.total_upgraded_count = total_upgraded_count
self.max_upgraded_count = max_upgraded_count
self.owner_user_id = owner_user_id
self._raw = _raw # TODO


@staticmethod
def _parse(
client,
star_gift: "raw.types.StarGiftUnique"
) -> "UpgradedGift":
return UpgradedGift(
id=star_gift.id,
title=star_gift.title,
number=star_gift.num,
total_upgraded_count=star_gift.availability_issued,
max_upgraded_count=star_gift.availability_total,
owner_user_id=utils.get_raw_peer_id(getattr(star_gift, "owner_id", None)),
_raw=star_gift,
client=client
)
36 changes: 5 additions & 31 deletions pyrogram/types/messages_and_media/user_gift.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from datetime import datetime
from typing import Optional
from typing import Optional, Union

import pyrogram
from pyrogram import raw, types, utils
Expand Down Expand Up @@ -47,7 +47,7 @@ class UserGift(Object):
date (:py:obj:`~datetime.datetime`, *optional*):
Date when the gift was sent.
gift (:obj:`~pyrogram.types.Gift`, *optional*):
gift (:obj:`~pyrogram.types.Gift` | :obj:`~pyrogram.types.UpgradedGift`, *optional*):
Information about the gift.
message_id (``int``, *optional*):
Expand Down Expand Up @@ -89,7 +89,7 @@ def __init__(
date: datetime,
is_private: Optional[bool] = None,
is_saved: Optional[bool] = None,
gift: Optional["types.Gift"] = None,
gift: Optional[Union["types.Gift", "types.UpgradedGift"]] = None,
message_id: Optional[int] = None,
sell_star_count: Optional[int] = None,
was_converted: Optional[bool] = None,
Expand Down Expand Up @@ -153,9 +153,6 @@ async def _parse_action(
) -> "UserGift":
action = message.action

doc = action.gift.sticker
attributes = {type(i): i for i in doc.attributes}

text, entities = None, None
if getattr(action, "message", None):
text = action.message.text or None
Expand All @@ -164,17 +161,7 @@ async def _parse_action(

if isinstance(action, raw.types.MessageActionStarGift):
return UserGift(
gift=types.Gift(
id=action.gift.id,
sticker=await types.Sticker._parse(client, doc, attributes),
star_count=action.gift.stars,
total_count=getattr(action.gift, "availability_total", None),
remaining_count=getattr(action.gift, "availability_remains", None),
default_sell_star_count=action.gift.convert_stars,
is_limited=getattr(action.gift, "limited", None),upgrade_star_count=getattr(star_gift, "upgrade_stars", 0),
is_for_birthday=getattr(star_gift, "birthday", None),
client=client
),
gift=await types.Gift._parse(client, action.gift),
date=utils.timestamp_to_datetime(message.date),
is_private=getattr(action, "name_hidden", None),
is_saved=getattr(action, "saved", None),
Expand All @@ -192,18 +179,7 @@ async def _parse_action(

if isinstance(action, raw.types.MessageActionStarGiftUnique):
return UserGift(
gift=types.Gift(
id=action.gift.id,
sticker=await types.Sticker._parse(client, doc, attributes),
star_count=action.gift.stars,
total_count=getattr(action.gift, "availability_total", None),
remaining_count=getattr(action.gift, "availability_remains", None),
default_sell_star_count=action.gift.convert_stars,
is_limited=getattr(action.gift, "limited", None),
upgrade_star_count=getattr(star_gift, "upgrade_stars", 0),
is_for_birthday=getattr(star_gift, "birthday", None),
client=client
),
gift=await types.Gift._parse(client, action.gift),
date=utils.timestamp_to_datetime(message.date),
sender_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.peer_id))),
message_id=message.id,
Expand All @@ -225,7 +201,6 @@ async def toggle(self, is_saved: bool) -> bool:
.. code-block:: python
await client.toggle_gift_is_saved(
sender_user_id=user_id,
message_id=message_id
)
Expand All @@ -243,7 +218,6 @@ async def toggle(self, is_saved: bool) -> bool:
"""
return await self._client.toggle_gift_is_saved(
sender_user_id=self.sender_user.id,
message_id=self.message_id,
is_saved=is_saved
)

0 comments on commit 4bc566e

Please sign in to comment.