Bot API 8.0: Gifts by Bibo-Joshi · Pull Request #4576 · python-telegram-bot/python-telegram-bot · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/inclusions/bot_methods.rst
1 change: 0 additions & 1 deletion docs/source/telegram.at-tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ Available Types
telegram.inputpaidmediaphoto
telegram.inputpaidmediavideo
telegram.inputpolloption
telegram.inputsticker
telegram.keyboardbutton
telegram.keyboardbuttonpolltype
telegram.keyboardbuttonrequestchat
Expand Down
6 changes: 6 additions & 0 deletions docs/source/telegram.gift.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Gift
====

.. autoclass:: telegram.Gift
:members:
:show-inheritance:
6 changes: 6 additions & 0 deletions docs/source/telegram.gifts.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Gifts
=====

.. autoclass:: telegram.Gifts
:members:
:show-inheritance:
3 changes: 3 additions & 0 deletions docs/source/telegram.stickers-tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The following methods and objects allow your bot to handle stickers and sticker
.. toctree::
:titlesonly:

telegram.gift
telegram.gifts
telegram.inputsticker
telegram.maskposition
telegram.sticker
telegram.stickerset
3 changes: 3 additions & 0 deletions telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
"GameHighScore",
"GeneralForumTopicHidden",
"GeneralForumTopicUnhidden",
"Gift",
"Gifts",
"Giveaway",
"GiveawayCompleted",
"GiveawayCreated",
Expand Down Expand Up @@ -373,6 +375,7 @@
from ._games.callbackgame import CallbackGame
from ._games.game import Game
from ._games.gamehighscore import GameHighScore
from ._gifts import Gift, Gifts
from ._giveaway import Giveaway, GiveawayCompleted, GiveawayCreated, GiveawayWinners
from ._inline.inlinekeyboardbutton import InlineKeyboardButton
from ._inline.inlinekeyboardmarkup import InlineKeyboardMarkup
Expand Down
98 changes: 98 additions & 0 deletions telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
from telegram._files.voice import Voice
from telegram._forumtopic import ForumTopic
from telegram._games.gamehighscore import GameHighScore
from telegram._gifts import Gift, Gifts
from telegram._inline.inlinequeryresultsbutton import InlineQueryResultsButton
from telegram._menubutton import MenuButton
from telegram._message import Message
Expand Down Expand Up @@ -9475,6 +9476,99 @@ async def edit_chat_subscription_invite_link(

return ChatInviteLink.de_json(result, self) # type: ignore[return-value]

async def get_available_gifts(
self,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> Gifts:
"""Returns the list of gifts that can be sent by the bot to users.
Requires no parameters.

.. versionadded:: NEXT.VERSION

Returns:
:class:`telegram.Gifts`

Raises:
:class:`telegram.error.TelegramError`
"""
return Gifts.de_json( # type: ignore[return-value]
await self._post(
"getAvailableGifts",
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)
)

async def send_gift(
self,
user_id: int,
gift_id: Union[str, Gift],
text: Optional[str] = None,
text_parse_mode: ODVInput[str] = DEFAULT_NONE,
text_entities: Optional[Sequence["MessageEntity"]] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Sends a gift to the given user.
The gift can't be converted to Telegram Stars by the user

.. versionadded:: NEXT.VERSION

Args:
user_id (:obj:`int`): Unique identifier of the target user that will receive the gift
gift_id (:obj:`str` | :class:`~telegram.Gift`): Identifier of the gift or a
:class:`~telegram.Gift` object
text (:obj:`str`, optional): Text that will be shown along with the gift;
0- :tg-const:`telegram.constants.GiftLimit.MAX_TEXT_LENGTH` characters
text_parse_mode (:obj:`str`, optional): Mode for parsing entities.
See :class:`telegram.constants.ParseMode` and
`formatting options <https://core.telegram.org/bots/api#formatting-options>`__ for
more details. Entities other than :attr:`~MessageEntity.BOLD`,
:attr:`~MessageEntity.ITALIC`, :attr:`~MessageEntity.UNDERLINE`,
:attr:`~MessageEntity.STRIKETHROUGH`, :attr:`~MessageEntity.SPOILER`, and
:attr:`~MessageEntity.CUSTOM_EMOJI` are ignored.
text_entities (Sequence[:class:`telegram.MessageEntity`], optional): A list of special
entities that appear in the gift text. It can be specified instead of
:paramref:`text_parse_mode`. Entities other than :attr:`~MessageEntity.BOLD`,
:attr:`~MessageEntity.ITALIC`, :attr:`~MessageEntity.UNDERLINE`,
:attr:`~MessageEntity.STRIKETHROUGH`, :attr:`~MessageEntity.SPOILER`, and
:attr:`~MessageEntity.CUSTOM_EMOJI` are ignored.

Returns:
:obj:`bool`: On success, :obj:`True` is returned.

Raises:
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {
"user_id": user_id,
"gift_id": gift_id.id if isinstance(gift_id, Gift) else gift_id,
"text": text,
"text_parse_mode": text_parse_mode,
"text_entities": text_entities,
}
return await self._post(
"sendGift",
data,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

def to_dict(self, recursive: bool = True) -> JSONDict: # noqa: ARG002
"""See :meth:`telegram.TelegramObject.to_dict`."""
data: JSONDict = {"id": self.id, "username": self.username, "first_name": self.first_name}
Expand Down Expand Up @@ -9735,3 +9829,7 @@ def to_dict(self, recursive: bool = True) -> JSONDict: # noqa: ARG002
"""Alias for :meth:`create_chat_subscription_invite_link`"""
editChatSubscriptionInviteLink = edit_chat_subscription_invite_link
"""Alias for :meth:`edit_chat_subscription_invite_link`"""
getAvailableGifts = get_available_gifts
"""Alias for :meth:`get_available_gifts`"""
sendGift = send_gift
"""Alias for :meth:`send_gift`"""
41 changes: 41 additions & 0 deletions telegram/_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
ChatMember,
Contact,
Document,
Gift,
InlineKeyboardMarkup,
InputMediaAudio,
InputMediaDocument,
Expand Down Expand Up @@ -3436,6 +3437,46 @@ async def send_paid_media(
allow_paid_broadcast=allow_paid_broadcast,
)

async def send_gift(
self,
gift_id: Union[str, "Gift"],
text: Optional[str] = None,
text_parse_mode: ODVInput[str] = DEFAULT_NONE,
text_entities: Optional[Sequence["MessageEntity"]] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::

await bot.send_gift(user_id=update.effective_chat.id, *args, **kwargs )

For the documentation of the arguments, please see :meth:`telegram.Bot.send_gift`.

Caution:
Can only work, if the chat is a private chat, see :attr:`type`.

.. versionadded:: NEXT.VERSION

Returns:
:obj:`bool`: On success, :obj:`True` is returned.
"""
return await self.get_bot().send_gift(
user_id=self.id,
gift_id=gift_id,
text=text,
text_parse_mode=text_parse_mode,
text_entities=text_entities,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)


class Chat(_ChatBase):
"""This object represents a chat.
Expand Down
136 changes: 136 additions & 0 deletions telegram/_gifts.py
Loading