diff --git a/README.rst b/README.rst index 87dcb730f5e..6fcf3822b5e 100644 --- a/README.rst +++ b/README.rst @@ -11,7 +11,7 @@ :target: https://pypi.org/project/python-telegram-bot/ :alt: Supported Python versions -.. image:: https://img.shields.io/badge/Bot%20API-9.1-blue?logo=telegram +.. image:: https://img.shields.io/badge/Bot%20API-9.2-blue?logo=telegram :target: https://core.telegram.org/bots/api-changelog :alt: Supported Bot API version @@ -81,7 +81,7 @@ After installing_ the library, be sure to check out the section on `working with Telegram API support ~~~~~~~~~~~~~~~~~~~~ -All types and methods of the Telegram Bot API **9.1** are natively supported by this library. +All types and methods of the Telegram Bot API **9.2** are natively supported by this library. In addition, Bot API functionality not yet natively included can still be used as described `in our wiki `_. Notable Features diff --git a/changes/unreleased/4911.kiF45Y4cfPGMq5cuLpa5da.toml b/changes/unreleased/4911.kiF45Y4cfPGMq5cuLpa5da.toml new file mode 100644 index 00000000000..9b7a7cc4080 --- /dev/null +++ b/changes/unreleased/4911.kiF45Y4cfPGMq5cuLpa5da.toml @@ -0,0 +1,14 @@ +features = "Full Support for Bot API 9.2" + +pull_requests = [ + { uid = "4911", author_uid = "aelkheir", closes_threads = ["4910"] }, + { uid = "4918", author_uid = "Poolitzer" }, + { uid = "4917", author_uid = "Poolitzer" }, + { uid = "4914", author_uid = "harshil21"}, + { uid = "4916", author_uid = "harshil21"}, + { uid = "4912", author_uid = "aelkheir" }, + { uid = "4921", author_uid = "aelkheir" }, + { uid = "4936", author_uid = "Bibo-Joshi" }, + { uid = "4935", author_uid = "Bibo-Joshi" }, + { uid = "4931", author_uid = "aelkheir" }, +] diff --git a/docs/source/inclusions/bot_methods.rst b/docs/source/inclusions/bot_methods.rst index 470fcb17ff6..128fa26ac5c 100644 --- a/docs/source/inclusions/bot_methods.rst +++ b/docs/source/inclusions/bot_methods.rst @@ -121,6 +121,10 @@ - Used for approving a chat join request * - :meth:`~telegram.Bot.decline_chat_join_request` - Used for declining a chat join request + * - :meth:`~telegram.Bot.approve_suggested_post` + - Used for approving a suggested post + * - :meth:`~telegram.Bot.decline_suggested_post` + - Used for declining a suggested post * - :meth:`~telegram.Bot.ban_chat_member` - Used for banning a member from the chat * - :meth:`~telegram.Bot.unban_chat_member` diff --git a/docs/source/telegram.at-tree.rst b/docs/source/telegram.at-tree.rst index acfaf866f46..f04e35df648 100644 --- a/docs/source/telegram.at-tree.rst +++ b/docs/source/telegram.at-tree.rst @@ -71,6 +71,7 @@ Available Types telegram.contact telegram.dice telegram.directmessagepricechanged + telegram.directmessagestopic telegram.document telegram.externalreplyinfo telegram.file @@ -168,6 +169,14 @@ Available Types telegram.storyareatypesuggestedreaction telegram.storyareatypeuniquegift telegram.storyareatypeweather + telegram.suggestedpostapprovalfailed + telegram.suggestedpostapproved + telegram.suggestedpostdeclined + telegram.suggestedpostinfo + telegram.suggestedpostpaid + telegram.suggestedpostparameters + telegram.suggestedpostprice + telegram.suggestedpostrefunded telegram.switchinlinequerychosenchat telegram.telegramobject telegram.textquote diff --git a/docs/source/telegram.directmessagestopic.rst b/docs/source/telegram.directmessagestopic.rst new file mode 100644 index 00000000000..9779a021c91 --- /dev/null +++ b/docs/source/telegram.directmessagestopic.rst @@ -0,0 +1,6 @@ +DirectMessagesTopic +=================== + +.. autoclass:: telegram.DirectMessagesTopic + :members: + :show-inheritance: \ No newline at end of file diff --git a/docs/source/telegram.suggestedpostapprovalfailed.rst b/docs/source/telegram.suggestedpostapprovalfailed.rst new file mode 100644 index 00000000000..5b730f18583 --- /dev/null +++ b/docs/source/telegram.suggestedpostapprovalfailed.rst @@ -0,0 +1,6 @@ +SuggestedPostApprovalFailed +=========================== + +.. autoclass:: telegram.SuggestedPostApprovalFailed + :members: + :show-inheritance: diff --git a/docs/source/telegram.suggestedpostapproved.rst b/docs/source/telegram.suggestedpostapproved.rst new file mode 100644 index 00000000000..c9e74a94652 --- /dev/null +++ b/docs/source/telegram.suggestedpostapproved.rst @@ -0,0 +1,6 @@ +SuggestedPostApproved +===================== + +.. autoclass:: telegram.SuggestedPostApproved + :members: + :show-inheritance: diff --git a/docs/source/telegram.suggestedpostdeclined.rst b/docs/source/telegram.suggestedpostdeclined.rst new file mode 100644 index 00000000000..bf9194d074b --- /dev/null +++ b/docs/source/telegram.suggestedpostdeclined.rst @@ -0,0 +1,6 @@ +SuggestedPostDeclined +===================== + +.. autoclass:: telegram.SuggestedPostDeclined + :members: + :show-inheritance: diff --git a/docs/source/telegram.suggestedpostinfo.rst b/docs/source/telegram.suggestedpostinfo.rst new file mode 100644 index 00000000000..a974dda9887 --- /dev/null +++ b/docs/source/telegram.suggestedpostinfo.rst @@ -0,0 +1,6 @@ +SuggestedPostInfo +================= + +.. autoclass:: telegram.SuggestedPostInfo + :members: + :show-inheritance: diff --git a/docs/source/telegram.suggestedpostpaid.rst b/docs/source/telegram.suggestedpostpaid.rst new file mode 100644 index 00000000000..6eb4a57bbda --- /dev/null +++ b/docs/source/telegram.suggestedpostpaid.rst @@ -0,0 +1,6 @@ +SuggestedPostPaid +================= + +.. autoclass:: telegram.SuggestedPostPaid + :members: + :show-inheritance: diff --git a/docs/source/telegram.suggestedpostparameters.rst b/docs/source/telegram.suggestedpostparameters.rst new file mode 100644 index 00000000000..5111d8fdd48 --- /dev/null +++ b/docs/source/telegram.suggestedpostparameters.rst @@ -0,0 +1,6 @@ +SuggestedPostParameters +======================= + +.. autoclass:: telegram.SuggestedPostParameters + :members: + :show-inheritance: diff --git a/docs/source/telegram.suggestedpostprice.rst b/docs/source/telegram.suggestedpostprice.rst new file mode 100644 index 00000000000..f5034e8f047 --- /dev/null +++ b/docs/source/telegram.suggestedpostprice.rst @@ -0,0 +1,6 @@ +SuggestedPostPrice +================== + +.. autoclass:: telegram.SuggestedPostPrice + :members: + :show-inheritance: diff --git a/docs/source/telegram.suggestedpostrefunded.rst b/docs/source/telegram.suggestedpostrefunded.rst new file mode 100644 index 00000000000..2fb5ad1beff --- /dev/null +++ b/docs/source/telegram.suggestedpostrefunded.rst @@ -0,0 +1,6 @@ +SuggestedPostRefunded +===================== + +.. autoclass:: telegram.SuggestedPostRefunded + :members: + :show-inheritance: diff --git a/docs/substitutions/global.rst b/docs/substitutions/global.rst index c161278591a..fe7b976f5b7 100644 --- a/docs/substitutions/global.rst +++ b/docs/substitutions/global.rst @@ -96,6 +96,10 @@ .. |allow_paid_broadcast| replace:: Pass True to allow up to :tg-const:`telegram.constants.FloodLimit.PAID_MESSAGES_PER_SECOND` messages per second, ignoring `broadcasting limits `__ for a fee of 0.1 Telegram Stars per message. The relevant Stars will be withdrawn from the bot's balance. +.. |direct_messages_topic_id| replace:: Identifier of the direct messages topic to which the message will be sent; required if the message is sent to a direct messages chat. + +.. |suggested_post_parameters| replace:: An object containing the parameters of the suggested post to send; for direct messages chats only. If the message is sent as a reply to another suggested post, then that suggested post is automatically declined. + .. |tz-naive-dtms| replace:: For timezone naive :obj:`datetime.datetime` objects, the default timezone of the bot will be used, which is UTC unless :attr:`telegram.ext.Defaults.tzinfo` is used. .. |org-verify| replace:: `on behalf of the organization `__ diff --git a/src/telegram/__init__.py b/src/telegram/__init__.py index b0277a7e77a..0d77c81eeba 100644 --- a/src/telegram/__init__.py +++ b/src/telegram/__init__.py @@ -93,6 +93,7 @@ "DataCredentials", "Dice", "DirectMessagePriceChanged", + "DirectMessagesTopic", "Document", "EncryptedCredentials", "EncryptedPassportElement", @@ -264,6 +265,14 @@ "StoryAreaTypeUniqueGift", "StoryAreaTypeWeather", "SuccessfulPayment", + "SuggestedPostApprovalFailed", + "SuggestedPostApproved", + "SuggestedPostDeclined", + "SuggestedPostInfo", + "SuggestedPostPaid", + "SuggestedPostParameters", + "SuggestedPostPrice", + "SuggestedPostRefunded", "SwitchInlineQueryChosenChat", "TelegramObject", "TextQuote", @@ -394,6 +403,7 @@ from ._copytextbutton import CopyTextButton from ._dice import Dice from ._directmessagepricechanged import DirectMessagePriceChanged +from ._directmessagestopic import DirectMessagesTopic from ._files._inputstorycontent import ( InputStoryContent, InputStoryContentPhoto, @@ -571,6 +581,16 @@ StoryAreaTypeUniqueGift, StoryAreaTypeWeather, ) +from ._suggestedpost import ( + SuggestedPostApprovalFailed, + SuggestedPostApproved, + SuggestedPostDeclined, + SuggestedPostInfo, + SuggestedPostPaid, + SuggestedPostParameters, + SuggestedPostPrice, + SuggestedPostRefunded, +) from ._switchinlinequerychosenchat import SwitchInlineQueryChosenChat from ._telegramobject import TelegramObject from ._uniquegift import ( diff --git a/src/telegram/_bot.py b/src/telegram/_bot.py index 33fba87e798..55697aacac4 100644 --- a/src/telegram/_bot.py +++ b/src/telegram/_bot.py @@ -136,6 +136,7 @@ PassportElementError, ShippingOption, StoryArea, + SuggestedPostParameters, ) BT = TypeVar("BT", bound="Bot") @@ -758,6 +759,8 @@ async def _send_message( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -800,6 +803,7 @@ async def _send_message( "business_connection_id": business_connection_id, "caption": caption, "caption_entities": caption_entities, + "direct_messages_topic_id": direct_messages_topic_id, "disable_notification": disable_notification, "link_preview_options": link_preview_options, "message_thread_id": message_thread_id, @@ -808,6 +812,7 @@ async def _send_message( "protect_content": protect_content, "reply_markup": reply_markup, "reply_parameters": reply_parameters, + "suggested_post_parameters": suggested_post_parameters, } ) @@ -1003,6 +1008,8 @@ async def send_message( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -1057,6 +1064,13 @@ async def send_message( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -1116,6 +1130,8 @@ async def send_message( reply_parameters=reply_parameters, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, read_timeout=read_timeout, write_timeout=write_timeout, connect_timeout=connect_timeout, @@ -1232,6 +1248,8 @@ async def forward_message( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -1267,6 +1285,16 @@ async def forward_message( message_thread_id (:obj:`int`, optional): |message_thread_id_arg| .. versionadded:: 20.0 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): An + object containing the parameters of the suggested post to send; for direct messages + chats only. + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): Identifier of the direct messages + topic to which the message will be forwarded; required if the message is + forwarded to a direct messages chat. + + .. versionadded:: NEXT.VERSION Returns: :class:`telegram.Message`: On success, the sent Message is returned. @@ -1287,11 +1315,13 @@ async def forward_message( disable_notification=disable_notification, protect_content=protect_content, message_thread_id=message_thread_id, + suggested_post_parameters=suggested_post_parameters, read_timeout=read_timeout, write_timeout=write_timeout, connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def forward_messages( @@ -1302,6 +1332,7 @@ async def forward_messages( disable_notification: ODVInput[bool] = DEFAULT_NONE, protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -1328,6 +1359,11 @@ async def forward_messages( disable_notification (:obj:`bool`, optional): |disable_notification| protect_content (:obj:`bool`, optional): |protect_content| message_thread_id (:obj:`int`, optional): |message_thread_id_arg| + direct_messages_topic_id (:obj:`int`, optional): Identifier of the direct messages + topic to which the messages will be forwarded; required if the messages are + forwarded to a direct messages chat. + + .. versionadded:: NEXT.VERSION Returns: tuple[:class:`telegram.Message`]: On success, a tuple of ``MessageId`` of sent messages @@ -1343,6 +1379,7 @@ async def forward_messages( "disable_notification": disable_notification, "protect_content": protect_content, "message_thread_id": message_thread_id, + "direct_messages_topic_id": direct_messages_topic_id, } result = await self._post( @@ -1373,6 +1410,8 @@ async def send_photo( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -1445,6 +1484,13 @@ async def send_photo( show_caption_above_media (:obj:`bool`, optional): Pass |show_cap_above_med| .. versionadded:: 21.3 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -1506,6 +1552,8 @@ async def send_photo( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_audio( @@ -1527,6 +1575,8 @@ async def send_audio( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -1609,6 +1659,13 @@ async def send_audio( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -1672,6 +1729,8 @@ async def send_audio( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_document( @@ -1691,6 +1750,8 @@ async def send_document( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -1768,6 +1829,13 @@ async def send_document( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -1827,6 +1895,8 @@ async def send_document( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_sticker( @@ -1842,6 +1912,8 @@ async def send_sticker( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -1899,6 +1971,13 @@ async def send_sticker( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -1950,6 +2029,8 @@ async def send_sticker( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video( @@ -1976,6 +2057,8 @@ async def send_video( show_caption_above_media: Optional[bool] = None, cover: Optional[FileInput] = None, start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -2076,6 +2159,13 @@ async def send_video( show_caption_above_media (:obj:`bool`, optional): Pass |show_cap_above_med| .. versionadded:: 21.3 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -2144,6 +2234,8 @@ async def send_video( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video_note( @@ -2161,6 +2253,8 @@ async def send_video_note( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -2237,6 +2331,13 @@ async def send_video_note( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -2296,6 +2397,8 @@ async def send_video_note( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_animation( @@ -2319,6 +2422,8 @@ async def send_animation( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -2406,6 +2511,13 @@ async def send_animation( show_caption_above_media (:obj:`bool`, optional): Pass |show_cap_above_med| .. versionadded:: 21.3 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -2471,6 +2583,8 @@ async def send_animation( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_voice( @@ -2489,6 +2603,8 @@ async def send_voice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -2567,6 +2683,13 @@ async def send_voice( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -2627,6 +2750,8 @@ async def send_voice( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_media_group( @@ -2642,6 +2767,7 @@ async def send_media_group( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -2698,6 +2824,12 @@ async def send_media_group( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + direct_messages_topic_id (:obj:`int`, optional): Identifier of the direct messages + topic to which the messages will be sent; required if the messages are sent to a + direct messages chat. + + .. versionadded:: NEXT.VERSION + Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -2793,6 +2925,7 @@ async def send_media_group( "business_connection_id": business_connection_id, "message_effect_id": message_effect_id, "allow_paid_broadcast": allow_paid_broadcast, + "direct_messages_topic_id": direct_messages_topic_id, } result = await self._post( @@ -2824,6 +2957,8 @@ async def send_location( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -2890,6 +3025,13 @@ async def send_location( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -2961,6 +3103,8 @@ async def send_location( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def edit_message_live_location( @@ -3148,6 +3292,8 @@ async def send_venue( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -3206,6 +3352,13 @@ async def send_venue( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -3288,6 +3441,8 @@ async def send_venue( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_contact( @@ -3305,6 +3460,8 @@ async def send_contact( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -3353,6 +3510,13 @@ async def send_contact( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -3426,6 +3590,8 @@ async def send_contact( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_game( @@ -5231,6 +5397,8 @@ async def send_invoice( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -5351,6 +5519,13 @@ async def send_invoice( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -5421,6 +5596,8 @@ async def send_invoice( api_kwargs=api_kwargs, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def answer_shipping_query( @@ -5673,6 +5850,7 @@ async def promote_chat_member( can_post_stories: Optional[bool] = None, can_edit_stories: Optional[bool] = None, can_delete_stories: Optional[bool] = None, + can_manage_direct_messages: Optional[bool] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -5741,6 +5919,11 @@ async def promote_chat_member( delete stories posted by other users. .. versionadded:: 20.6 + can_manage_direct_messages (:obj:`bool`, optional): Pass :obj:`True`, if the + administrator can manage direct messages within the channel and decline suggested + posts; for channels only + + .. versionadded:: NEXT.VERSION Returns: :obj:`bool`: On success, :obj:`True` is returned. @@ -5767,6 +5950,7 @@ async def promote_chat_member( "can_post_stories": can_post_stories, "can_edit_stories": can_edit_stories, "can_delete_stories": can_delete_stories, + "can_manage_direct_messages": can_manage_direct_messages, } return await self._post( @@ -7707,6 +7891,8 @@ async def send_dice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -7759,6 +7945,13 @@ async def send_dice( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -7808,6 +8001,8 @@ async def send_dice( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def get_my_default_administrator_rights( @@ -8140,6 +8335,8 @@ async def copy_message( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -8194,6 +8391,13 @@ async def copy_message( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -8253,7 +8457,9 @@ async def copy_message( "reply_parameters": reply_parameters, "show_caption_above_media": show_caption_above_media, "allow_paid_broadcast": allow_paid_broadcast, + "direct_messages_topic_id": direct_messages_topic_id, "video_start_timestamp": video_start_timestamp, + "suggested_post_parameters": suggested_post_parameters, } result = await self._post( @@ -8276,6 +8482,7 @@ async def copy_messages( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, remove_caption: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -8308,6 +8515,12 @@ async def copy_messages( message_thread_id (:obj:`int`, optional): |message_thread_id_arg| remove_caption (:obj:`bool`, optional): Pass :obj:`True` to copy the messages without their captions. + direct_messages_topic_id (:obj:`int`, optional): Identifier of the direct messages + topic to which the message will be sent; required if the message is sent to a + direct messages chat. + + .. versionadded:: NEXT.VERSION + Returns: tuple[:class:`telegram.MessageId`]: On success, a tuple of :class:`~telegram.MessageId` @@ -8325,6 +8538,7 @@ async def copy_messages( "protect_content": protect_content, "message_thread_id": message_thread_id, "remove_caption": remove_caption, + "direct_messages_topic_id": direct_messages_topic_id, } result = await self._post( @@ -10729,6 +10943,9 @@ async def send_paid_media( business_connection_id: Optional[str] = None, payload: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, + message_thread_id: Optional[int] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -10775,6 +10992,16 @@ async def send_paid_media( allow_paid_broadcast (:obj:`bool`, optional): |allow_paid_broadcast| .. versionadded:: 21.7 + suggested_post_parameters (:class:`telegram.SuggestedPostParameters`, optional): + |suggested_post_parameters| + + .. versionadded:: NEXT.VERSION + direct_messages_topic_id (:obj:`int`, optional): |direct_messages_topic_id| + + .. versionadded:: NEXT.VERSION + message_thread_id (:obj:`int`, optional): |message_thread_id_arg| + + .. versionadded:: NEXT.VERSION Keyword Args: allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| @@ -10819,6 +11046,9 @@ async def send_paid_media( api_kwargs=api_kwargs, business_connection_id=business_connection_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, + message_thread_id=message_thread_id, ) async def create_chat_subscription_invite_link( @@ -11147,8 +11377,6 @@ async def remove_chat_verification( """Removes verification from a chat that is currently verified |org-verify| represented by the bot. - - .. versionadded:: 21.10 Args: @@ -11186,8 +11414,6 @@ async def remove_user_verification( """Removes verification from a user who is currently verified |org-verify| represented by the bot. - - .. versionadded:: 21.10 Args: @@ -11242,6 +11468,105 @@ async def get_my_star_balance( ) ) + async def approve_suggested_post( + self, + chat_id: int, + message_id: int, + send_date: Optional[Union[int, dtm.datetime]] = 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: + """ + Use this method to approve a suggested post in a direct messages chat. + The bot must have the :attr:`~telegram.ChatMemberAdministrator.can_post_messages` + administrator right in the corresponding channel chat. + + .. versionadded:: NEXT.VERSION + + Args: + chat_id (:obj:`int`): Unique identifier of the target direct messages chat. + message_id (:obj:`int`): Identifier of a suggested post message to approve. + send_date (:obj:`int` | :obj:`datetime.datetime`, optional): Date when the post is + expected to be published; omit if the date has already been specified when the + suggested post was created. If specified, then the date must be not more than + :tg-const:`telegram.constants.SuggestedPost.MAX_SEND_DATE` seconds (30 days) + in the future. + + |tz-naive-dtms| + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + Raises: + :class:`telegram.error.TelegramError` + """ + data: JSONDict = { + "chat_id": chat_id, + "message_id": message_id, + "send_date": send_date, + } + + return await self._post( + "approveSuggestedPost", + data, + read_timeout=read_timeout, + write_timeout=write_timeout, + connect_timeout=connect_timeout, + pool_timeout=pool_timeout, + api_kwargs=api_kwargs, + ) + + async def decline_suggested_post( + self, + chat_id: int, + message_id: int, + comment: Optional[str] = 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: + """ + Use this method to decline a suggested post in a direct messages chat. + The bot must have the :attr:`~telegram.ChatMemberAdministrator.can_manage_direct_messages` + administrator right in the corresponding channel chat. + + .. versionadded:: NEXT.VERSION + + Args: + chat_id (:obj:`int`): Unique identifier of the target direct messages chat. + message_id (:obj:`int`): Identifier of a suggested post message to decline. + comment (:obj:`str`, optional): Comment for the creator of the suggested post. + 0-:tg-const:`telegram.constants.SuggestedPost.MAX_COMMENT_LENGTH` characters. + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + + Raises: + :class:`telegram.error.TelegramError` + """ + data: JSONDict = { + "chat_id": chat_id, + "message_id": message_id, + "comment": comment, + } + + return await self._post( + "declineSuggestedPost", + 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} @@ -11562,3 +11887,7 @@ def to_dict(self, recursive: bool = True) -> JSONDict: # noqa: ARG002 """Alias for :meth:`remove_user_verification`""" getMyStarBalance = get_my_star_balance """Alias for :meth:`get_my_star_balance`""" + approveSuggestedPost = approve_suggested_post + """Alias for :meth:`approve_suggested_post`""" + declineSuggestedPost = decline_suggested_post + """Alias for :meth:`decline_suggested_post`""" diff --git a/src/telegram/_callbackquery.py b/src/telegram/_callbackquery.py index 18b5980e6c6..7c6952889de 100644 --- a/src/telegram/_callbackquery.py +++ b/src/telegram/_callbackquery.py @@ -42,6 +42,7 @@ MessageEntity, MessageId, ReplyParameters, + SuggestedPostParameters, ) @@ -871,6 +872,7 @@ async def copy_message( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -885,6 +887,7 @@ async def copy_message( await update.callback_query.message.copy( from_chat_id=update.message.chat_id, message_id=update.message.message_id, + direct_messages_topic_id=update.message.direct_messages_topic.topic_id, *args, **kwargs ) @@ -920,6 +923,7 @@ async def copy_message( reply_parameters=reply_parameters, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + suggested_post_parameters=suggested_post_parameters, ) MAX_ANSWER_TEXT_LENGTH: Final[int] = ( diff --git a/src/telegram/_chat.py b/src/telegram/_chat.py index 53e4934523b..f1c3cf7aeae 100644 --- a/src/telegram/_chat.py +++ b/src/telegram/_chat.py @@ -71,6 +71,7 @@ PhotoSize, ReplyParameters, Sticker, + SuggestedPostParameters, UserChatBoosts, Venue, Video, @@ -85,7 +86,16 @@ class _ChatBase(TelegramObject): .. versionadded:: 21.3 """ - __slots__ = ("first_name", "id", "is_forum", "last_name", "title", "type", "username") + __slots__ = ( + "first_name", + "id", + "is_direct_messages", + "is_forum", + "last_name", + "title", + "type", + "username", + ) def __init__( self, @@ -96,6 +106,7 @@ def __init__( first_name: Optional[str] = None, last_name: Optional[str] = None, is_forum: Optional[bool] = None, + is_direct_messages: Optional[bool] = None, *, api_kwargs: Optional[JSONDict] = None, ): @@ -109,6 +120,7 @@ def __init__( self.first_name: Optional[str] = first_name self.last_name: Optional[str] = last_name self.is_forum: Optional[bool] = is_forum + self.is_direct_messages: Optional[bool] = is_direct_messages self._id_attrs = (self.id,) @@ -604,6 +616,7 @@ async def promote_member( can_post_stories: Optional[bool] = None, can_edit_stories: Optional[bool] = None, can_delete_stories: Optional[bool] = None, + can_manage_direct_messages: Optional[bool] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -654,6 +667,7 @@ async def promote_member( can_post_stories=can_post_stories, can_edit_stories=can_edit_stories, can_delete_stories=can_delete_stories, + can_manage_direct_messages=can_manage_direct_messages, ) async def restrict_member( @@ -1018,6 +1032,8 @@ async def send_message( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1060,6 +1076,8 @@ async def send_message( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def delete_message( @@ -1138,6 +1156,7 @@ async def send_media_group( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1181,6 +1200,7 @@ async def send_media_group( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, ) async def send_chat_action( @@ -1236,6 +1256,8 @@ async def send_photo( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1280,6 +1302,8 @@ async def send_photo( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_contact( @@ -1296,6 +1320,8 @@ async def send_contact( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1338,6 +1364,8 @@ async def send_contact( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_audio( @@ -1358,6 +1386,8 @@ async def send_audio( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1404,6 +1434,8 @@ async def send_audio( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_document( @@ -1422,6 +1454,8 @@ async def send_document( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1466,6 +1500,8 @@ async def send_document( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_checklist( @@ -1527,6 +1563,8 @@ async def send_dice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1564,6 +1602,8 @@ async def send_dice( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_game( @@ -1646,6 +1686,8 @@ async def send_invoice( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1712,6 +1754,8 @@ async def send_invoice( reply_parameters=reply_parameters, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_location( @@ -1730,6 +1774,8 @@ async def send_location( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1774,6 +1820,8 @@ async def send_location( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_animation( @@ -1796,6 +1844,8 @@ async def send_animation( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1844,6 +1894,8 @@ async def send_animation( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_sticker( @@ -1858,6 +1910,8 @@ async def send_sticker( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1896,6 +1950,8 @@ async def send_sticker( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_venue( @@ -1916,6 +1972,8 @@ async def send_venue( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1962,6 +2020,8 @@ async def send_venue( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video( @@ -1987,6 +2047,8 @@ async def send_video( show_caption_above_media: Optional[bool] = None, cover: Optional[FileInput] = None, start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2038,6 +2100,8 @@ async def send_video( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video_note( @@ -2054,6 +2118,8 @@ async def send_video_note( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2096,6 +2162,8 @@ async def send_video_note( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_voice( @@ -2113,6 +2181,8 @@ async def send_voice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2156,6 +2226,8 @@ async def send_voice( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_poll( @@ -2249,6 +2321,8 @@ async def send_copy( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2292,6 +2366,8 @@ async def send_copy( message_thread_id=message_thread_id, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def copy_message( @@ -2309,6 +2385,8 @@ async def copy_message( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2352,6 +2430,8 @@ async def copy_message( message_thread_id=message_thread_id, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_copies( @@ -2362,6 +2442,7 @@ async def send_copies( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, remove_caption: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2397,6 +2478,7 @@ async def send_copies( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def copy_messages( @@ -2407,6 +2489,7 @@ async def copy_messages( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, remove_caption: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2442,6 +2525,7 @@ async def copy_messages( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def forward_from( @@ -2452,6 +2536,8 @@ async def forward_from( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2486,6 +2572,8 @@ async def forward_from( api_kwargs=api_kwargs, protect_content=protect_content, message_thread_id=message_thread_id, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def forward_to( @@ -2496,6 +2584,8 @@ async def forward_to( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2531,6 +2621,8 @@ async def forward_to( api_kwargs=api_kwargs, protect_content=protect_content, message_thread_id=message_thread_id, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def forward_messages_from( @@ -2540,6 +2632,7 @@ async def forward_messages_from( disable_notification: ODVInput[bool] = DEFAULT_NONE, protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2574,6 +2667,7 @@ async def forward_messages_from( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def forward_messages_to( @@ -2583,6 +2677,7 @@ async def forward_messages_to( disable_notification: ODVInput[bool] = DEFAULT_NONE, protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2617,6 +2712,7 @@ async def forward_messages_to( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def export_invite_link( @@ -3456,6 +3552,9 @@ async def send_paid_media( business_connection_id: Optional[str] = None, payload: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, + message_thread_id: Optional[int] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -3499,6 +3598,9 @@ async def send_paid_media( business_connection_id=business_connection_id, payload=payload, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, + message_thread_id=message_thread_id, ) async def send_gift( @@ -3682,6 +3784,76 @@ async def read_business_message( api_kwargs=api_kwargs, ) + async def approve_suggested_post( + self, + message_id: int, + send_date: Optional[Union[int, dtm.datetime]] = 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.approve_suggested_post(chat_id=update.effective_chat.id, *args, **kwargs) + + For the documentation of the arguments, please see + :meth:`telegram.Bot.approve_suggested_post`. + + .. versionadded:: NEXT.VERSION + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + """ + return await self.get_bot().approve_suggested_post( + chat_id=self.id, + message_id=message_id, + send_date=send_date, + read_timeout=read_timeout, + write_timeout=write_timeout, + connect_timeout=connect_timeout, + pool_timeout=pool_timeout, + api_kwargs=api_kwargs, + ) + + async def decline_suggested_post( + self, + message_id: int, + comment: Optional[str] = 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.decline_suggested_post(chat_id=update.effective_chat.id, *args, **kwargs) + + For the documentation of the arguments, please see + :meth:`telegram.Bot.decline_suggested_post`. + + .. versionadded:: NEXT.VERSION + + Returns: + :obj:`bool`: On success, :obj:`True` is returned. + """ + return await self.get_bot().decline_suggested_post( + chat_id=self.id, + message_id=message_id, + comment=comment, + 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. @@ -3719,6 +3891,10 @@ class Chat(_ChatBase): (has topics_ enabled). .. versionadded:: 20.0 + is_direct_messages (:obj:`bool`, optional): :obj:`True`, if the chat is the direct messages + chat of a channel. + + .. versionadded:: NEXT.VERSION Attributes: id (:obj:`int`): Unique identifier for this chat. @@ -3733,6 +3909,10 @@ class Chat(_ChatBase): (has topics_ enabled). .. versionadded:: 20.0 + is_direct_messages (:obj:`bool`): Optional. :obj:`True`, if the chat is the direct messages + chat of a channel. + + .. versionadded:: NEXT.VERSION .. _topics: https://telegram.org/blog/topics-in-groups-collectible-usernames#topics-in-groups """ diff --git a/src/telegram/_chatadministratorrights.py b/src/telegram/_chatadministratorrights.py index f5b75e786ac..311122d4da8 100644 --- a/src/telegram/_chatadministratorrights.py +++ b/src/telegram/_chatadministratorrights.py @@ -32,8 +32,8 @@ class ChatAdministratorRights(TelegramObject): :attr:`can_delete_messages`, :attr:`can_manage_video_chats`, :attr:`can_restrict_members`, :attr:`can_promote_members`, :attr:`can_change_info`, :attr:`can_invite_users`, :attr:`can_post_messages`, :attr:`can_edit_messages`, :attr:`can_pin_messages`, - :attr:`can_manage_topics`, :attr:`can_post_stories`, :attr:`can_delete_stories`, and - :attr:`can_edit_stories` are equal. + :attr:`can_manage_topics`, :attr:`can_post_stories`, :attr:`can_delete_stories`, + :attr:`can_edit_stories` and :attr:`can_manage_direct_messages` are equal. .. versionadded:: 20.0 @@ -50,6 +50,10 @@ class ChatAdministratorRights(TelegramObject): and :attr:`can_delete_stories` is now required. Thus, the order of arguments had to be changed. + .. versionchanged:: NEXT.VERSION + :attr:`can_manage_direct_messages` is considered as well when comparing objects of + this type in terms of equality. + Args: is_anonymous (:obj:`bool`): :obj:`True`, if the user's presence in the chat is hidden. can_manage_chat (:obj:`bool`): :obj:`True`, if the administrator can access the chat event @@ -98,6 +102,10 @@ class ChatAdministratorRights(TelegramObject): to create, rename, close, and reopen forum topics; for supergroups only. .. versionadded:: 20.0 + can_manage_direct_messages (:obj:`bool`, optional): :obj:`True`, if the administrator can + manage direct messages of the channel and decline suggested posts; for channels only. + + .. versionadded:: NEXT.VERSION Attributes: is_anonymous (:obj:`bool`): :obj:`True`, if the user's presence in the chat is hidden. @@ -147,6 +155,10 @@ class ChatAdministratorRights(TelegramObject): to create, rename, close, and reopen forum topics; for supergroups only. .. versionadded:: 20.0 + can_manage_direct_messages (:obj:`bool`): Optional. :obj:`True`, if the administrator can + manage direct messages of the channel and decline suggested posts; for channels only. + + .. versionadded:: NEXT.VERSION """ __slots__ = ( @@ -157,6 +169,7 @@ class ChatAdministratorRights(TelegramObject): "can_edit_stories", "can_invite_users", "can_manage_chat", + "can_manage_direct_messages", "can_manage_topics", "can_manage_video_chats", "can_pin_messages", @@ -184,6 +197,7 @@ def __init__( can_edit_messages: Optional[bool] = None, can_pin_messages: Optional[bool] = None, can_manage_topics: Optional[bool] = None, + can_manage_direct_messages: Optional[bool] = None, *, api_kwargs: Optional[JSONDict] = None, ) -> None: @@ -205,6 +219,7 @@ def __init__( self.can_edit_messages: Optional[bool] = can_edit_messages self.can_pin_messages: Optional[bool] = can_pin_messages self.can_manage_topics: Optional[bool] = can_manage_topics + self.can_manage_direct_messages: Optional[bool] = can_manage_direct_messages self._id_attrs = ( self.is_anonymous, @@ -222,6 +237,7 @@ def __init__( self.can_post_stories, self.can_edit_stories, self.can_delete_stories, + self.can_manage_direct_messages, ) self._freeze() diff --git a/src/telegram/_chatfullinfo.py b/src/telegram/_chatfullinfo.py index 67ef717832e..243787a6a13 100644 --- a/src/telegram/_chatfullinfo.py +++ b/src/telegram/_chatfullinfo.py @@ -224,6 +224,14 @@ class ChatFullInfo(_ChatBase): sent or forwarded to the channel chat. The field is available only for channel chats. .. versionadded:: 21.4 + is_direct_messages (:obj:`bool`, optional): :obj:`True`, if the chat is the direct messages + chat of a channel. + + .. versionadded:: NEXT.VERSION + parent_chat (:obj:`telegram.Chat`, optional): Information about the corresponding channel + chat; for direct messages chats only. + + .. versionadded:: NEXT.VERSION Attributes: id (:obj:`int`): Unique identifier for this chat. @@ -388,6 +396,14 @@ class ChatFullInfo(_ChatBase): sent or forwarded to the channel chat. The field is available only for channel chats. .. versionadded:: 21.4 + is_direct_messages (:obj:`bool`): Optional. :obj:`True`, if the chat is the direct messages + chat of a channel. + + .. versionadded:: NEXT.VERSION + parent_chat (:obj:`telegram.Chat`): Optional. Information about the corresponding channel + chat; for direct messages chats only. + + .. versionadded:: NEXT.VERSION .. _accent colors: https://core.telegram.org/bots/api#accent-colors .. _topics: https://telegram.org/blog/topics-in-groups-collectible-usernames#topics-in-groups @@ -424,6 +440,7 @@ class ChatFullInfo(_ChatBase): "linked_chat_id", "location", "max_reaction_count", + "parent_chat", "permissions", "personal_chat", "photo", @@ -481,6 +498,8 @@ def __init__( linked_chat_id: Optional[int] = None, location: Optional[ChatLocation] = None, can_send_paid_media: Optional[bool] = None, + is_direct_messages: Optional[bool] = None, + parent_chat: Optional[Chat] = None, *, api_kwargs: Optional[JSONDict] = None, ): @@ -492,6 +511,7 @@ def __init__( first_name=first_name, last_name=last_name, is_forum=is_forum, + is_direct_messages=is_direct_messages, api_kwargs=api_kwargs, ) # Required and unique to this class- @@ -546,6 +566,7 @@ def __init__( self.business_opening_hours: Optional[BusinessOpeningHours] = business_opening_hours self.can_send_paid_media: Optional[bool] = can_send_paid_media self.accepted_gift_types: AcceptedGiftTypes = accepted_gift_types + self.parent_chat: Optional[Chat] = parent_chat @property def slow_mode_delay(self) -> Optional[Union[int, dtm.timedelta]]: @@ -596,5 +617,6 @@ def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "ChatFullInfo": data["business_opening_hours"] = de_json_optional( data.get("business_opening_hours"), BusinessOpeningHours, bot ) + data["parent_chat"] = de_json_optional(data.get("parent_chat"), Chat, bot) return super().de_json(data=data, bot=bot) diff --git a/src/telegram/_chatmember.py b/src/telegram/_chatmember.py index 647c089edde..2c77a944033 100644 --- a/src/telegram/_chatmember.py +++ b/src/telegram/_chatmember.py @@ -253,6 +253,10 @@ class ChatMemberAdministrator(ChatMember): .. versionadded:: 20.0 custom_title (:obj:`str`, optional): Custom title for this user. + can_manage_direct_messages (:obj:`bool`, optional): :obj:`True`, if the administrator can + manage direct messages of the channel and decline suggested posts; for channels only. + + .. versionadded:: NEXT.VERSION Attributes: status (:obj:`str`): The member's status in the chat, @@ -313,6 +317,10 @@ class ChatMemberAdministrator(ChatMember): .. versionadded:: 20.0 custom_title (:obj:`str`): Optional. Custom title for this user. + can_manage_direct_messages (:obj:`bool`, optional): :obj:`True`, if the administrator can + manage direct messages of the channel and decline suggested posts; for channels only. + + .. versionadded:: NEXT.VERSION """ __slots__ = ( @@ -324,6 +332,7 @@ class ChatMemberAdministrator(ChatMember): "can_edit_stories", "can_invite_users", "can_manage_chat", + "can_manage_direct_messages", "can_manage_topics", "can_manage_video_chats", "can_pin_messages", @@ -355,6 +364,7 @@ def __init__( can_pin_messages: Optional[bool] = None, can_manage_topics: Optional[bool] = None, custom_title: Optional[str] = None, + can_manage_direct_messages: Optional[bool] = None, *, api_kwargs: Optional[JSONDict] = None, ): @@ -378,6 +388,7 @@ def __init__( self.can_pin_messages: Optional[bool] = can_pin_messages self.can_manage_topics: Optional[bool] = can_manage_topics self.custom_title: Optional[str] = custom_title + self.can_manage_direct_messages: Optional[bool] = can_manage_direct_messages class ChatMemberMember(ChatMember): diff --git a/src/telegram/_directmessagestopic.py b/src/telegram/_directmessagestopic.py new file mode 100644 index 00000000000..304e496ec98 --- /dev/null +++ b/src/telegram/_directmessagestopic.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015-2025 +# Leandro Toledo de Souza +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. +"""This module contains the DirectMessagesTopic class.""" + +from typing import TYPE_CHECKING, Optional + +from telegram._telegramobject import TelegramObject +from telegram._user import User +from telegram._utils.argumentparsing import de_json_optional +from telegram._utils.types import JSONDict + +if TYPE_CHECKING: + from telegram._bot import Bot + + +class DirectMessagesTopic(TelegramObject): + """ + This class represents a topic for direct messages in a chat. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if their :attr:`topic_id` and :attr:`user` is equal. + + .. versionadded:: NEXT.VERSION + + Args: + topic_id (:obj:`int`): Unique identifier of the topic. This number may have more than 32 + significant bits and some programming languages may have difficulty/silent defects in + interpreting it. But it has at most 52 significant bits, so a 64-bit integer or + double-precision float type are safe for storing this identifier. + user (:class:`telegram.User`, optional): Information about the user that created the topic. + + .. hint:: + According to Telegram, this field is always present as of Bot API 9.2. + + Attributes: + topic_id (:obj:`int`): Unique identifier of the topic. This number may have more than 32 + significant bits and some programming languages may have difficulty/silent defects in + interpreting it. But it has at most 52 significant bits, so a 64-bit integer or + double-precision float type are safe for storing this identifier. + user (:class:`telegram.User`): Optional. Information about the user that created the topic. + + .. hint:: + According to Telegram, this field is always present as of Bot API 9.2. + + """ + + __slots__ = ("topic_id", "user") + + def __init__( + self, topic_id: int, user: Optional[User] = None, *, api_kwargs: Optional[JSONDict] = None + ): + super().__init__(api_kwargs=api_kwargs) + + # Required: + self.topic_id: int = topic_id + + # Optionals: + self.user: Optional[User] = user + + self._id_attrs = (self.topic_id, self.user) + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "DirectMessagesTopic": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + data["user"] = de_json_optional(data.get("user"), User, bot) + + return super().de_json(data=data, bot=bot) diff --git a/src/telegram/_gifts.py b/src/telegram/_gifts.py index 7c49aa1fd1e..5ed1a60f0f7 100644 --- a/src/telegram/_gifts.py +++ b/src/telegram/_gifts.py @@ -22,6 +22,7 @@ from collections.abc import Sequence from typing import TYPE_CHECKING, Optional +from telegram._chat import Chat from telegram._files.sticker import Sticker from telegram._messageentity import MessageEntity from telegram._telegramobject import TelegramObject @@ -53,6 +54,10 @@ class Gift(TelegramObject): to upgrade the gift to a unique one .. versionadded:: 21.10 + publisher_chat (:class:`telegram.Chat`, optional): Information about the chat that + published the gift. + + .. versionadded:: NEXT.VERSION Attributes: id (:obj:`str`): Unique identifier of the gift @@ -66,11 +71,16 @@ class Gift(TelegramObject): to upgrade the gift to a unique one .. versionadded:: 21.10 + publisher_chat (:class:`telegram.Chat`): Optional. Information about the chat that + published the gift. + + .. versionadded:: NEXT.VERSION """ __slots__ = ( "id", + "publisher_chat", "remaining_count", "star_count", "sticker", @@ -86,6 +96,7 @@ def __init__( total_count: Optional[int] = None, remaining_count: Optional[int] = None, upgrade_star_count: Optional[int] = None, + publisher_chat: Optional[Chat] = None, *, api_kwargs: Optional[JSONDict] = None, ): @@ -96,6 +107,7 @@ def __init__( self.total_count: Optional[int] = total_count self.remaining_count: Optional[int] = remaining_count self.upgrade_star_count: Optional[int] = upgrade_star_count + self.publisher_chat: Optional[Chat] = publisher_chat self._id_attrs = (self.id,) @@ -107,6 +119,7 @@ def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "Gift": data = cls._parse_data(data) data["sticker"] = de_json_optional(data.get("sticker"), Sticker, bot) + data["publisher_chat"] = de_json_optional(data.get("publisher_chat"), Chat, bot) return super().de_json(data=data, bot=bot) diff --git a/src/telegram/_message.py b/src/telegram/_message.py index 2ca0b491860..10b5ccb8353 100644 --- a/src/telegram/_message.py +++ b/src/telegram/_message.py @@ -31,6 +31,7 @@ from telegram._checklists import Checklist, ChecklistTasksAdded, ChecklistTasksDone from telegram._dice import Dice from telegram._directmessagepricechanged import DirectMessagePriceChanged +from telegram._directmessagestopic import DirectMessagesTopic from telegram._files.animation import Animation from telegram._files.audio import Audio from telegram._files.contact import Contact @@ -118,6 +119,13 @@ MessageId, MessageOrigin, ReactionType, + SuggestedPostApprovalFailed, + SuggestedPostApproved, + SuggestedPostDeclined, + SuggestedPostInfo, + SuggestedPostPaid, + SuggestedPostParameters, + SuggestedPostRefunded, TextQuote, ) @@ -342,6 +350,13 @@ class Message(MaybeInaccessibleMessage): .. versionadded:: 20.8 + suggested_post_info (:class:`telegram.SuggestedPostInfo`, optional): Information about + suggested post parameters if the message is a suggested post in a channel direct + messages chat. If the message is an approved or declined suggested post, then it can't + be edited. + + .. versionadded:: NEXT.VERSION + effect_id (:obj:`str`, optional): Unique identifier of the message effect added to the message. @@ -566,6 +581,26 @@ class Message(MaybeInaccessibleMessage): message: the price for paid messages has changed in the chat .. versionadded:: 22.1 + suggested_post_approved (:class:`telegram.SuggestedPostApproved`, optional): Service + message: a suggested post was approved. + + .. versionadded:: NEXT.VERSION + suggested_post_approval_failed (:class:`telegram.SuggestedPostApproved`, optional): Service + message: approval of a suggested post has failed. + + .. versionadded:: NEXT.VERSION + suggested_post_declined (:class:`telegram.SuggestedPostDeclined`, optional): Service + message: a suggested post was declined. + + .. versionadded:: NEXT.VERSION + suggested_post_paid (:class:`telegram.SuggestedPostPaid`, optional): Service + message: payment for a suggested post was received. + + .. versionadded:: NEXT.VERSION + suggested_post_refunded (:class:`telegram.SuggestedPostRefunded`, optional): Service + message: payment for a suggested post was refunded. + + .. versionadded:: NEXT.VERSION external_reply (:class:`telegram.ExternalReplyInfo`, optional): Information about the message that is being replied to, which may come from another chat or forum topic. @@ -628,6 +663,18 @@ class Message(MaybeInaccessibleMessage): of a channel has changed. .. versionadded:: 22.3 + is_paid_post (:obj:`bool`, optional): :obj:`True`, if the message is a paid post. Note that + such posts must not be deleted for 24 hours to receive the payment and can't be edited. + + .. versionadded:: NEXT.VERSION + direct_messages_topic (:class:`telegram.DirectMessagesTopic`, optional): Information about + the direct messages chat topic that contains the message. + + .. versionadded:: NEXT.VERSION + reply_to_checklist_task_id (:obj:`int`, optional): Identifier of the specific checklist + task that is being replied to. + + .. versionadded:: NEXT.VERSION Attributes: message_id (:obj:`int`): Unique message identifier inside this chat. In specific instances @@ -687,6 +734,13 @@ class Message(MaybeInaccessibleMessage): .. versionadded:: 20.8 + suggested_post_info (:class:`telegram.SuggestedPostInfo`): Optional. Information about + suggested post parameters if the message is a suggested post in a channel direct + messages chat. If the message is an approved or declined suggested post, then it can't + be edited. + + .. versionadded:: NEXT.VERSION + effect_id (:obj:`str`): Optional. Unique identifier of the message effect added to the message. @@ -926,6 +980,26 @@ class Message(MaybeInaccessibleMessage): message: the price for paid messages has changed in the chat .. versionadded:: 22.1 + suggested_post_approved (:class:`telegram.SuggestedPostApproved`): Optional. Service + message: a suggested post was approved. + + .. versionadded:: NEXT.VERSION + suggested_post_approval_failed (:class:`telegram.SuggestedPostApproved`): Optional. Service + message: approval of a suggested post has failed. + + .. versionadded:: NEXT.VERSION + suggested_post_declined (:class:`telegram.SuggestedPostDeclined`): Optional. Service + message: a suggested post was declined. + + .. versionadded:: NEXT.VERSION + suggested_post_paid (:class:`telegram.SuggestedPostPaid`): Optional. Service + message: payment for a suggested post was received. + + .. versionadded:: NEXT.VERSION + suggested_post_refunded (:class:`telegram.SuggestedPostRefunded`): Optional. Service + message: payment for a suggested post was refunded. + + .. versionadded:: NEXT.VERSION external_reply (:class:`telegram.ExternalReplyInfo`): Optional. Information about the message that is being replied to, which may come from another chat or forum topic. @@ -989,6 +1063,18 @@ class Message(MaybeInaccessibleMessage): messages chat of a channel has changed. .. versionadded:: 22.3 + is_paid_post (:obj:`bool`): Optional. :obj:`True`, if the message is a paid post. Note that + such posts must not be deleted for 24 hours to receive the payment and can't be edited. + + .. versionadded:: NEXT.VERSION + direct_messages_topic (:class:`telegram.DirectMessagesTopic`): Optional. Information about + the direct messages chat topic that contains the message. + + .. versionadded:: NEXT.VERSION + reply_to_checklist_task_id (:obj:`int`): Optional. Identifier of the specific checklist + task that is being replied to. + + .. versionadded:: NEXT.VERSION .. |custom_emoji_no_md1_support| replace:: Since custom emoji entities are not supported by :attr:`~telegram.constants.ParseMode.MARKDOWN`, this method now raises a @@ -1026,6 +1112,7 @@ class Message(MaybeInaccessibleMessage): "delete_chat_photo", "dice", "direct_message_price_changed", + "direct_messages_topic", "document", "edit_date", "effect_id", @@ -1051,6 +1138,7 @@ class Message(MaybeInaccessibleMessage): "invoice", "is_automatic_forward", "is_from_offline", + "is_paid_post", "is_topic_message", "left_chat_member", "link_preview_options", @@ -1074,6 +1162,7 @@ class Message(MaybeInaccessibleMessage): "quote", "refunded_payment", "reply_markup", + "reply_to_checklist_task_id", "reply_to_message", "reply_to_story", "sender_boost_count", @@ -1083,6 +1172,12 @@ class Message(MaybeInaccessibleMessage): "sticker", "story", "successful_payment", + "suggested_post_approval_failed", + "suggested_post_approved", + "suggested_post_declined", + "suggested_post_info", + "suggested_post_paid", + "suggested_post_refunded", "supergroup_chat_created", "text", "unique_gift", @@ -1195,6 +1290,15 @@ def __init__( checklist: Optional[Checklist] = None, checklist_tasks_done: Optional[ChecklistTasksDone] = None, checklist_tasks_added: Optional[ChecklistTasksAdded] = None, + is_paid_post: Optional[bool] = None, + direct_messages_topic: Optional[DirectMessagesTopic] = None, + reply_to_checklist_task_id: Optional[int] = None, + suggested_post_declined: Optional["SuggestedPostDeclined"] = None, + suggested_post_paid: Optional["SuggestedPostPaid"] = None, + suggested_post_refunded: Optional["SuggestedPostRefunded"] = None, + suggested_post_info: Optional["SuggestedPostInfo"] = None, + suggested_post_approved: Optional["SuggestedPostApproved"] = None, + suggested_post_approval_failed: Optional["SuggestedPostApprovalFailed"] = None, *, api_kwargs: Optional[JSONDict] = None, ): @@ -1310,6 +1414,17 @@ def __init__( self.direct_message_price_changed: Optional[DirectMessagePriceChanged] = ( direct_message_price_changed ) + self.is_paid_post: Optional[bool] = is_paid_post + self.direct_messages_topic: Optional[DirectMessagesTopic] = direct_messages_topic + self.reply_to_checklist_task_id: Optional[int] = reply_to_checklist_task_id + self.suggested_post_declined: Optional[SuggestedPostDeclined] = suggested_post_declined + self.suggested_post_paid: Optional[SuggestedPostPaid] = suggested_post_paid + self.suggested_post_refunded: Optional[SuggestedPostRefunded] = suggested_post_refunded + self.suggested_post_info: Optional[SuggestedPostInfo] = suggested_post_info + self.suggested_post_approved: Optional[SuggestedPostApproved] = suggested_post_approved + self.suggested_post_approval_failed: Optional[SuggestedPostApprovalFailed] = ( + suggested_post_approval_failed + ) self._effective_attachment = DEFAULT_NONE @@ -1464,6 +1579,14 @@ def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "Message": ExternalReplyInfo, TextQuote, ) + from telegram._suggestedpost import ( # pylint: disable=import-outside-toplevel # noqa: PLC0415 + SuggestedPostApprovalFailed, + SuggestedPostApproved, + SuggestedPostDeclined, + SuggestedPostInfo, + SuggestedPostPaid, + SuggestedPostRefunded, + ) data["giveaway"] = de_json_optional(data.get("giveaway"), Giveaway, bot) data["giveaway_completed"] = de_json_optional( @@ -1496,6 +1619,27 @@ def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "Message": data["checklist_tasks_added"] = de_json_optional( data.get("checklist_tasks_added"), ChecklistTasksAdded, bot ) + data["direct_messages_topic"] = de_json_optional( + data.get("direct_messages_topic"), DirectMessagesTopic, bot + ) + data["suggested_post_declined"] = de_json_optional( + data.get("suggested_post_declined"), SuggestedPostDeclined, bot + ) + data["suggested_post_paid"] = de_json_optional( + data.get("suggested_post_paid"), SuggestedPostPaid, bot + ) + data["suggested_post_refunded"] = de_json_optional( + data.get("suggested_post_refunded"), SuggestedPostRefunded, bot + ) + data["suggested_post_info"] = de_json_optional( + data.get("suggested_post_info"), SuggestedPostInfo, bot + ) + data["suggested_post_approved"] = de_json_optional( + data.get("suggested_post_approved"), SuggestedPostApproved, bot + ) + data["suggested_post_approval_failed"] = de_json_optional( + data.get("suggested_post_approval_failed"), SuggestedPostApprovalFailed, bot + ) api_kwargs = {} # This is a deprecated field that TG still returns for backwards compatibility @@ -1852,6 +1996,10 @@ def _parse_message_thread_id( # the same chat. return self.message_thread_id if chat_id in {self.chat_id, self.chat.username} else None + def _extract_direct_messages_topic_id(self) -> Optional[int]: + """Return the topic id of the direct messages chat, if it is present.""" + return self.direct_messages_topic.topic_id if self.direct_messages_topic else None + async def reply_text( self, text: str, @@ -1865,6 +2013,7 @@ async def reply_text( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1882,6 +2031,7 @@ async def reply_text( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -1927,6 +2077,8 @@ async def reply_text( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_markdown( @@ -1941,6 +2093,7 @@ async def reply_markdown( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1959,6 +2112,7 @@ async def reply_markdown( message_thread_id=update.effective_message.message_thread_id, parse_mode=ParseMode.MARKDOWN, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2009,6 +2163,8 @@ async def reply_markdown( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_markdown_v2( @@ -2023,6 +2179,7 @@ async def reply_markdown_v2( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2040,6 +2197,7 @@ async def reply_markdown_v2( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, parse_mode=ParseMode.MARKDOWN_V2, + direct_messages_topic_id=self.direct_messages_topic.topic_id, business_connection_id=self.business_connection_id, *args, **kwargs, @@ -2087,6 +2245,8 @@ async def reply_markdown_v2( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_html( @@ -2101,6 +2261,7 @@ async def reply_html( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2118,6 +2279,7 @@ async def reply_html( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, parse_mode=ParseMode.HTML, + direct_messages_topic_id=self.direct_messages_topic.topic_id, business_connection_id=self.business_connection_id, *args, **kwargs, @@ -2165,6 +2327,8 @@ async def reply_html( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_media_group( @@ -2197,6 +2361,7 @@ async def reply_media_group( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2242,6 +2407,7 @@ async def reply_media_group( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), ) async def reply_photo( @@ -2259,6 +2425,7 @@ async def reply_photo( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2276,6 +2443,7 @@ async def reply_photo( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2323,6 +2491,8 @@ async def reply_photo( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_audio( @@ -2342,6 +2512,7 @@ async def reply_audio( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2359,6 +2530,7 @@ async def reply_audio( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2408,6 +2580,8 @@ async def reply_audio( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_document( @@ -2425,6 +2599,7 @@ async def reply_document( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2442,6 +2617,7 @@ async def reply_document( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2489,6 +2665,8 @@ async def reply_document( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_animation( @@ -2510,6 +2688,7 @@ async def reply_animation( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2527,6 +2706,7 @@ async def reply_animation( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2578,6 +2758,8 @@ async def reply_animation( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_sticker( @@ -2591,6 +2773,7 @@ async def reply_sticker( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2607,6 +2790,7 @@ async def reply_sticker( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2649,6 +2833,8 @@ async def reply_sticker( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_video( @@ -2673,6 +2859,7 @@ async def reply_video( show_caption_above_media: Optional[bool] = None, cover: Optional[FileInput] = None, start_timestamp: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2690,6 +2877,7 @@ async def reply_video( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2744,6 +2932,8 @@ async def reply_video( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_video_note( @@ -2759,6 +2949,7 @@ async def reply_video_note( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2776,6 +2967,7 @@ async def reply_video_note( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2821,6 +3013,8 @@ async def reply_video_note( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_voice( @@ -2837,6 +3031,7 @@ async def reply_voice( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2854,6 +3049,7 @@ async def reply_voice( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2900,6 +3096,8 @@ async def reply_voice( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_location( @@ -2917,6 +3115,7 @@ async def reply_location( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2934,6 +3133,7 @@ async def reply_location( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -2981,6 +3181,8 @@ async def reply_location( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_venue( @@ -3000,6 +3202,7 @@ async def reply_venue( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3017,6 +3220,7 @@ async def reply_venue( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -3066,6 +3270,8 @@ async def reply_venue( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_contact( @@ -3081,6 +3287,7 @@ async def reply_contact( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3098,6 +3305,7 @@ async def reply_contact( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -3142,7 +3350,9 @@ async def reply_contact( message_thread_id=message_thread_id, business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), allow_paid_broadcast=allow_paid_broadcast, + suggested_post_parameters=suggested_post_parameters, ) async def reply_poll( @@ -3250,6 +3460,7 @@ async def reply_dice( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3266,6 +3477,7 @@ async def reply_dice( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, business_connection_id=self.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -3307,6 +3519,8 @@ async def reply_dice( business_connection_id=self.business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_checklist( @@ -3511,6 +3725,7 @@ async def reply_invoice( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3526,6 +3741,7 @@ async def reply_invoice( await bot.send_invoice( update.effective_message.chat_id, message_thread_id=update.effective_message.message_thread_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs, ) @@ -3598,6 +3814,8 @@ async def reply_invoice( message_thread_id=message_thread_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def forward( @@ -3607,6 +3825,7 @@ async def forward( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, video_start_timestamp: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -3619,6 +3838,7 @@ async def forward( await bot.forward_message( from_chat_id=update.effective_message.chat_id, message_id=update.effective_message.message_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs ) @@ -3645,11 +3865,13 @@ async def forward( disable_notification=disable_notification, protect_content=protect_content, message_thread_id=message_thread_id, + suggested_post_parameters=suggested_post_parameters, read_timeout=read_timeout, write_timeout=write_timeout, connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), ) async def copy( @@ -3666,6 +3888,7 @@ async def copy( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3681,6 +3904,7 @@ async def copy( chat_id=chat_id, from_chat_id=update.effective_message.chat_id, message_id=update.effective_message.message_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs ) @@ -3713,6 +3937,8 @@ async def copy( message_thread_id=message_thread_id, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_copy( @@ -3730,6 +3956,7 @@ async def reply_copy( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3746,6 +3973,7 @@ async def reply_copy( chat_id=message.chat.id, message_thread_id=update.effective_message.message_thread_id, message_id=message_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs ) @@ -3791,6 +4019,8 @@ async def reply_copy( message_thread_id=message_thread_id, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, ) async def reply_paid_media( @@ -3807,6 +4037,8 @@ async def reply_paid_media( reply_markup: Optional[ReplyMarkup] = None, payload: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, + message_thread_id: ODVInput[int] = DEFAULT_NONE, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3821,7 +4053,9 @@ async def reply_paid_media( await bot.send_paid_media( chat_id=message.chat.id, + message_thread_id=update.effective_message.message_thread_id, business_connection_id=message.business_connection_id, + direct_messages_topic_id=self.direct_messages_topic.topic_id, *args, **kwargs ) @@ -3840,6 +4074,7 @@ async def reply_paid_media( chat_id, effective_reply_parameters = await self._parse_quote_arguments( do_quote, reply_to_message_id, reply_parameters, allow_sending_without_reply ) + message_thread_id = self._parse_message_thread_id(chat_id, message_thread_id) return await self.get_bot().send_paid_media( chat_id=chat_id, caption=caption, @@ -3860,6 +4095,9 @@ async def reply_paid_media( protect_content=protect_content, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=self._extract_direct_messages_topic_id(), + suggested_post_parameters=suggested_post_parameters, + message_thread_id=message_thread_id, ) async def edit_text( @@ -4737,6 +4975,80 @@ async def read_business_message( api_kwargs=api_kwargs, ) + async def approve_suggested_post( + self, + send_date: Optional[Union[int, dtm.datetime]] = 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.approve_suggested_post( + chat_id=message.chat_id, + message_id=message.message_id, + *args, **kwargs + ) + + For the documentation of the arguments, please see + :meth:`telegram.Bot.approve_suggested_post`. + + .. versionadded:: NEXT.VERSION + + Returns: + :obj:`bool` On success, :obj:`True` is returned. + """ + return await self.get_bot().approve_suggested_post( + chat_id=self.chat_id, + message_id=self.message_id, + send_date=send_date, + read_timeout=read_timeout, + write_timeout=write_timeout, + connect_timeout=connect_timeout, + pool_timeout=pool_timeout, + api_kwargs=api_kwargs, + ) + + async def decline_suggested_post( + self, + comment: Optional[str] = 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.decline_suggested_post( + chat_id=message.chat_id, + message_id=message.message_id, + *args, **kwargs + ) + + For the documentation of the arguments, please see + :meth:`telegram.Bot.decline_suggested_post`. + + .. versionadded:: NEXT.VERSION + + Returns: + :obj:`bool` On success, :obj:`True` is returned. + """ + return await self.get_bot().decline_suggested_post( + chat_id=self.chat_id, + message_id=self.message_id, + comment=comment, + read_timeout=read_timeout, + write_timeout=write_timeout, + connect_timeout=connect_timeout, + pool_timeout=pool_timeout, + api_kwargs=api_kwargs, + ) + def parse_entity(self, entity: MessageEntity) -> str: """Returns the text from a given :class:`telegram.MessageEntity`. diff --git a/src/telegram/_reply.py b/src/telegram/_reply.py index ae2165bd60e..bb9c72e7ea6 100644 --- a/src/telegram/_reply.py +++ b/src/telegram/_reply.py @@ -382,7 +382,8 @@ class ReplyParameters(TelegramObject): chat, or in the chat :paramref:`chat_id` if it is specified. chat_id (:obj:`int` | :obj:`str`, optional): If the message to be replied to is from a different chat, |chat_id_channel| - Not supported for messages sent on behalf of a business account. + Not supported for messages sent on behalf of a business account and messages from + channel direct messages chats. allow_sending_without_reply (:obj:`bool`, optional): |allow_sending_without_reply| Can be used only for replies in the same chat and forum topic. quote (:obj:`str`, optional): Quoted part of the message to be replied to; 0-1024 @@ -399,13 +400,18 @@ class ReplyParameters(TelegramObject): :paramref:`quote_parse_mode`. quote_position (:obj:`int`, optional): Position of the quote in the original message in UTF-16 code units. + checklist_task_id (:obj:`int`, optional): Identifier of the specific checklist task to be + replied to. + + .. versionadded:: NEXT.VERSION Attributes: message_id (:obj:`int`): Identifier of the message that will be replied to in the current chat, or in the chat :paramref:`chat_id` if it is specified. chat_id (:obj:`int` | :obj:`str`): Optional. If the message to be replied to is from a different chat, |chat_id_channel| - Not supported for messages sent on behalf of a business account. + Not supported for messages sent on behalf of a business account and messages from + channel direct messages chats. allow_sending_without_reply (:obj:`bool`): Optional. |allow_sending_without_reply| Can be used only for replies in the same chat and forum topic. quote (:obj:`str`): Optional. Quoted part of the message to be replied to; 0-1024 @@ -421,11 +427,16 @@ class ReplyParameters(TelegramObject): :paramref:`quote_parse_mode`. quote_position (:obj:`int`): Optional. Position of the quote in the original message in UTF-16 code units. + checklist_task_id (:obj:`int`): Optional. Identifier of the specific checklist task to be + replied to. + + .. versionadded:: NEXT.VERSION """ __slots__ = ( "allow_sending_without_reply", "chat_id", + "checklist_task_id", "message_id", "quote", "quote_entities", @@ -438,6 +449,7 @@ def __init__( message_id: int, chat_id: Optional[Union[int, str]] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, + checklist_task_id: Optional[int] = None, quote: Optional[str] = None, quote_parse_mode: ODVInput[str] = DEFAULT_NONE, quote_entities: Optional[Sequence[MessageEntity]] = None, @@ -456,6 +468,7 @@ def __init__( quote_entities ) self.quote_position: Optional[int] = quote_position + self.checklist_task_id: Optional[int] = checklist_task_id self._id_attrs = (self.message_id,) diff --git a/src/telegram/_suggestedpost.py b/src/telegram/_suggestedpost.py new file mode 100644 index 00000000000..f86090927a7 --- /dev/null +++ b/src/telegram/_suggestedpost.py @@ -0,0 +1,573 @@ +#!/usr/bin/env python +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015-2025 +# Leandro Toledo de Souza +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. +"""This module contains objects related to Telegram suggested posts.""" + +import datetime as dtm +from typing import TYPE_CHECKING, Final, Optional + +from telegram import constants +from telegram._message import Message +from telegram._payment.stars.staramount import StarAmount +from telegram._telegramobject import TelegramObject +from telegram._utils import enum +from telegram._utils.argumentparsing import de_json_optional +from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp +from telegram._utils.types import JSONDict + +if TYPE_CHECKING: + from telegram import Bot + + +class SuggestedPostPrice(TelegramObject): + """ + Desribes the price of a suggested post. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if their :attr:`currency` and :attr:`amount` are equal. + + .. versionadded:: NEXT.VERSION + + Args: + currency (:obj:`str`): + Currency in which the post will be paid. Currently, must be one of ``“XTR”`` for + Telegram Stars or ``“TON”`` for toncoins. + amount (:obj:`int`): + The amount of the currency that will be paid for the post in the smallest units of the + currency, i.e. Telegram Stars or nanotoncoins. Currently, price in Telegram Stars must + be between :tg-const:`telegram.constants.SuggestedPost.MIN_PRICE_STARS` + and :tg-const:`telegram.constants.SuggestedPost.MAX_PRICE_STARS`, and price in + nanotoncoins must be between + :tg-const:`telegram.constants.SuggestedPost.MIN_PRICE_NANOTONCOINS` + and :tg-const:`telegram.constants.SuggestedPost.MAX_PRICE_NANOTONCOINS`. + + Attributes: + currency (:obj:`str`): + Currency in which the post will be paid. Currently, must be one of ``“XTR”`` for + Telegram Stars or ``“TON”`` for toncoins. + amount (:obj:`int`): + The amount of the currency that will be paid for the post in the smallest units of the + currency, i.e. Telegram Stars or nanotoncoins. Currently, price in Telegram Stars must + be between :tg-const:`telegram.constants.SuggestedPost.MIN_PRICE_STARS` + and :tg-const:`telegram.constants.SuggestedPost.MAX_PRICE_STARS`, and price in + nanotoncoins must be between + :tg-const:`telegram.constants.SuggestedPost.MIN_PRICE_NANOTONCOINS` + and :tg-const:`telegram.constants.SuggestedPost.MAX_PRICE_NANOTONCOINS`. + """ + + __slots__ = ("amount", "currency") + + def __init__( + self, + currency: str, + amount: int, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + self.currency: str = currency + self.amount: int = amount + + self._id_attrs = (self.currency, self.amount) + + self._freeze() + + +class SuggestedPostParameters(TelegramObject): + """ + Contains parameters of a post that is being suggested by the bot. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if their :attr:`price` and :attr:`send_date` are equal. + + .. versionadded:: NEXT.VERSION + + Args: + price (:class:`telegram.SuggestedPostPrice`, optional): + Proposed price for the post. If the field is omitted, then the post is unpaid. + send_date (:class:`datetime.datetime`, optional): + Proposed send date of the post. If specified, then the date + must be between :tg-const:`telegram.constants.SuggestedPost.MIN_SEND_DATE` + second and :tg-const:`telegram.constants.SuggestedPost.MAX_SEND_DATE` seconds (30 days) + in the future. If the field is omitted, then the post can be published at any time + within :tg-const:`telegram.constants.SuggestedPost.MAX_SEND_DATE` seconds (30 days) at + the sole discretion of the user who approves it. + |datetime_localization| + + Attributes: + price (:class:`telegram.SuggestedPostPrice`): + Optional. Proposed price for the post. If the field is omitted, then the post + is unpaid. + send_date (:class:`datetime.datetime`): + Optional. Proposed send date of the post. If specified, then the date + must be between :tg-const:`telegram.constants.SuggestedPost.MIN_SEND_DATE` + second and :tg-const:`telegram.constants.SuggestedPost.MAX_SEND_DATE` seconds (30 days) + in the future. If the field is omitted, then the post can be published at any time + within :tg-const:`telegram.constants.SuggestedPost.MAX_SEND_DATE` seconds (30 days) at + the sole discretion of the user who approves it. + |datetime_localization| + + """ + + __slots__ = ("price", "send_date") + + def __init__( + self, + price: Optional[SuggestedPostPrice] = None, + send_date: Optional[dtm.datetime] = None, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + self.price: Optional[SuggestedPostPrice] = price + self.send_date: Optional[dtm.datetime] = send_date + + self._id_attrs = (self.price, self.send_date) + + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SuggestedPostParameters": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + data["price"] = de_json_optional(data.get("price"), SuggestedPostPrice, bot) + + # Get the local timezone from the bot if it has defaults + loc_tzinfo = extract_tzinfo_from_defaults(bot) + + data["send_date"] = from_timestamp(data.get("send_date"), tzinfo=loc_tzinfo) + + return super().de_json(data=data, bot=bot) + + +class SuggestedPostInfo(TelegramObject): + """ + Contains information about a suggested post. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if their :attr:`state` and :attr:`price` are equal. + + .. versionadded:: NEXT.VERSION + + Args: + state (:obj:`str`): + State of the suggested post. Currently, it can be one of + :tg-const:`~telegram.constants.SuggestedPostInfoState.PENDING`, + :tg-const:`~telegram.constants.SuggestedPostInfoState.APPROVED`, + :tg-const:`~telegram.constants.SuggestedPostInfoState.DECLINED`. + price (:obj:`SuggestedPostPrice`, optional): + Proposed price of the post. If the field is omitted, then the post is unpaid. + send_date (:class:`datetime.datetime`, optional): + Proposed send date of the post. If the field is omitted, then the post can be published + at any time within 30 days at the sole discretion of the user or administrator who + approves it. + |datetime_localization| + + Attributes: + state (:obj:`str`): + State of the suggested post. Currently, it can be one of + :tg-const:`~telegram.constants.SuggestedPostInfoState.PENDING`, + :tg-const:`~telegram.constants.SuggestedPostInfoState.APPROVED`, + :tg-const:`~telegram.constants.SuggestedPostInfoState.DECLINED`. + price (:obj:`SuggestedPostPrice`): + Optional. Proposed price of the post. If the field is omitted, then the post is unpaid. + send_date (:class:`datetime.datetime`): + Optional. Proposed send date of the post. If the field is omitted, then the post can be + published at any time within 30 days at the sole discretion of the user or + administrator who approves it. + |datetime_localization| + + """ + + __slots__ = ("price", "send_date", "state") + + PENDING: Final[str] = constants.SuggestedPostInfoState.PENDING + """:const:`telegram.constants.SuggestedPostInfoState.PENDING`""" + APPROVED: Final[str] = constants.SuggestedPostInfoState.APPROVED + """:const:`telegram.constants.SuggestedPostInfoState.APPROVED`""" + DECLINED: Final[str] = constants.SuggestedPostInfoState.DECLINED + """:const:`telegram.constants.SuggestedPostInfoState.DECLINED`""" + + def __init__( + self, + state: str, + price: Optional[SuggestedPostPrice] = None, + send_date: Optional[dtm.datetime] = None, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + # Required + self.state: str = enum.get_member(constants.SuggestedPostInfoState, state, state) + # Optionals + self.price: Optional[SuggestedPostPrice] = price + self.send_date: Optional[dtm.datetime] = send_date + + self._id_attrs = (self.state, self.price) + + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SuggestedPostInfo": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + # Get the local timezone from the bot if it has defaults + loc_tzinfo = extract_tzinfo_from_defaults(bot) + + data["price"] = de_json_optional(data.get("price"), SuggestedPostPrice, bot) + data["send_date"] = from_timestamp(data.get("send_date"), tzinfo=loc_tzinfo) + + return super().de_json(data=data, bot=bot) + + +class SuggestedPostDeclined(TelegramObject): + """ + Describes a service message about the rejection of a suggested post. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if their :attr:`suggested_post_message` and :attr:`comment` are equal. + + .. versionadded:: NEXT.VERSION + + Args: + suggested_post_message (:class:`telegram.Message`, optional): + Message containing the suggested post. Note that the :class:`~telegram.Message` object + in this field will not contain the :attr:`~telegram.Message.reply_to_message` field + even if it itself is a reply. + comment (:obj:`str`, optional): + Comment with which the post was declined. + + Attributes: + suggested_post_message (:class:`telegram.Message`): + Optional. Message containing the suggested post. Note that the + :class:`~telegram.Message` object in this field will not contain + the :attr:`~telegram.Message.reply_to_message` field even if it itself is a reply. + comment (:obj:`str`): + Optional. Comment with which the post was declined. + + """ + + __slots__ = ("comment", "suggested_post_message") + + def __init__( + self, + suggested_post_message: Optional[Message] = None, + comment: Optional[str] = None, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + self.suggested_post_message: Optional[Message] = suggested_post_message + self.comment: Optional[str] = comment + + self._id_attrs = (self.suggested_post_message, self.comment) + + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SuggestedPostDeclined": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + data["suggested_post_message"] = de_json_optional( + data.get("suggested_post_message"), Message, bot + ) + + return super().de_json(data=data, bot=bot) + + +class SuggestedPostPaid(TelegramObject): + """ + Describes a service message about a successful payment for a suggested post. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if all of their attributes are equal. + + .. versionadded:: NEXT.VERSION + + Args: + suggested_post_message (:class:`telegram.Message`, optional): + Message containing the suggested post. Note that the :class:`~telegram.Message` object + in this field will not contain the :attr:`~telegram.Message.reply_to_message` field + even if it itself is a reply. + currency (:obj:`str`): + Currency in which the payment was made. Currently, one of ``“XTR”`` for Telegram Stars + or ``“TON”`` for toncoins. + amount (:obj:`int`, optional): + The amount of the currency that was received by the channel in nanotoncoins; for + payments in toncoins only. + star_amount (:class:`telegram.StarAmount`, optional): + The amount of Telegram Stars that was received by the channel; for payments in Telegram + Stars only. + + + Attributes: + suggested_post_message (:class:`telegram.Message`): + Optional. Message containing the suggested post. Note that the + :class:`~telegram.Message` object in this field will not contain + the :attr:`~telegram.Message.reply_to_message` field even if it itself is a reply. + currency (:obj:`str`): + Currency in which the payment was made. Currently, one of ``“XTR”`` for Telegram Stars + or ``“TON”`` for toncoins. + amount (:obj:`int`): + Optional. The amount of the currency that was received by the channel in nanotoncoins; + for payments in toncoins only. + star_amount (:class:`telegram.StarAmount`): + Optional. The amount of Telegram Stars that was received by the channel; for payments + in Telegram Stars only. + + """ + + __slots__ = ("amount", "currency", "star_amount", "suggested_post_message") + + def __init__( + self, + currency: str, + suggested_post_message: Optional[Message] = None, + amount: Optional[int] = None, + star_amount: Optional[StarAmount] = None, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + # Required + self.currency: str = currency + # Optionals + self.suggested_post_message: Optional[Message] = suggested_post_message + self.amount: Optional[int] = amount + self.star_amount: Optional[StarAmount] = star_amount + + self._id_attrs = ( + self.currency, + self.suggested_post_message, + self.amount, + self.star_amount, + ) + + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SuggestedPostPaid": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + data["suggested_post_message"] = de_json_optional( + data.get("suggested_post_message"), Message, bot + ) + data["star_amount"] = de_json_optional(data.get("star_amount"), StarAmount, bot) + + return super().de_json(data=data, bot=bot) + + +class SuggestedPostRefunded(TelegramObject): + """ + Describes a service message about a payment refund for a suggested post. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if their :attr:`suggested_post_message` and :attr:`reason` are equal. + + .. versionadded:: NEXT.VERSION + + Args: + suggested_post_message (:class:`telegram.Message`, optional): + Message containing the suggested post. Note that the :class:`~telegram.Message` object + in this field will not contain the :attr:`~telegram.Message.reply_to_message` field + even if it itself is a reply. + reason (:obj:`str`): + Reason for the refund. Currently, + one of :tg-const:`telegram.constants.SuggestedPostRefunded.POST_DELETED` if the post + was deleted within 24 hours of being posted or removed from scheduled messages without + being posted, or :tg-const:`telegram.constants.SuggestedPostRefunded.PAYMENT_REFUNDED` + if the payer refunded their payment. + + Attributes: + suggested_post_message (:class:`telegram.Message`): + Optional. Message containing the suggested post. Note that the + :class:`~telegram.Message` object in this field will not contain + the :attr:`~telegram.Message.reply_to_message` field even if it itself is a reply. + reason (:obj:`str`): + Reason for the refund. Currently, + one of :tg-const:`telegram.constants.SuggestedPostRefunded.POST_DELETED` if the post + was deleted within 24 hours of being posted or removed from scheduled messages without + being posted, or :tg-const:`telegram.constants.SuggestedPostRefunded.PAYMENT_REFUNDED` + if the payer refunded their payment. + + """ + + __slots__ = ("reason", "suggested_post_message") + + def __init__( + self, + reason: str, + suggested_post_message: Optional[Message] = None, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + # Required + self.reason: str = reason + # Optionals + self.suggested_post_message: Optional[Message] = suggested_post_message + + self._id_attrs = (self.reason, self.suggested_post_message) + + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SuggestedPostRefunded": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + data["suggested_post_message"] = de_json_optional( + data.get("suggested_post_message"), Message, bot + ) + + return super().de_json(data=data, bot=bot) + + +class SuggestedPostApproved(TelegramObject): + """ + Describes a service message about the approval of a suggested post. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if all of their attributes are equal. + + .. versionadded:: NEXT.VERSION + + Args: + suggested_post_message (:class:`telegram.Message`, optional): + Message containing the suggested post. Note that the :class:`~telegram.Message` object + in this field will not contain the :attr:`~telegram.Message.reply_to_message` field + even if it itself is a reply. + price (:obj:`SuggestedPostPrice`, optional): + Amount paid for the post. + send_date (:class:`datetime.datetime`): + Date when the post will be published. + |datetime_localization| + + Attributes: + suggested_post_message (:class:`telegram.Message`): + Optional. Message containing the suggested post. Note that the + :class:`~telegram.Message` object in this field will not contain + the :attr:`~telegram.Message.reply_to_message` field even if it itself is a reply. + price (:obj:`SuggestedPostPrice`): + Optional. Amount paid for the post. + send_date (:class:`datetime.datetime`): + Date when the post will be published. + |datetime_localization| + + """ + + __slots__ = ("price", "send_date", "suggested_post_message") + + def __init__( + self, + send_date: dtm.datetime, + suggested_post_message: Optional[Message] = None, + price: Optional[SuggestedPostPrice] = None, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + # Required + self.send_date: dtm.datetime = send_date + # Optionals + self.suggested_post_message: Optional[Message] = suggested_post_message + self.price: Optional[SuggestedPostPrice] = price + + self._id_attrs = (self.send_date, self.suggested_post_message, self.price) + + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SuggestedPostApproved": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + # Get the local timezone from the bot if it has defaults + loc_tzinfo = extract_tzinfo_from_defaults(bot) + + data["send_date"] = from_timestamp(data.get("send_date"), tzinfo=loc_tzinfo) + data["price"] = de_json_optional(data.get("price"), SuggestedPostPrice, bot) + data["suggested_post_message"] = de_json_optional( + data.get("suggested_post_message"), Message, bot + ) + + return super().de_json(data=data, bot=bot) + + +class SuggestedPostApprovalFailed(TelegramObject): + """ + Describes a service message about the failed approval of a suggested post. Currently, only + caused by insufficient user funds at the time of approval. + + Objects of this class are comparable in terms of equality. Two objects of this class are + considered equal, if their :attr:`suggested_post_message` and :attr:`price` are equal. + + .. versionadded:: NEXT.VERSION + + Args: + suggested_post_message (:class:`telegram.Message`, optional): + Message containing the suggested post. Note that the :class:`~telegram.Message` object + in this field will not contain the :attr:`~telegram.Message.reply_to_message` field + even if it itself is a reply. + price (:obj:`SuggestedPostPrice`): + Expected price of the post. + + Attributes: + suggested_post_message (:class:`telegram.Message`): + Optional. Message containing the suggested post. Note that the + :class:`~telegram.Message` object in this field will not contain + the :attr:`~telegram.Message.reply_to_message` field even if it itself is a reply. + price (:obj:`SuggestedPostPrice`): + Expected price of the post. + + """ + + __slots__ = ("price", "suggested_post_message") + + def __init__( + self, + price: SuggestedPostPrice, + suggested_post_message: Optional[Message] = None, + *, + api_kwargs: Optional[JSONDict] = None, + ): + super().__init__(api_kwargs=api_kwargs) + # Required + self.price: SuggestedPostPrice = price + # Optionals + self.suggested_post_message: Optional[Message] = suggested_post_message + + self._id_attrs = (self.price, self.suggested_post_message) + + self._freeze() + + @classmethod + def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "SuggestedPostApprovalFailed": + """See :meth:`telegram.TelegramObject.de_json`.""" + data = cls._parse_data(data) + + data["price"] = de_json_optional(data.get("price"), SuggestedPostPrice, bot) + data["suggested_post_message"] = de_json_optional( + data.get("suggested_post_message"), Message, bot + ) + + return super().de_json(data=data, bot=bot) diff --git a/src/telegram/_uniquegift.py b/src/telegram/_uniquegift.py index 264b1ede4e1..ff825b987fd 100644 --- a/src/telegram/_uniquegift.py +++ b/src/telegram/_uniquegift.py @@ -23,6 +23,7 @@ from typing import TYPE_CHECKING, Final, Optional from telegram import constants +from telegram._chat import Chat from telegram._files.sticker import Sticker from telegram._telegramobject import TelegramObject from telegram._utils import enum @@ -268,6 +269,10 @@ class UniqueGift(TelegramObject): model (:class:`UniqueGiftModel`): Model of the gift. symbol (:class:`UniqueGiftSymbol`): Symbol of the gift. backdrop (:class:`UniqueGiftBackdrop`): Backdrop of the gift. + publisher_chat (:class:`telegram.Chat`, optional): Information about the chat that + published the gift. + + .. versionadded:: NEXT.VERSION Attributes: base_name (:obj:`str`): Human-readable name of the regular gift from which this unique @@ -279,6 +284,10 @@ class UniqueGift(TelegramObject): model (:class:`telegram.UniqueGiftModel`): Model of the gift. symbol (:class:`telegram.UniqueGiftSymbol`): Symbol of the gift. backdrop (:class:`telegram.UniqueGiftBackdrop`): Backdrop of the gift. + publisher_chat (:class:`telegram.Chat`): Optional. Information about the chat that + published the gift. + + .. versionadded:: NEXT.VERSION """ @@ -288,6 +297,7 @@ class UniqueGift(TelegramObject): "model", "name", "number", + "publisher_chat", "symbol", ) @@ -299,6 +309,7 @@ def __init__( model: UniqueGiftModel, symbol: UniqueGiftSymbol, backdrop: UniqueGiftBackdrop, + publisher_chat: Optional[Chat] = None, *, api_kwargs: Optional[JSONDict] = None, ): @@ -309,6 +320,7 @@ def __init__( self.model: UniqueGiftModel = model self.symbol: UniqueGiftSymbol = symbol self.backdrop: UniqueGiftBackdrop = backdrop + self.publisher_chat: Optional[Chat] = publisher_chat self._id_attrs = ( self.base_name, @@ -329,6 +341,7 @@ def de_json(cls, data: JSONDict, bot: Optional["Bot"] = None) -> "UniqueGift": data["model"] = de_json_optional(data.get("model"), UniqueGiftModel, bot) data["symbol"] = de_json_optional(data.get("symbol"), UniqueGiftSymbol, bot) data["backdrop"] = de_json_optional(data.get("backdrop"), UniqueGiftBackdrop, bot) + data["publisher_chat"] = de_json_optional(data.get("publisher_chat"), Chat, bot) return super().de_json(data=data, bot=bot) diff --git a/src/telegram/_user.py b/src/telegram/_user.py index ca9cd637193..3d49931ca1d 100644 --- a/src/telegram/_user.py +++ b/src/telegram/_user.py @@ -61,6 +61,7 @@ PhotoSize, ReplyParameters, Sticker, + SuggestedPostParameters, UserChatBoosts, UserProfilePhotos, Venue, @@ -435,6 +436,8 @@ async def send_message( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, disable_web_page_preview: Optional[bool] = None, @@ -480,6 +483,8 @@ async def send_message( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def delete_message( @@ -562,6 +567,8 @@ async def send_photo( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -609,6 +616,8 @@ async def send_photo( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_media_group( @@ -623,6 +632,7 @@ async def send_media_group( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -669,6 +679,7 @@ async def send_media_group( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, ) async def send_audio( @@ -689,6 +700,8 @@ async def send_audio( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -738,6 +751,8 @@ async def send_audio( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_chat_action( @@ -794,6 +809,8 @@ async def send_contact( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -839,6 +856,8 @@ async def send_contact( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_dice( @@ -852,6 +871,8 @@ async def send_dice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -892,6 +913,8 @@ async def send_dice( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_document( @@ -910,6 +933,8 @@ async def send_document( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -957,6 +982,8 @@ async def send_document( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_game( @@ -1042,6 +1069,8 @@ async def send_invoice( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1111,6 +1140,8 @@ async def send_invoice( message_thread_id=message_thread_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_location( @@ -1129,6 +1160,8 @@ async def send_location( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1176,6 +1209,8 @@ async def send_location( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_animation( @@ -1198,6 +1233,8 @@ async def send_animation( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1249,6 +1286,8 @@ async def send_animation( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_sticker( @@ -1263,6 +1302,8 @@ async def send_sticker( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1304,6 +1345,8 @@ async def send_sticker( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video( @@ -1329,6 +1372,8 @@ async def send_video( show_caption_above_media: Optional[bool] = None, cover: Optional[FileInput] = None, start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1383,6 +1428,8 @@ async def send_video( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_venue( @@ -1403,6 +1450,8 @@ async def send_venue( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1452,6 +1501,8 @@ async def send_venue( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video_note( @@ -1468,6 +1519,8 @@ async def send_video_note( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1513,6 +1566,8 @@ async def send_video_note( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_voice( @@ -1530,6 +1585,8 @@ async def send_voice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1576,6 +1633,8 @@ async def send_voice( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_poll( @@ -1752,6 +1811,8 @@ async def send_copy( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1796,6 +1857,8 @@ async def send_copy( message_thread_id=message_thread_id, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def copy_message( @@ -1813,6 +1876,8 @@ async def copy_message( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -1857,6 +1922,8 @@ async def copy_message( message_thread_id=message_thread_id, show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_copies( @@ -1867,6 +1934,7 @@ async def send_copies( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, remove_caption: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -1902,6 +1970,7 @@ async def send_copies( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def copy_messages( @@ -1912,6 +1981,7 @@ async def copy_messages( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, remove_caption: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -1947,6 +2017,7 @@ async def copy_messages( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def forward_from( @@ -1957,6 +2028,8 @@ async def forward_from( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -1991,6 +2064,8 @@ async def forward_from( api_kwargs=api_kwargs, protect_content=protect_content, message_thread_id=message_thread_id, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def forward_to( @@ -2001,6 +2076,8 @@ async def forward_to( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2036,6 +2113,8 @@ async def forward_to( api_kwargs=api_kwargs, protect_content=protect_content, message_thread_id=message_thread_id, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def forward_messages_from( @@ -2045,6 +2124,7 @@ async def forward_messages_from( disable_notification: ODVInput[bool] = DEFAULT_NONE, protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2079,6 +2159,7 @@ async def forward_messages_from( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def forward_messages_to( @@ -2088,6 +2169,7 @@ async def forward_messages_to( disable_notification: ODVInput[bool] = DEFAULT_NONE, protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2122,6 +2204,7 @@ async def forward_messages_to( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=api_kwargs, + direct_messages_topic_id=direct_messages_topic_id, ) async def approve_join_request( diff --git a/src/telegram/constants.py b/src/telegram/constants.py index a403a78e0cd..9641643028c 100644 --- a/src/telegram/constants.py +++ b/src/telegram/constants.py @@ -117,6 +117,9 @@ "StoryAreaTypeLimit", "StoryAreaTypeType", "StoryLimit", + "SuggestedPost", + "SuggestedPostInfoState", + "SuggestedPostRefunded", "TransactionPartnerType", "TransactionPartnerUser", "UniqueGiftInfoOrigin", @@ -173,7 +176,7 @@ class _AccentColor(NamedTuple): #: :data:`telegram.__bot_api_version_info__`. #: #: .. versionadded:: 20.0 -BOT_API_VERSION_INFO: Final[_BotAPIVersion] = _BotAPIVersion(major=9, minor=1) +BOT_API_VERSION_INFO: Final[_BotAPIVersion] = _BotAPIVersion(major=9, minor=2) #: :obj:`str`: Telegram Bot API #: version supported by this version of `python-telegram-bot`. Also available as #: :data:`telegram.__bot_api_version__`. @@ -2220,6 +2223,36 @@ class MessageType(StringEnum): .. versionadded:: v22.2 """ + SUGGESTED_POST_APPROVAL_FAILED = "suggested_post_approval_failed" + """:obj:`str`: Messages with :attr:`telegram.Message.suggested_post_approval_failed`. + + .. versionadded:: NEXT.VERSION + """ + SUGGESTED_POST_APPROVED = "suggested_post_approved" + """:obj:`str`: Messages with :attr:`telegram.Message.suggested_post_approved`. + + .. versionadded:: NEXT.VERSION + """ + SUGGESTED_POST_DECLINED = "suggested_post_declined" + """:obj:`str`: Messages with :attr:`telegram.Message.suggested_post_declined`. + + .. versionadded:: NEXT.VERSION + """ + SUGGESTED_POST_INFO = "suggested_post_info" + """:obj:`str`: Messages with :attr:`telegram.Message.suggested_post_info`. + + .. versionadded:: NEXT.VERSION + """ + SUGGESTED_POST_PAID = "suggested_post_paid" + """:obj:`str`: Messages with :attr:`telegram.Message.suggested_post_paid`. + + .. versionadded:: NEXT.VERSION + """ + SUGGESTED_POST_REFUNDED = "suggested_post_refunded" + """:obj:`str`: Messages with :attr:`telegram.Message.suggested_post_refunded`. + + .. versionadded:: NEXT.VERSION + """ PASSPORT_DATA = "passport_data" """:obj:`str`: Messages with :attr:`telegram.Message.passport_data`.""" PHOTO = "photo" @@ -3054,6 +3087,66 @@ class StoryLimit(StringEnum): :meth:`telegram.Bot.post_story`.""" +class SuggestedPost(IntEnum): + """This enum contains limitations for :class:`telegram.SuggestedPostPrice`\ +/:class:`telegram.SuggestedPostParameters`/:meth:`telegram.Bot.decline_suggested_post`. The enum + members of this enumeration are instances of :class:`int` and can be treated as such. + + .. versionadded:: NEXT.VERSION + """ + + __slots__ = () + + MIN_PRICE_STARS = 5 + """:obj:`int`: Minimum number of Telegram Stars in + :paramref:`~telegram.SuggestedPostPrice.amount` + parameter of :class:`telegram.SuggestedPostPrice`. + """ + MAX_PRICE_STARS = 100_000 + """:obj:`int`: Maximum number of Telegram Stars in + :paramref:`~telegram.SuggestedPostPrice.amount` + parameter of :class:`telegram.SuggestedPostPrice`. + """ + MIN_PRICE_NANOTONCOINS = 10_000_000 + """:obj:`int`: Minimum number of nanotoncoins in + :paramref:`~telegram.SuggestedPostPrice.amount` + parameter of :class:`telegram.SuggestedPostPrice`. + """ + MAX_PRICE_NANOTONCOINS = 10_000_000_000_000 + """:obj:`int`: Maximum number of nanotoncoins in + :paramref:`~telegram.SuggestedPostPrice.amount` + parameter of :class:`telegram.SuggestedPostPrice`. + """ + MIN_SEND_DATE = 300 + """:obj:`int`: Minimum number of seconds in the future for + the :paramref:`~telegram.SuggestedPostParameters.send_date` parameter of + :class:`telegram.SuggestedPostParameters`.""" + MAX_SEND_DATE = 2_678_400 + """:obj:`int`: Maximum number of seconds in the future for + the :paramref:`~telegram.SuggestedPostParameters.send_date` parameter of + :class:`telegram.SuggestedPostParameters`.""" + MAX_COMMENT_LENGTH = 128 + """:obj:`int`: Maximum number of characters in the + :paramref:`telegram.Bot.decline_suggested_post.comment` parameter. + """ + + +class SuggestedPostRefunded(StringEnum): + """This enum contains available refund reasons for :class:`telegram.SuggestedPostRefunded`. + The enum members of this enumeration are instances of :class:`str` and can be treated as such. + + .. versionadded:: NEXT.VERSION + """ + + __slots__ = () + + POST_DELETED = "post_deleted" + """:obj:`str`: The post was deleted within 24 hours of being posted or removed from + scheduled messages without being posted.""" + PAYMENT_REFUNDED = "payment_refunded" + """:obj:`str`: The payer refunded their payment.""" + + class TransactionPartnerType(StringEnum): """This enum contains the available types of :class:`telegram.TransactionPartner`. The enum members of this enumeration are instances of :class:`str` and can be treated as such. @@ -3528,6 +3621,25 @@ class ForumTopicLimit(IntEnum): """ +class SuggestedPostInfoState(StringEnum): + """This enum contains the available states of :attr:`telegram.SuggestedPostInfo.state`. + The enum members of this enumeration are instances + of :class:`str` and can be treated as such. + + .. versionadded:: NEXT.VERSION + """ + + __slots__ = () + + PENDING = "pending" + """:obj:`str`: Suggested post is pending.""" + APPROVED = "approved" + """:obj:`str`: Suggested post was approved.""" + DECLINED = "declined" + """:obj:`str`: Suggested post was declined. + """ + + class ReactionType(StringEnum): """This enum contains the available types of :class:`telegram.ReactionType`. The enum members of this enumeration are instances of :class:`str` and can be treated as such. diff --git a/src/telegram/ext/_extbot.py b/src/telegram/ext/_extbot.py index 1f9e14644c9..78b24727d1c 100644 --- a/src/telegram/ext/_extbot.py +++ b/src/telegram/ext/_extbot.py @@ -129,6 +129,7 @@ PassportElementError, ShippingOption, StoryArea, + SuggestedPostParameters, ) from telegram.ext import BaseRateLimiter, Defaults @@ -617,6 +618,8 @@ async def _send_message( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -650,6 +653,8 @@ async def _send_message( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) if isinstance(result, Message): self._insert_callback_data(result) @@ -829,6 +834,8 @@ async def copy_message( show_caption_above_media: Optional[bool] = None, allow_paid_broadcast: Optional[bool] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -862,6 +869,8 @@ async def copy_message( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), show_caption_above_media=show_caption_above_media, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def copy_messages( @@ -873,6 +882,7 @@ async def copy_messages( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, remove_caption: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -895,6 +905,7 @@ async def copy_messages( connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), + direct_messages_topic_id=direct_messages_topic_id, ) async def get_chat( @@ -1768,6 +1779,8 @@ async def forward_message( protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, video_start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -1784,11 +1797,13 @@ async def forward_message( disable_notification=disable_notification, protect_content=protect_content, message_thread_id=message_thread_id, + suggested_post_parameters=suggested_post_parameters, read_timeout=read_timeout, write_timeout=write_timeout, connect_timeout=connect_timeout, pool_timeout=pool_timeout, api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), + direct_messages_topic_id=direct_messages_topic_id, ) async def forward_messages( @@ -1799,6 +1814,7 @@ async def forward_messages( disable_notification: ODVInput[bool] = DEFAULT_NONE, protect_content: ODVInput[bool] = DEFAULT_NONE, message_thread_id: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -1814,6 +1830,7 @@ async def forward_messages( disable_notification=disable_notification, protect_content=protect_content, message_thread_id=message_thread_id, + direct_messages_topic_id=direct_messages_topic_id, read_timeout=read_timeout, write_timeout=write_timeout, connect_timeout=connect_timeout, @@ -2340,6 +2357,7 @@ async def promote_chat_member( can_post_stories: Optional[bool] = None, can_edit_stories: Optional[bool] = None, can_delete_stories: Optional[bool] = None, + can_manage_direct_messages: Optional[bool] = None, *, read_timeout: ODVInput[float] = DEFAULT_NONE, write_timeout: ODVInput[float] = DEFAULT_NONE, @@ -2366,6 +2384,7 @@ async def promote_chat_member( can_post_stories=can_post_stories, can_edit_stories=can_edit_stories, can_delete_stories=can_delete_stories, + can_manage_direct_messages=can_manage_direct_messages, read_timeout=read_timeout, write_timeout=write_timeout, connect_timeout=connect_timeout, @@ -2466,6 +2485,8 @@ async def send_animation( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2505,6 +2526,8 @@ async def send_animation( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_audio( @@ -2526,6 +2549,8 @@ async def send_audio( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2563,6 +2588,8 @@ async def send_audio( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_chat_action( @@ -2606,6 +2633,8 @@ async def send_contact( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2638,7 +2667,9 @@ async def send_contact( business_connection_id=business_connection_id, api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, + direct_messages_topic_id=direct_messages_topic_id, allow_paid_broadcast=allow_paid_broadcast, + suggested_post_parameters=suggested_post_parameters, ) async def send_checklist( @@ -2719,6 +2750,8 @@ async def send_dice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2747,6 +2780,8 @@ async def send_dice( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_document( @@ -2766,6 +2801,8 @@ async def send_document( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2801,6 +2838,8 @@ async def send_document( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_game( @@ -2876,6 +2915,8 @@ async def send_invoice( reply_parameters: Optional["ReplyParameters"] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2923,6 +2964,8 @@ async def send_invoice( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_location( @@ -2942,6 +2985,8 @@ async def send_location( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -2977,6 +3022,8 @@ async def send_location( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_media_group( @@ -2992,6 +3039,7 @@ async def send_media_group( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3025,6 +3073,7 @@ async def send_media_group( caption_entities=caption_entities, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, ) async def send_message( @@ -3042,6 +3091,8 @@ async def send_message( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, disable_web_page_preview: Optional[bool] = None, reply_to_message_id: Optional[int] = None, @@ -3075,6 +3126,8 @@ async def send_message( link_preview_options=link_preview_options, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_photo( @@ -3094,6 +3147,8 @@ async def send_photo( message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, show_caption_above_media: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3129,6 +3184,8 @@ async def send_photo( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_poll( @@ -3212,6 +3269,8 @@ async def send_sticker( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3241,6 +3300,8 @@ async def send_sticker( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_venue( @@ -3262,6 +3323,8 @@ async def send_venue( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3299,6 +3362,8 @@ async def send_venue( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video( @@ -3325,6 +3390,8 @@ async def send_video( show_caption_above_media: Optional[bool] = None, cover: Optional[FileInput] = None, start_timestamp: Optional[int] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3367,6 +3434,8 @@ async def send_video( message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, show_caption_above_media=show_caption_above_media, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_video_note( @@ -3384,6 +3453,8 @@ async def send_video_note( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3417,6 +3488,8 @@ async def send_video_note( business_connection_id=business_connection_id, message_effect_id=message_effect_id, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, ) async def send_voice( @@ -3435,6 +3508,8 @@ async def send_voice( business_connection_id: Optional[str] = None, message_effect_id: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, *, reply_to_message_id: Optional[int] = None, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, @@ -3468,7 +3543,9 @@ async def send_voice( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), business_connection_id=business_connection_id, message_effect_id=message_effect_id, + direct_messages_topic_id=direct_messages_topic_id, allow_paid_broadcast=allow_paid_broadcast, + suggested_post_parameters=suggested_post_parameters, ) async def set_chat_administrator_custom_title( @@ -4907,6 +4984,9 @@ async def send_paid_media( business_connection_id: Optional[str] = None, payload: Optional[str] = None, allow_paid_broadcast: Optional[bool] = None, + direct_messages_topic_id: Optional[int] = None, + suggested_post_parameters: Optional["SuggestedPostParameters"] = None, + message_thread_id: Optional[int] = None, *, allow_sending_without_reply: ODVInput[bool] = DEFAULT_NONE, reply_to_message_id: Optional[int] = None, @@ -4939,6 +5019,9 @@ async def send_paid_media( business_connection_id=business_connection_id, payload=payload, allow_paid_broadcast=allow_paid_broadcast, + direct_messages_topic_id=direct_messages_topic_id, + suggested_post_parameters=suggested_post_parameters, + message_thread_id=message_thread_id, ) async def create_chat_subscription_invite_link( @@ -5143,6 +5226,54 @@ async def get_my_star_balance( api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), ) + async def decline_suggested_post( + self, + chat_id: int, + message_id: int, + comment: Optional[str] = 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, + rate_limit_args: Optional[RLARGS] = None, + ) -> bool: + return await super().decline_suggested_post( + chat_id=chat_id, + message_id=message_id, + comment=comment, + read_timeout=read_timeout, + write_timeout=write_timeout, + connect_timeout=connect_timeout, + pool_timeout=pool_timeout, + api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), + ) + + async def approve_suggested_post( + self, + chat_id: int, + message_id: int, + send_date: Optional[Union[int, dtm.datetime]] = 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, + rate_limit_args: Optional[RLARGS] = None, + ) -> bool: + return await super().approve_suggested_post( + chat_id=chat_id, + message_id=message_id, + send_date=send_date, + read_timeout=read_timeout, + write_timeout=write_timeout, + connect_timeout=connect_timeout, + pool_timeout=pool_timeout, + api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args), + ) + # updated camelCase aliases getMe = get_me sendMessage = send_message @@ -5299,3 +5430,5 @@ async def get_my_star_balance( removeChatVerification = remove_chat_verification removeUserVerification = remove_user_verification getMyStarBalance = get_my_star_balance + approveSuggestedPost = approve_suggested_post + declineSuggestedPost = decline_suggested_post diff --git a/src/telegram/ext/filters.py b/src/telegram/ext/filters.py index 80e09f30d41..4d8908e8140 100644 --- a/src/telegram/ext/filters.py +++ b/src/telegram/ext/filters.py @@ -49,6 +49,7 @@ "CHECKLIST", "COMMAND", "CONTACT", + "DIRECT_MESSAGES", "EFFECT_ID", "FORUM", "FORWARDED", @@ -72,6 +73,7 @@ "SENDER_BOOST_COUNT", "STORY", "SUCCESSFUL_PAYMENT", + "SUGGESTED_POST_INFO", "TEXT", "USER", "USER_ATTACHMENT", @@ -1149,6 +1151,22 @@ def __init__(self, values: SCT[int]): """Dice messages with the emoji 🎰. Matches any dice value.""" +class _DirectMessages(UpdateFilter): + __slots__ = () + + def filter(self, update: Update) -> bool: + return bool(update.effective_chat and update.effective_chat.is_direct_messages) + + +DIRECT_MESSAGES = _DirectMessages(name="filters.DIRECT_MESSAGES") +"""Filter chats which are the direct messages for a channel. + +.. seealso:: :attr:`telegram.Chat.is_direct_messages` + +.. versionadded:: NEXT.VERSION +""" + + class Document: """ Subset for messages containing a document/file. @@ -1972,6 +1990,11 @@ def filter(self, update: Update) -> bool: or StatusUpdate.PINNED_MESSAGE.check_update(update) or StatusUpdate.PROXIMITY_ALERT_TRIGGERED.check_update(update) or StatusUpdate.REFUNDED_PAYMENT.check_update(update) + or StatusUpdate.SUGGESTED_POST_APPROVAL_FAILED.check_update(update) + or StatusUpdate.SUGGESTED_POST_APPROVED.check_update(update) + or StatusUpdate.SUGGESTED_POST_DECLINED.check_update(update) + or StatusUpdate.SUGGESTED_POST_PAID.check_update(update) + or StatusUpdate.SUGGESTED_POST_REFUNDED.check_update(update) or StatusUpdate.UNIQUE_GIFT.check_update(update) or StatusUpdate.USERS_SHARED.check_update(update) or StatusUpdate.VIDEO_CHAT_ENDED.check_update(update) @@ -2293,6 +2316,69 @@ def filter(self, message: Message) -> bool: .. versionadded:: 21.4 """ + class _SuggestedPostApprovalFailed(MessageFilter): + __slots__ = () + + def filter(self, message: Message) -> bool: + return bool(message.suggested_post_approval_failed) + + SUGGESTED_POST_APPROVAL_FAILED = _SuggestedPostApprovalFailed( + "filters.StatusUpdate.SUGGESTED_POST_APPROVAL_FAILED" + ) + """Messages that contain :attr:`telegram.Message.suggested_post_approval_failed`. + .. versionadded:: NEXT.VERSION + """ + + class _SuggestedPostApproved(MessageFilter): + __slots__ = () + + def filter(self, message: Message) -> bool: + return bool(message.suggested_post_approved) + + SUGGESTED_POST_APPROVED = _SuggestedPostApproved( + "filters.StatusUpdate.SUGGESTED_POST_APPROVED" + ) + """Messages that contain :attr:`telegram.Message.suggested_post_approved`. + .. versionadded:: NEXT.VERSION + """ + + class _SuggestedPostDeclined(MessageFilter): + __slots__ = () + + def filter(self, message: Message) -> bool: + return bool(message.suggested_post_declined) + + SUGGESTED_POST_DECLINED = _SuggestedPostDeclined( + "filters.StatusUpdate.SUGGESTED_POST_DECLINED" + ) + """Messages that contain :attr:`telegram.Message.suggested_post_declined`. + .. versionadded:: NEXT.VERSION + """ + + class _SuggestedPostPaid(MessageFilter): + __slots__ = () + + def filter(self, message: Message) -> bool: + return bool(message.suggested_post_paid) + + SUGGESTED_POST_PAID = _SuggestedPostPaid("filters.StatusUpdate.SUGGESTED_POST_PAID") + """Messages that contain :attr:`telegram.Message.suggested_post_paid`. + .. versionadded:: NEXT.VERSION + """ + + class _SuggestedPostRefunded(MessageFilter): + __slots__ = () + + def filter(self, message: Message) -> bool: + return bool(message.suggested_post_refunded) + + SUGGESTED_POST_REFUNDED = _SuggestedPostRefunded( + "filters.StatusUpdate.SUGGESTED_POST_REFUNDED" + ) + """Messages that contain :attr:`telegram.Message.suggested_post_refunded`. + .. versionadded:: NEXT.VERSION + """ + class _UniqueGift(MessageFilter): __slots__ = () @@ -2539,6 +2625,20 @@ def filter(self, message: Message) -> bool: """Messages that contain :attr:`telegram.Message.successful_payment`.""" +class _SuggestedPostInfo(MessageFilter): + __slots__ = () + + def filter(self, message: Message) -> bool: + return bool(message.suggested_post_info) + + +SUGGESTED_POST_INFO = _SuggestedPostInfo(name="filters.SUGGESTED_POST_INFO") +"""Messages that contain :attr:`telegram.Message.suggested_post_info`. + +.. versionadded:: NEXT.VERSION +""" + + class Text(MessageFilter): """Text Messages. If a list of strings is passed, it filters messages to only allow those whose text is appearing in the given list. diff --git a/tests/ext/test_filters.py b/tests/ext/test_filters.py index bdb581da747..fdaa673f922 100644 --- a/tests/ext/test_filters.py +++ b/tests/ext/test_filters.py @@ -1101,6 +1101,31 @@ def test_filters_status_update(self, update): assert filters.StatusUpdate.REFUNDED_PAYMENT.check_update(update) update.message.refunded_payment = None + update.message.suggested_post_approval_failed = "suggested_post_approval_failed" + assert filters.StatusUpdate.ALL.check_update(update) + assert filters.StatusUpdate.SUGGESTED_POST_APPROVAL_FAILED.check_update(update) + update.message.suggested_post_approval_failed = None + + update.message.suggested_post_approved = "suggested_post_approved" + assert filters.StatusUpdate.ALL.check_update(update) + assert filters.StatusUpdate.SUGGESTED_POST_APPROVED.check_update(update) + update.message.suggested_post_approved = None + + update.message.suggested_post_declined = "suggested_post_declined" + assert filters.StatusUpdate.ALL.check_update(update) + assert filters.StatusUpdate.SUGGESTED_POST_DECLINED.check_update(update) + update.message.suggested_post_declined = None + + update.message.suggested_post_paid = "suggested_post_paid" + assert filters.StatusUpdate.ALL.check_update(update) + assert filters.StatusUpdate.SUGGESTED_POST_PAID.check_update(update) + update.message.suggested_post_paid = None + + update.message.suggested_post_refunded = "suggested_post_refunded" + assert filters.StatusUpdate.ALL.check_update(update) + assert filters.StatusUpdate.SUGGESTED_POST_REFUNDED.check_update(update) + update.message.suggested_post_refunded = None + update.message.gift = "gift" assert filters.StatusUpdate.ALL.check_update(update) assert filters.StatusUpdate.GIFT.check_update(update) @@ -2113,6 +2138,11 @@ def test_filters_successful_payment(self, update): update.message.successful_payment = "test" assert filters.SUCCESSFUL_PAYMENT.check_update(update) + def test_filters_suggested_post_info(self, update): + assert not filters.SUGGESTED_POST_INFO.check_update(update) + update.message.suggested_post_info = "test" + assert filters.SUGGESTED_POST_INFO.check_update(update) + def test_filters_successful_payment_payloads(self, update): assert not filters.SuccessfulPayment(("custom-payload",)).check_update(update) assert not filters.SuccessfulPayment().check_update(update) @@ -2819,3 +2849,10 @@ def test_filters_checklist(self, update): update.message.checklist = "test" assert filters.CHECKLIST.check_update(update) assert str(filters.CHECKLIST) == "filters.CHECKLIST" + + def test_filters_direct_messages(self, update): + assert not filters.DIRECT_MESSAGES.check_update(update) + + update.message.chat.is_direct_messages = True + assert filters.DIRECT_MESSAGES.check_update(update) + assert str(filters.DIRECT_MESSAGES) == "filters.DIRECT_MESSAGES" diff --git a/tests/test_bot.py b/tests/test_bot.py index 6ecf041f77a..e2c905c1adb 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -75,6 +75,8 @@ ShippingOption, StarTransaction, StarTransactions, + SuggestedPostParameters, + SuggestedPostPrice, Update, User, WebAppInfo, @@ -2373,6 +2375,32 @@ async def make_assertion(url, request_data: RequestData, *args, **kwargs): monkeypatch.setattr(offline_bot.request, "post", make_assertion) assert await offline_bot.send_message(2, "text", allow_paid_broadcast=42) + async def test_direct_messages_topic_id_argument(self, offline_bot, monkeypatch): + """We can't test every single method easily, so we just test one. Our linting will catch + any unused args with the others.""" + + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + return request_data.parameters.get("direct_messages_topic_id") == 42 + + monkeypatch.setattr(offline_bot.request, "post", make_assertion) + assert await offline_bot.send_message(2, "text", direct_messages_topic_id=42) + + async def test_suggested_post_parameters_argument(self, offline_bot, monkeypatch): + """We can't test every single method easily, so we just test one. Our linting will catch + any unused args with the others.""" + suggested_post_parameters = SuggestedPostParameters(price=SuggestedPostPrice("TON", 10)) + + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + return ( + request_data.parameters.get("suggested_post_parameters") + == suggested_post_parameters.to_dict() + ) + + monkeypatch.setattr(offline_bot.request, "post", make_assertion) + assert await offline_bot.send_message( + 2, "text", suggested_post_parameters=suggested_post_parameters + ) + async def test_send_chat_action_all_args(self, bot, chat_id, monkeypatch): async def make_assertion(*args, **_): kwargs = args[1] @@ -2586,6 +2614,51 @@ async def do_request(url, request_data: RequestData, *args, **kwargs): obj = await offline_bot.get_my_star_balance() assert isinstance(obj, StarAmount) + async def test_approve_suggested_post(self, offline_bot, monkeypatch): + "No way to test this without receiving suggested posts" + + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + data = request_data.json_parameters + chat_id = data.get("chat_id") == "1234" + message_id = data.get("message_id") == "5678" + send_date = data.get("send_date", "1577887200") == "1577887200" + return chat_id and message_id and send_date + + until = from_timestamp(1577887200) + monkeypatch.setattr(offline_bot.request, "post", make_assertion) + + assert await offline_bot.approve_suggested_post(1234, 5678, 1577887200) + assert await offline_bot.approve_suggested_post(1234, 5678, until) + + async def test_approve_suggested_post_with_tz(self, monkeypatch, tz_bot): + until = dtm.datetime(2020, 1, 11, 16, 13) + until_timestamp = to_timestamp(until, tzinfo=tz_bot.defaults.tzinfo) + + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + data = request_data.parameters + chat_id = data["chat_id"] == 2 + message_id = data["message_id"] == 32 + until_date = data.get("until_date", until_timestamp) == until_timestamp + return chat_id and message_id and until_date + + monkeypatch.setattr(tz_bot.request, "post", make_assertion) + + assert await tz_bot.approve_suggested_post(2, 32) + assert await tz_bot.approve_suggested_post(2, 32, send_date=until) + assert await tz_bot.approve_suggested_post(2, 32, send_date=until_timestamp) + + async def test_decline_suggested_post(self, offline_bot, monkeypatch): + "No way to test this without receiving suggested posts" + + async def make_assertion(url, request_data: RequestData, *args, **kwargs): + assert request_data.parameters.get("chat_id") == 1234 + assert request_data.parameters.get("message_id") == 5678 + assert request_data.parameters.get("comment") == "declined" + + monkeypatch.setattr(offline_bot.request, "post", make_assertion) + + await offline_bot.decline_suggested_post(1234, 5678, "declined") + class TestBotWithRequest: """ @@ -3552,6 +3625,7 @@ async def test_promote_chat_member(self, bot, channel_id, monkeypatch): can_post_stories=True, can_edit_stories=True, can_delete_stories=True, + can_manage_direct_messages=True, ) # Test that we pass the correct params to TG @@ -3575,6 +3649,7 @@ async def make_assertion(*args, **_): and data.get("can_post_stories") == 13 and data.get("can_edit_stories") == 14 and data.get("can_delete_stories") == 15 + and data.get("can_manage_direct_messages") == 16 ) monkeypatch.setattr(bot, "_post", make_assertion) @@ -3596,6 +3671,7 @@ async def make_assertion(*args, **_): can_post_stories=13, can_edit_stories=14, can_delete_stories=15, + can_manage_direct_messages=16, ) async def test_export_chat_invite_link(self, bot, channel_id): diff --git a/tests/test_callbackquery.py b/tests/test_callbackquery.py index 6b759e885cb..0cf81c53cbb 100644 --- a/tests/test_callbackquery.py +++ b/tests/test_callbackquery.py @@ -576,11 +576,14 @@ async def make_assertion(*args, **kwargs): assert check_shortcut_signature( CallbackQuery.copy_message, Bot.copy_message, - ["message_id", "from_chat_id"], + ["message_id", "from_chat_id", "direct_messages_topic_id"], [], ) assert await check_shortcut_call( - callback_query.copy_message, callback_query.get_bot(), "copy_message" + callback_query.copy_message, + callback_query.get_bot(), + "copy_message", + shortcut_kwargs=["direct_messages_topic_id"], ) assert await check_defaults_handling(callback_query.copy_message, callback_query.get_bot()) diff --git a/tests/test_chat.py b/tests/test_chat.py index 4651393e473..7ab0c2f1d0f 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -49,6 +49,7 @@ def chat(bot): is_forum=True, first_name=ChatTestBase.first_name, last_name=ChatTestBase.last_name, + is_direct_messages=ChatTestBase.is_direct_messages, ) chat.set_bot(bot) chat._unfreeze() @@ -63,6 +64,7 @@ class ChatTestBase: is_forum = True first_name = "first" last_name = "last" + is_direct_messages = True class TestChatWithoutRequest(ChatTestBase): @@ -80,6 +82,7 @@ def test_de_json(self, offline_bot): "is_forum": self.is_forum, "first_name": self.first_name, "last_name": self.last_name, + "is_direct_messages": self.is_direct_messages, } chat = Chat.de_json(json_dict, offline_bot) @@ -90,6 +93,7 @@ def test_de_json(self, offline_bot): assert chat.is_forum == self.is_forum assert chat.first_name == self.first_name assert chat.last_name == self.last_name + assert chat.is_direct_messages == self.is_direct_messages def test_to_dict(self, chat): chat_dict = chat.to_dict() @@ -102,6 +106,7 @@ def test_to_dict(self, chat): assert chat_dict["is_forum"] == chat.is_forum assert chat_dict["first_name"] == chat.first_name assert chat_dict["last_name"] == chat.last_name + assert chat_dict["is_direct_messages"] == chat.is_direct_messages def test_enum_init(self): chat = Chat(id=1, type="foo") @@ -1452,6 +1457,44 @@ async def make_assertion(*_, **kwargs): message_id="message_id", business_connection_id="business_connection_id" ) + async def test_instance_method_approve_suggested_post(self, monkeypatch, chat): + async def make_assertion(*_, **kwargs): + return ( + kwargs["chat_id"] == chat.id + and kwargs["message_id"] == "message_id" + and kwargs["send_date"] == "send_date" + ) + + assert check_shortcut_signature( + Chat.approve_suggested_post, Bot.approve_suggested_post, ["chat_id"], [] + ) + assert await check_shortcut_call( + chat.approve_suggested_post, chat.get_bot(), "approve_suggested_post" + ) + assert await check_defaults_handling(chat.approve_suggested_post, chat.get_bot()) + + monkeypatch.setattr(chat.get_bot(), "approve_suggested_post", make_assertion) + assert await chat.approve_suggested_post(message_id="message_id", send_date="send_date") + + async def test_instance_method_decline_suggested_post(self, monkeypatch, chat): + async def make_assertion(*_, **kwargs): + return ( + kwargs["chat_id"] == chat.id + and kwargs["message_id"] == "message_id" + and kwargs["comment"] == "comment" + ) + + assert check_shortcut_signature( + Chat.decline_suggested_post, Bot.decline_suggested_post, ["chat_id"], [] + ) + assert await check_shortcut_call( + chat.decline_suggested_post, chat.get_bot(), "decline_suggested_post" + ) + assert await check_defaults_handling(chat.decline_suggested_post, chat.get_bot()) + + monkeypatch.setattr(chat.get_bot(), "decline_suggested_post", make_assertion) + assert await chat.decline_suggested_post(message_id="message_id", comment="comment") + def test_mention_html(self): chat = Chat(id=1, type="foo") with pytest.raises(TypeError, match="Can not create a mention to a private group chat"): diff --git a/tests/test_chatadministratorrights.py b/tests/test_chatadministratorrights.py index c93f23d4bcd..76a6f06e7cc 100644 --- a/tests/test_chatadministratorrights.py +++ b/tests/test_chatadministratorrights.py @@ -40,6 +40,7 @@ def chat_admin_rights(): can_post_stories=True, can_edit_stories=True, can_delete_stories=True, + can_manage_direct_messages=True, ) @@ -67,6 +68,7 @@ def test_de_json(self, offline_bot, chat_admin_rights): "can_post_stories": True, "can_edit_stories": True, "can_delete_stories": True, + "can_manage_direct_messages": True, } chat_administrator_rights_de = ChatAdministratorRights.de_json(json_dict, offline_bot) assert chat_administrator_rights_de.api_kwargs == {} @@ -93,6 +95,7 @@ def test_to_dict(self, chat_admin_rights): assert admin_rights_dict["can_post_stories"] == car.can_post_stories assert admin_rights_dict["can_edit_stories"] == car.can_edit_stories assert admin_rights_dict["can_delete_stories"] == car.can_delete_stories + assert admin_rights_dict["can_manage_direct_messages"] == car.can_manage_direct_messages def test_equality(self): a = ChatAdministratorRights( @@ -143,6 +146,7 @@ def test_all_rights(self): True, True, True, + True, ) t = ChatAdministratorRights.all_rights() # if the dirs are the same, the attributes will all be there @@ -168,6 +172,7 @@ def test_no_rights(self): False, False, False, + False, ) t = ChatAdministratorRights.no_rights() # if the dirs are the same, the attributes will all be there diff --git a/tests/test_chatfullinfo.py b/tests/test_chatfullinfo.py index ebcdd6a71cc..79d55e2fa8b 100644 --- a/tests/test_chatfullinfo.py +++ b/tests/test_chatfullinfo.py @@ -87,6 +87,8 @@ def chat_full_info(bot): first_name=ChatFullInfoTestBase.first_name, last_name=ChatFullInfoTestBase.last_name, can_send_paid_media=ChatFullInfoTestBase.can_send_paid_media, + is_direct_messages=ChatFullInfoTestBase.is_direct_messages, + parent_chat=ChatFullInfoTestBase.parent_chat, ) chat.set_bot(bot) chat._unfreeze() @@ -146,6 +148,8 @@ class ChatFullInfoTestBase: last_name = "last_name" can_send_paid_media = True accepted_gift_types = AcceptedGiftTypes(True, True, True, True) + is_direct_messages = True + parent_chat = Chat(4, "channel", "channel") class TestChatFullInfoWithoutRequest(ChatFullInfoTestBase): @@ -201,6 +205,8 @@ def test_de_json(self, offline_bot): "first_name": self.first_name, "last_name": self.last_name, "can_send_paid_media": self.can_send_paid_media, + "is_direct_messages": self.is_direct_messages, + "parent_chat": self.parent_chat.to_dict(), } cfi = ChatFullInfo.de_json(json_dict, offline_bot) @@ -250,6 +256,8 @@ def test_de_json(self, offline_bot): assert cfi.last_name == self.last_name assert cfi.max_reaction_count == self.max_reaction_count assert cfi.can_send_paid_media == self.can_send_paid_media + assert cfi.is_direct_messages == self.is_direct_messages + assert cfi.parent_chat == self.parent_chat def test_de_json_localization(self, offline_bot, raw_bot, tz_bot): json_dict = { @@ -331,6 +339,8 @@ def test_to_dict(self, chat_full_info): assert cfi_dict["accepted_gift_types"] == cfi.accepted_gift_types.to_dict() assert cfi_dict["max_reaction_count"] == cfi.max_reaction_count + assert cfi_dict["is_direct_messages"] == cfi.is_direct_messages + assert cfi_dict["parent_chat"] == cfi.parent_chat.to_dict() def test_time_period_properties(self, PTB_TIMEDELTA, chat_full_info): cfi = chat_full_info diff --git a/tests/test_chatmember.py b/tests/test_chatmember.py index fdf6136f701..5453e470b2b 100644 --- a/tests/test_chatmember.py +++ b/tests/test_chatmember.py @@ -75,6 +75,7 @@ class ChatMemberTestBase: can_send_voice_notes = True can_send_messages = True is_member = True + can_manage_direct_messages = True class TestChatMemberWithoutRequest(ChatMemberTestBase): @@ -170,6 +171,7 @@ def chat_member_administrator(): TestChatMemberAdministratorWithoutRequest.can_restrict_members, TestChatMemberAdministratorWithoutRequest.custom_title, TestChatMemberAdministratorWithoutRequest.is_anonymous, + TestChatMemberAdministratorWithoutRequest.can_manage_direct_messages, ) @@ -202,6 +204,7 @@ def test_de_json(self, offline_bot): "can_restrict_members": self.can_restrict_members, "custom_title": self.custom_title, "is_anonymous": self.is_anonymous, + "can_manage_direct_messages": self.can_manage_direct_messages, } chat_member = ChatMemberAdministrator.de_json(data, offline_bot) @@ -226,6 +229,7 @@ def test_de_json(self, offline_bot): assert chat_member.can_restrict_members == self.can_restrict_members assert chat_member.custom_title == self.custom_title assert chat_member.is_anonymous == self.is_anonymous + assert chat_member.can_manage_direct_messages == self.can_manage_direct_messages def test_to_dict(self, chat_member_administrator): assert chat_member_administrator.to_dict() == { @@ -248,6 +252,7 @@ def test_to_dict(self, chat_member_administrator): "can_restrict_members": chat_member_administrator.can_restrict_members, "custom_title": chat_member_administrator.custom_title, "is_anonymous": chat_member_administrator.is_anonymous, + "can_manage_direct_messages": chat_member_administrator.can_manage_direct_messages, } def test_equality(self, chat_member_administrator): @@ -266,6 +271,7 @@ def test_equality(self, chat_member_administrator): True, True, True, + True, ) c = ChatMemberAdministrator( User(1, "test_user", is_bot=False), @@ -281,6 +287,7 @@ def test_equality(self, chat_member_administrator): False, False, False, + False, ) d = Dice(5, "test") diff --git a/tests/test_constants.py b/tests/test_constants.py index b7cc6483627..db988a3d889 100644 --- a/tests/test_constants.py +++ b/tests/test_constants.py @@ -182,6 +182,7 @@ def is_type_attribute(name: str) -> bool: "caption", "chat", "chat_id", + "direct_messages_topic", "effective_attachment", "entities", "from_user", @@ -204,6 +205,8 @@ def is_type_attribute(name: str) -> bool: "is_from_offline", "show_caption_above_media", "paid_star_count", + "is_paid_post", + "reply_to_checklist_task_id", } @pytest.mark.parametrize( diff --git a/tests/test_directmessagestopic.py b/tests/test_directmessagestopic.py new file mode 100644 index 00000000000..6d086b4b104 --- /dev/null +++ b/tests/test_directmessagestopic.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015-2025 +# Leandro Toledo de Souza +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. +"""This module contains the TestDirectMessagesTopic class.""" + +import pytest + +from telegram import DirectMessagesTopic, User +from tests.auxil.slots import mro_slots + + +@pytest.fixture(scope="module") +def direct_messages_topic(offline_bot): + dmt = DirectMessagesTopic( + topic_id=DirectMessagesTopicTestBase.topic_id, + user=DirectMessagesTopicTestBase.user, + ) + dmt.set_bot(offline_bot) + dmt._unfreeze() + return dmt + + +class DirectMessagesTopicTestBase: + topic_id = 12345 + user = User(id=67890, is_bot=False, first_name="Test") + + +class TestDirectMessagesTopicWithoutRequest(DirectMessagesTopicTestBase): + def test_slot_behaviour(self, direct_messages_topic): + cfi = direct_messages_topic + for attr in cfi.__slots__: + assert getattr(cfi, attr, "err") != "err", f"got extra slot '{attr}'" + + assert len(mro_slots(cfi)) == len(set(mro_slots(cfi))), "duplicate slot" + + def test_de_json(self, offline_bot): + json_dict = { + "topic_id": self.topic_id, + "user": self.user.to_dict(), + } + + dmt = DirectMessagesTopic.de_json(json_dict, offline_bot) + assert dmt.topic_id == self.topic_id + assert dmt.user == self.user + assert dmt.api_kwargs == {} + + def test_to_dict(self, direct_messages_topic): + dmt = direct_messages_topic + dmt_dict = dmt.to_dict() + + assert isinstance(dmt_dict, dict) + assert dmt_dict["topic_id"] == dmt.topic_id + assert dmt_dict["user"] == dmt.user.to_dict() + + def test_equality(self, direct_messages_topic): + dmt_1 = direct_messages_topic + dmt_2 = DirectMessagesTopic( + topic_id=dmt_1.topic_id, + user=dmt_1.user, + ) + assert dmt_1 == dmt_2 + assert hash(dmt_1) == hash(dmt_2) + + random = User(id=99999, is_bot=False, first_name="Random") + assert random != dmt_2 + assert hash(random) != hash(dmt_2) + + dmt_3 = DirectMessagesTopic( + topic_id=8371, + user=dmt_1.user, + ) + assert dmt_1 != dmt_3 + assert hash(dmt_1) != hash(dmt_3) diff --git a/tests/test_gifts.py b/tests/test_gifts.py index e1f10d43564..b1f8190cfb2 100644 --- a/tests/test_gifts.py +++ b/tests/test_gifts.py @@ -20,7 +20,7 @@ import pytest -from telegram import BotCommand, Gift, GiftInfo, Gifts, MessageEntity, Sticker +from telegram import BotCommand, Chat, Gift, GiftInfo, Gifts, MessageEntity, Sticker from telegram._gifts import AcceptedGiftTypes from telegram._utils.defaultvalue import DEFAULT_NONE from telegram.request import RequestData @@ -36,6 +36,7 @@ def gift(request): total_count=GiftTestBase.total_count, remaining_count=GiftTestBase.remaining_count, upgrade_star_count=GiftTestBase.upgrade_star_count, + publisher_chat=GiftTestBase.publisher_chat, ) @@ -54,6 +55,7 @@ class GiftTestBase: total_count = 10 remaining_count = 5 upgrade_star_count = 10 + publisher_chat = Chat(1, Chat.PRIVATE) class TestGiftWithoutRequest(GiftTestBase): @@ -70,6 +72,7 @@ def test_de_json(self, offline_bot, gift): "total_count": self.total_count, "remaining_count": self.remaining_count, "upgrade_star_count": self.upgrade_star_count, + "publisher_chat": self.publisher_chat.to_dict(), } gift = Gift.de_json(json_dict, offline_bot) assert gift.api_kwargs == {} @@ -80,6 +83,7 @@ def test_de_json(self, offline_bot, gift): assert gift.total_count == self.total_count assert gift.remaining_count == self.remaining_count assert gift.upgrade_star_count == self.upgrade_star_count + assert gift.publisher_chat == self.publisher_chat def test_to_dict(self, gift): gift_dict = gift.to_dict() @@ -91,6 +95,7 @@ def test_to_dict(self, gift): assert gift_dict["total_count"] == self.total_count assert gift_dict["remaining_count"] == self.remaining_count assert gift_dict["upgrade_star_count"] == self.upgrade_star_count + assert gift_dict["publisher_chat"] == self.publisher_chat.to_dict() def test_equality(self, gift): a = gift @@ -101,6 +106,7 @@ def test_equality(self, gift): self.total_count, self.remaining_count, self.upgrade_star_count, + self.publisher_chat, ) c = Gift( "other_uid", @@ -109,6 +115,7 @@ def test_equality(self, gift): self.total_count, self.remaining_count, self.upgrade_star_count, + self.publisher_chat, ) d = BotCommand("start", "description") @@ -210,6 +217,7 @@ class GiftsTestBase: total_count=5, remaining_count=5, upgrade_star_count=5, + publisher_chat=Chat(5, Chat.PRIVATE), ), Gift( id="id2", @@ -226,6 +234,7 @@ class GiftsTestBase: total_count=6, remaining_count=6, upgrade_star_count=6, + publisher_chat=Chat(6, Chat.PRIVATE), ), Gift( id="id3", @@ -242,6 +251,7 @@ class GiftsTestBase: total_count=7, remaining_count=7, upgrade_star_count=7, + publisher_chat=Chat(7, Chat.PRIVATE), ), ] @@ -265,6 +275,7 @@ def test_de_json(self, offline_bot, gifts): assert de_json_gift.total_count == original_gift.total_count assert de_json_gift.remaining_count == original_gift.remaining_count assert de_json_gift.upgrade_star_count == original_gift.upgrade_star_count + assert de_json_gift.publisher_chat == original_gift.publisher_chat def test_to_dict(self, gifts): gifts_dict = gifts.to_dict() diff --git a/tests/test_message.py b/tests/test_message.py index 5fbfcee4925..81fa66f792d 100644 --- a/tests/test_message.py +++ b/tests/test_message.py @@ -71,6 +71,13 @@ Sticker, Story, SuccessfulPayment, + SuggestedPostApprovalFailed, + SuggestedPostApproved, + SuggestedPostDeclined, + SuggestedPostInfo, + SuggestedPostPaid, + SuggestedPostPrice, + SuggestedPostRefunded, TextQuote, UniqueGift, UniqueGiftBackdrop, @@ -91,6 +98,7 @@ Voice, WebAppData, ) +from telegram._directmessagestopic import DirectMessagesTopic from telegram._utils.datetime import UTC from telegram._utils.defaultvalue import DEFAULT_NONE from telegram._utils.types import ODVInput @@ -355,6 +363,63 @@ def message(bot): tasks=[ChecklistTask(id=42, text="task 1"), ChecklistTask(id=43, text="task 2")], ) }, + {"is_paid_post": True}, + { + "direct_messages_topic": DirectMessagesTopic( + topic_id=1234, + user=User(id=5678, first_name="TestUser", is_bot=False), + ) + }, + {"reply_to_checklist_task_id": 11}, + { + "suggested_post_declined": SuggestedPostDeclined( + suggested_post_message=Message( + 7, dtm.datetime.utcnow(), Chat(13, "channel"), User(9, "i", False) + ), + comment="comment", + ) + }, + { + "suggested_post_paid": SuggestedPostPaid( + currency="XTR", + suggested_post_message=Message( + 7, dtm.datetime.utcnow(), Chat(13, "channel"), User(9, "i", False) + ), + amount=100, + ) + }, + { + "suggested_post_refunded": SuggestedPostRefunded( + reason="post_deleted", + suggested_post_message=Message( + 7, dtm.datetime.utcnow(), Chat(13, "channel"), User(9, "i", False) + ), + ) + }, + { + "suggested_post_approved": SuggestedPostApproved( + send_date=dtm.datetime.utcnow(), + price=SuggestedPostPrice(currency="XTR", amount=100), + suggested_post_message=Message( + 7, dtm.datetime.utcnow(), Chat(13, "channel"), User(9, "i", False) + ), + ) + }, + { + "suggested_post_approval_failed": SuggestedPostApprovalFailed( + price=SuggestedPostPrice(currency="XTR", amount=100), + suggested_post_message=Message( + 7, dtm.datetime.utcnow(), Chat(13, "channel"), User(9, "i", False) + ), + ) + }, + { + "suggested_post_info": SuggestedPostInfo( + state="pending", + price=SuggestedPostPrice(currency="XTR", amount=100), + send_date=dtm.datetime.utcnow(), + ) + }, ], ids=[ "reply", @@ -436,6 +501,15 @@ def message(bot): "checklist", "checklist_tasks_done", "checklist_tasks_added", + "is_paid_post", + "direct_messages_topic", + "reply_to_checklist_task_id", + "suggested_post_declined", + "suggested_post_paid", + "suggested_post_refunded", + "suggested_post_approved", + "suggested_post_approval_failed", + "suggested_post_info", ], ) def message_params(bot, request): @@ -1563,7 +1637,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_text, Bot.send_message, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1572,7 +1651,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_message", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_text, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1605,7 +1684,13 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_markdown, Bot.send_message, - ["chat_id", "parse_mode", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "parse_mode", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1614,7 +1699,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_message", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_text, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1654,7 +1739,13 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_markdown_v2, Bot.send_message, - ["chat_id", "parse_mode", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "parse_mode", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1663,7 +1754,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_message", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_text, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1706,7 +1797,13 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_html, Bot.send_message, - ["chat_id", "parse_mode", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "parse_mode", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1715,7 +1812,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_message", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_text, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1743,7 +1840,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_media_group, Bot.send_media_group, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1752,7 +1854,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_media_group", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_media_group, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1785,7 +1887,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_photo, Bot.send_photo, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1794,7 +1901,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_photo", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_photo, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1819,7 +1926,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_audio, Bot.send_audio, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1828,7 +1940,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_audio", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_audio, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1853,7 +1965,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_document, Bot.send_document, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1862,7 +1979,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_document", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_document, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1887,7 +2004,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_animation, Bot.send_animation, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1896,7 +2018,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_animation", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_animation, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1921,7 +2043,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_sticker, Bot.send_sticker, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1930,7 +2057,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_sticker", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_sticker, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1955,7 +2082,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_video, Bot.send_video, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1964,7 +2096,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_video", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_video, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -1989,7 +2121,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_video_note, Bot.send_video_note, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -1998,7 +2135,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_video_note", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_video_note, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2023,7 +2160,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_voice, Bot.send_voice, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -2032,7 +2174,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_voice", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_voice, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2057,7 +2199,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_location, Bot.send_location, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -2066,7 +2213,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_location", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_location, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2091,7 +2238,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_venue, Bot.send_venue, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -2100,7 +2252,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_venue", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_venue, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2125,7 +2277,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_contact, Bot.send_contact, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -2134,7 +2291,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_contact", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_contact, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2169,7 +2326,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_poll", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_poll, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2194,7 +2351,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_dice, Bot.send_dice, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -2203,7 +2365,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_dice", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_dice, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2345,7 +2507,12 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_invoice, Bot.send_invoice, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) @@ -2354,7 +2521,7 @@ async def make_assertion(*_, **kwargs): message.get_bot(), "send_invoice", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_invoice, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -2396,9 +2563,17 @@ async def make_assertion(*_, **kwargs): return chat_id and from_chat and message_id and notification and protected_cont assert check_shortcut_signature( - Message.forward, Bot.forward_message, ["from_chat_id", "message_id"], [] + Message.forward, + Bot.forward_message, + ["from_chat_id", "message_id", "direct_messages_topic_id"], + [], + ) + assert await check_shortcut_call( + message.forward, + message.get_bot(), + "forward_message", + shortcut_kwargs=["direct_messages_topic_id"], ) - assert await check_shortcut_call(message.forward, message.get_bot(), "forward_message") assert await check_defaults_handling(message.forward, message.get_bot()) monkeypatch.setattr(message.get_bot(), "forward_message", make_assertion) @@ -2431,9 +2606,17 @@ async def make_assertion(*_, **kwargs): ) assert check_shortcut_signature( - Message.copy, Bot.copy_message, ["from_chat_id", "message_id"], [] + Message.copy, + Bot.copy_message, + ["from_chat_id", "message_id", "direct_messages_topic_id"], + [], + ) + assert await check_shortcut_call( + message.copy, + message.get_bot(), + "copy_message", + shortcut_kwargs=["direct_messages_topic_id"], ) - assert await check_shortcut_call(message.copy, message.get_bot(), "copy_message") assert await check_defaults_handling(message.copy, message.get_bot()) monkeypatch.setattr(message.get_bot(), "copy_message", make_assertion) @@ -2474,11 +2657,21 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_copy, Bot.copy_message, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) - assert await check_shortcut_call(message.copy, message.get_bot(), "copy_message") + assert await check_shortcut_call( + message.copy, + message.get_bot(), + "copy_message", + shortcut_kwargs=["direct_messages_topic_id"], + ) assert await check_defaults_handling(message.copy, message.get_bot()) monkeypatch.setattr(message.get_bot(), "copy_message", make_assertion) @@ -2518,15 +2711,21 @@ async def make_assertion(*_, **kwargs): assert check_shortcut_signature( Message.reply_paid_media, Bot.send_paid_media, - ["chat_id", "reply_to_message_id", "business_connection_id"], + [ + "chat_id", + "reply_to_message_id", + "business_connection_id", + "direct_messages_topic_id", + ], ["do_quote", "reply_to_message_id"], + annotation_overrides={"message_thread_id": (ODVInput[int], DEFAULT_NONE)}, ) assert await check_shortcut_call( message.reply_paid_media, message.get_bot(), "send_paid_media", skip_params=["reply_to_message_id"], - shortcut_kwargs=["business_connection_id"], + shortcut_kwargs=["business_connection_id", "direct_messages_topic_id"], ) assert await check_defaults_handling( message.reply_paid_media, message.get_bot(), no_default_kwargs={"message_thread_id"} @@ -3078,3 +3277,53 @@ def test_attachement_successful_payment_deprecated(self, message, recwarn): ) assert recwarn[0].category is PTBDeprecationWarning assert recwarn[0].filename == __file__ + + async def test_approve_suggested_post(self, monkeypatch, message): + async def make_assertion(*_, **kwargs): + return ( + kwargs["chat_id"] == message.chat_id + and kwargs["message_id"] == message.message_id + and kwargs["send_date"] == 1234567890 + ) + + assert check_shortcut_signature( + Message.approve_suggested_post, + Bot.approve_suggested_post, + ["chat_id", "message_id"], + [], + ) + assert await check_shortcut_call( + message.approve_suggested_post, + message.get_bot(), + "approve_suggested_post", + shortcut_kwargs=["chat_id", "message_id"], + ) + assert await check_defaults_handling(message.approve_suggested_post, message.get_bot()) + + monkeypatch.setattr(message.get_bot(), "approve_suggested_post", make_assertion) + assert await message.approve_suggested_post(send_date=1234567890) + + async def test_decline_suggested_post(self, monkeypatch, message): + async def make_assertion(*_, **kwargs): + return ( + kwargs["chat_id"] == message.chat_id + and kwargs["message_id"] == message.message_id + and kwargs["comment"] == "some comment" + ) + + assert check_shortcut_signature( + Message.decline_suggested_post, + Bot.decline_suggested_post, + ["chat_id", "message_id"], + [], + ) + assert await check_shortcut_call( + message.decline_suggested_post, + message.get_bot(), + "decline_suggested_post", + shortcut_kwargs=["chat_id", "message_id"], + ) + assert await check_defaults_handling(message.decline_suggested_post, message.get_bot()) + + monkeypatch.setattr(message.get_bot(), "decline_suggested_post", make_assertion) + assert await message.decline_suggested_post(comment="some comment") diff --git a/tests/test_reply.py b/tests/test_reply.py index 0c144175640..4e1f3c3bf64 100644 --- a/tests/test_reply.py +++ b/tests/test_reply.py @@ -218,6 +218,7 @@ def reply_parameters(): quote_parse_mode=ReplyParametersTestBase.quote_parse_mode, quote_entities=ReplyParametersTestBase.quote_entities, quote_position=ReplyParametersTestBase.quote_position, + checklist_task_id=ReplyParametersTestBase.checklist_task_id, ) @@ -232,6 +233,7 @@ class ReplyParametersTestBase: MessageEntity(MessageEntity.EMAIL, 3, 4), ] quote_position = 5 + checklist_task_id = 9 class TestReplyParametersWithoutRequest(ReplyParametersTestBase): @@ -251,6 +253,7 @@ def test_de_json(self, offline_bot): "quote_parse_mode": self.quote_parse_mode, "quote_entities": [entity.to_dict() for entity in self.quote_entities], "quote_position": self.quote_position, + "checklist_task_id": self.checklist_task_id, } reply_parameters = ReplyParameters.de_json(json_dict, offline_bot) @@ -263,6 +266,7 @@ def test_de_json(self, offline_bot): assert reply_parameters.quote_parse_mode == self.quote_parse_mode assert reply_parameters.quote_entities == tuple(self.quote_entities) assert reply_parameters.quote_position == self.quote_position + assert reply_parameters.checklist_task_id == self.checklist_task_id def test_to_dict(self, reply_parameters): reply_parameters_dict = reply_parameters.to_dict() @@ -280,6 +284,7 @@ def test_to_dict(self, reply_parameters): entity.to_dict() for entity in self.quote_entities ] assert reply_parameters_dict["quote_position"] == self.quote_position + assert reply_parameters_dict["checklist_task_id"] == self.checklist_task_id def test_equality(self, reply_parameters): a = reply_parameters diff --git a/tests/test_suggestedpost.py b/tests/test_suggestedpost.py new file mode 100644 index 00000000000..dd6c08381aa --- /dev/null +++ b/tests/test_suggestedpost.py @@ -0,0 +1,600 @@ +#!/usr/bin/env python +# +# A library that provides a Python interface to the Telegram Bot API +# Copyright (C) 2015-2025 +# Leandro Toledo de Souza +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 Public License for more details. +# +# You should have received a copy of the GNU Lesser Public License +# along with this program. If not, see [http://www.gnu.org/licenses/]. + +import datetime as dtm + +import pytest + +from telegram import Dice +from telegram._chat import Chat +from telegram._message import Message +from telegram._payment.stars.staramount import StarAmount +from telegram._suggestedpost import ( + SuggestedPostApprovalFailed, + SuggestedPostApproved, + SuggestedPostDeclined, + SuggestedPostInfo, + SuggestedPostPaid, + SuggestedPostParameters, + SuggestedPostPrice, + SuggestedPostRefunded, +) +from telegram._utils.datetime import UTC, to_timestamp +from telegram.constants import SuggestedPostInfoState +from tests.auxil.slots import mro_slots + + +@pytest.fixture(scope="module") +def suggested_post_parameters(): + return SuggestedPostParameters( + price=SuggestedPostParametersTestBase.price, + send_date=SuggestedPostParametersTestBase.send_date, + ) + + +class SuggestedPostParametersTestBase: + price = SuggestedPostPrice(currency="XTR", amount=100) + send_date = dtm.datetime.now(tz=UTC).replace(microsecond=0) + + +class TestSuggestedPostParametersWithoutRequest(SuggestedPostParametersTestBase): + def test_slot_behaviour(self, suggested_post_parameters): + for attr in suggested_post_parameters.__slots__: + assert getattr(suggested_post_parameters, attr, "err") != "err", ( + f"got extra slot '{attr}'" + ) + assert len(mro_slots(suggested_post_parameters)) == len( + set(mro_slots(suggested_post_parameters)) + ), "duplicate slot" + + def test_de_json(self, offline_bot): + json_dict = { + "price": self.price.to_dict(), + "send_date": to_timestamp(self.send_date), + } + spp = SuggestedPostParameters.de_json(json_dict, offline_bot) + assert spp.price == self.price + assert spp.send_date == self.send_date + assert spp.api_kwargs == {} + + def test_de_json_localization(self, offline_bot, raw_bot, tz_bot): + json_dict = { + "price": self.price.to_dict(), + "send_date": to_timestamp(self.send_date), + } + + spp_bot = SuggestedPostParameters.de_json(json_dict, offline_bot) + spp_bot_raw = SuggestedPostParameters.de_json(json_dict, raw_bot) + spp_bot_tz = SuggestedPostParameters.de_json(json_dict, tz_bot) + + # comparing utcoffsets because comparing tzinfo objects is not reliable + send_date_offset = spp_bot_tz.send_date.utcoffset() + send_date_offset_tz = tz_bot.defaults.tzinfo.utcoffset( + spp_bot_tz.send_date.replace(tzinfo=None) + ) + + assert spp_bot.send_date.tzinfo == UTC + assert spp_bot_raw.send_date.tzinfo == UTC + assert send_date_offset_tz == send_date_offset + + def test_to_dict(self, suggested_post_parameters): + spp_dict = suggested_post_parameters.to_dict() + + assert isinstance(spp_dict, dict) + assert spp_dict["price"] == self.price.to_dict() + assert spp_dict["send_date"] == to_timestamp(self.send_date) + + def test_equality(self, suggested_post_parameters): + a = suggested_post_parameters + b = SuggestedPostParameters(price=self.price, send_date=self.send_date) + c = SuggestedPostParameters( + price=self.price, send_date=self.send_date + dtm.timedelta(seconds=1) + ) + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) + + +@pytest.fixture(scope="module") +def suggested_post_info(): + return SuggestedPostInfo( + state=SuggestedPostInfoTestBase.state, + price=SuggestedPostInfoTestBase.price, + send_date=SuggestedPostInfoTestBase.send_date, + ) + + +class SuggestedPostInfoTestBase: + state = SuggestedPostInfoState.PENDING + price = SuggestedPostPrice(currency="XTR", amount=100) + send_date = dtm.datetime.now(tz=UTC).replace(microsecond=0) + + +class TestSuggestedPostInfoWithoutRequest(SuggestedPostInfoTestBase): + def test_slot_behaviour(self, suggested_post_info): + for attr in suggested_post_info.__slots__: + assert getattr(suggested_post_info, attr, "err") != "err", f"got extra slot '{attr}'" + assert len(mro_slots(suggested_post_info)) == len(set(mro_slots(suggested_post_info))), ( + "duplicate slot" + ) + + def test_type_enum_conversion(self): + assert type(SuggestedPostInfo("pending").state) is SuggestedPostInfoState + assert SuggestedPostInfo("unknown").state == "unknown" + + def test_de_json(self, offline_bot): + json_dict = { + "state": self.state, + "price": self.price.to_dict(), + "send_date": to_timestamp(self.send_date), + } + spi = SuggestedPostInfo.de_json(json_dict, offline_bot) + assert spi.state == self.state + assert spi.price == self.price + assert spi.send_date == self.send_date + assert spi.api_kwargs == {} + + def test_de_json_localization(self, offline_bot, raw_bot, tz_bot): + json_dict = { + "state": self.state, + "price": self.price.to_dict(), + "send_date": to_timestamp(self.send_date), + } + + spi_bot = SuggestedPostInfo.de_json(json_dict, offline_bot) + spi_bot_raw = SuggestedPostInfo.de_json(json_dict, raw_bot) + spi_bot_tz = SuggestedPostInfo.de_json(json_dict, tz_bot) + + # comparing utcoffsets because comparing tzinfo objects is not reliable + send_date_offset = spi_bot_tz.send_date.utcoffset() + send_date_offset_tz = tz_bot.defaults.tzinfo.utcoffset( + spi_bot_tz.send_date.replace(tzinfo=None) + ) + + assert spi_bot.send_date.tzinfo == UTC + assert spi_bot_raw.send_date.tzinfo == UTC + assert send_date_offset_tz == send_date_offset + + def test_to_dict(self, suggested_post_info): + spi_dict = suggested_post_info.to_dict() + + assert isinstance(spi_dict, dict) + assert spi_dict["state"] == self.state + assert spi_dict["price"] == self.price.to_dict() + assert spi_dict["send_date"] == to_timestamp(self.send_date) + + def test_equality(self, suggested_post_info): + a = suggested_post_info + b = SuggestedPostInfo(state=self.state, price=self.price) + c = SuggestedPostInfo(state=SuggestedPostInfoState.DECLINED, price=self.price) + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) + + +@pytest.fixture(scope="module") +def suggested_post_price(): + return SuggestedPostPrice( + currency=SuggestedPostPriceTestBase.currency, + amount=SuggestedPostPriceTestBase.amount, + ) + + +class SuggestedPostPriceTestBase: + currency = "XTR" + amount = 100 + + +class TestSuggestedPostPriceWithoutRequest(SuggestedPostPriceTestBase): + def test_slot_behaviour(self, suggested_post_price): + for attr in suggested_post_price.__slots__: + assert getattr(suggested_post_price, attr, "err") != "err", f"got extra slot '{attr}'" + assert len(mro_slots(suggested_post_price)) == len(set(mro_slots(suggested_post_price))), ( + "duplicate slot" + ) + + def test_de_json(self, offline_bot): + json_dict = { + "currency": self.currency, + "amount": self.amount, + } + spp = SuggestedPostPrice.de_json(json_dict, offline_bot) + assert spp.currency == self.currency + assert spp.amount == self.amount + assert spp.api_kwargs == {} + + def test_to_dict(self, suggested_post_price): + spp_dict = suggested_post_price.to_dict() + + assert isinstance(spp_dict, dict) + assert spp_dict["currency"] == self.currency + assert spp_dict["amount"] == self.amount + + def test_equality(self, suggested_post_price): + a = suggested_post_price + b = SuggestedPostPrice(currency=self.currency, amount=self.amount) + c = SuggestedPostPrice(currency="TON", amount=self.amount) + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) + + +@pytest.fixture(scope="module") +def suggested_post_declined(): + return SuggestedPostDeclined( + suggested_post_message=SuggestedPostDeclinedTestBase.suggested_post_message, + comment=SuggestedPostDeclinedTestBase.comment, + ) + + +class SuggestedPostDeclinedTestBase: + suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.") + comment = "another time" + + +class TestSuggestedPostDeclinedWithoutRequest(SuggestedPostDeclinedTestBase): + def test_slot_behaviour(self, suggested_post_declined): + for attr in suggested_post_declined.__slots__: + assert getattr(suggested_post_declined, attr, "err") != "err", ( + f"got extra slot '{attr}'" + ) + assert len(mro_slots(suggested_post_declined)) == len( + set(mro_slots(suggested_post_declined)) + ), "duplicate slot" + + def test_de_json(self, offline_bot): + json_dict = { + "suggested_post_message": self.suggested_post_message.to_dict(), + "comment": self.comment, + } + spd = SuggestedPostDeclined.de_json(json_dict, offline_bot) + assert spd.suggested_post_message == self.suggested_post_message + assert spd.comment == self.comment + assert spd.api_kwargs == {} + + def test_to_dict(self, suggested_post_declined): + spd_dict = suggested_post_declined.to_dict() + + assert isinstance(spd_dict, dict) + assert spd_dict["suggested_post_message"] == self.suggested_post_message.to_dict() + assert spd_dict["comment"] == self.comment + + def test_equality(self, suggested_post_declined): + a = suggested_post_declined + b = SuggestedPostDeclined( + suggested_post_message=self.suggested_post_message, comment=self.comment + ) + c = SuggestedPostDeclined(suggested_post_message=self.suggested_post_message, comment="no") + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) + + +@pytest.fixture(scope="module") +def suggested_post_paid(): + return SuggestedPostPaid( + currency=SuggestedPostPaidTestBase.currency, + suggested_post_message=SuggestedPostPaidTestBase.suggested_post_message, + amount=SuggestedPostPaidTestBase.amount, + star_amount=SuggestedPostPaidTestBase.star_amount, + ) + + +class SuggestedPostPaidTestBase: + suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.") + currency = "XTR" + amount = 100 + star_amount = StarAmount(100) + + +class TestSuggestedPostPaidWithoutRequest(SuggestedPostPaidTestBase): + def test_slot_behaviour(self, suggested_post_paid): + for attr in suggested_post_paid.__slots__: + assert getattr(suggested_post_paid, attr, "err") != "err", f"got extra slot '{attr}'" + assert len(mro_slots(suggested_post_paid)) == len(set(mro_slots(suggested_post_paid))), ( + "duplicate slot" + ) + + def test_de_json(self, offline_bot): + json_dict = { + "suggested_post_message": self.suggested_post_message.to_dict(), + "currency": self.currency, + "amount": self.amount, + "star_amount": self.star_amount.to_dict(), + } + spp = SuggestedPostPaid.de_json(json_dict, offline_bot) + assert spp.suggested_post_message == self.suggested_post_message + assert spp.currency == self.currency + assert spp.amount == self.amount + assert spp.star_amount == self.star_amount + assert spp.api_kwargs == {} + + def test_to_dict(self, suggested_post_paid): + spp_dict = suggested_post_paid.to_dict() + + assert isinstance(spp_dict, dict) + assert spp_dict["suggested_post_message"] == self.suggested_post_message.to_dict() + assert spp_dict["currency"] == self.currency + assert spp_dict["amount"] == self.amount + assert spp_dict["star_amount"] == self.star_amount.to_dict() + + def test_equality(self, suggested_post_paid): + a = suggested_post_paid + b = SuggestedPostPaid( + suggested_post_message=self.suggested_post_message, + currency=self.currency, + amount=self.amount, + star_amount=self.star_amount, + ) + c = SuggestedPostPaid( + suggested_post_message=self.suggested_post_message, + currency=self.currency, + amount=self.amount - 1, + star_amount=StarAmount(self.amount - 1), + ) + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) + + +@pytest.fixture(scope="module") +def suggested_post_refunded(): + return SuggestedPostRefunded( + reason=SuggestedPostRefundedTestBase.reason, + suggested_post_message=SuggestedPostRefundedTestBase.suggested_post_message, + ) + + +class SuggestedPostRefundedTestBase: + reason = "post_deleted" + suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.") + + +class TestSuggestedPostRefundedWithoutRequest(SuggestedPostRefundedTestBase): + def test_slot_behaviour(self, suggested_post_refunded): + for attr in suggested_post_refunded.__slots__: + assert getattr(suggested_post_refunded, attr, "err") != "err", ( + f"got extra slot '{attr}'" + ) + assert len(mro_slots(suggested_post_refunded)) == len( + set(mro_slots(suggested_post_refunded)) + ), "duplicate slot" + + def test_de_json(self, offline_bot): + json_dict = { + "suggested_post_message": self.suggested_post_message.to_dict(), + "reason": self.reason, + } + spr = SuggestedPostRefunded.de_json(json_dict, offline_bot) + assert spr.suggested_post_message == self.suggested_post_message + assert spr.reason == self.reason + assert spr.api_kwargs == {} + + def test_to_dict(self, suggested_post_refunded): + spr_dict = suggested_post_refunded.to_dict() + + assert isinstance(spr_dict, dict) + assert spr_dict["suggested_post_message"] == self.suggested_post_message.to_dict() + assert spr_dict["reason"] == self.reason + + def test_equality(self, suggested_post_refunded): + a = suggested_post_refunded + b = SuggestedPostRefunded( + suggested_post_message=self.suggested_post_message, reason=self.reason + ) + c = SuggestedPostRefunded( + suggested_post_message=self.suggested_post_message, reason="payment_refunded" + ) + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) + + +@pytest.fixture(scope="module") +def suggested_post_approved(): + return SuggestedPostApproved( + send_date=SuggestedPostApprovedTestBase.send_date, + suggested_post_message=SuggestedPostApprovedTestBase.suggested_post_message, + price=SuggestedPostApprovedTestBase.price, + ) + + +class SuggestedPostApprovedTestBase: + send_date = dtm.datetime.now(tz=UTC).replace(microsecond=0) + suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.") + price = SuggestedPostPrice(currency="XTR", amount=100) + + +class TestSuggestedPostApprovedWithoutRequest(SuggestedPostApprovedTestBase): + def test_slot_behaviour(self, suggested_post_approved): + for attr in suggested_post_approved.__slots__: + assert getattr(suggested_post_approved, attr, "err") != "err", ( + f"got extra slot '{attr}'" + ) + assert len(mro_slots(suggested_post_approved)) == len( + set(mro_slots(suggested_post_approved)) + ), "duplicate slot" + + def test_de_json(self, offline_bot): + json_dict = { + "send_date": to_timestamp(self.send_date), + "suggested_post_message": self.suggested_post_message.to_dict(), + "price": self.price.to_dict(), + } + spa = SuggestedPostApproved.de_json(json_dict, offline_bot) + assert spa.send_date == self.send_date + assert spa.suggested_post_message == self.suggested_post_message + assert spa.price == self.price + assert spa.api_kwargs == {} + + def test_de_json_localization(self, offline_bot, raw_bot, tz_bot): + json_dict = { + "send_date": to_timestamp(self.send_date), + "suggested_post_message": self.suggested_post_message.to_dict(), + "price": self.price.to_dict(), + } + + spa_bot = SuggestedPostApproved.de_json(json_dict, offline_bot) + spa_bot_raw = SuggestedPostApproved.de_json(json_dict, raw_bot) + spi_bot_tz = SuggestedPostApproved.de_json(json_dict, tz_bot) + + # comparing utcoffsets because comparing tzinfo objects is not reliable + send_date_offset = spi_bot_tz.send_date.utcoffset() + send_date_offset_tz = tz_bot.defaults.tzinfo.utcoffset( + spi_bot_tz.send_date.replace(tzinfo=None) + ) + + assert spa_bot.send_date.tzinfo == UTC + assert spa_bot_raw.send_date.tzinfo == UTC + assert send_date_offset_tz == send_date_offset + + def test_to_dict(self, suggested_post_approved): + spa_dict = suggested_post_approved.to_dict() + + assert isinstance(spa_dict, dict) + assert spa_dict["send_date"] == to_timestamp(self.send_date) + assert spa_dict["suggested_post_message"] == self.suggested_post_message.to_dict() + assert spa_dict["price"] == self.price.to_dict() + + def test_equality(self, suggested_post_approved): + a = suggested_post_approved + b = SuggestedPostApproved( + send_date=self.send_date, + suggested_post_message=self.suggested_post_message, + price=self.price, + ) + c = SuggestedPostApproved( + send_date=self.send_date, + suggested_post_message=self.suggested_post_message, + price=SuggestedPostPrice(currency="XTR", amount=self.price.amount - 1), + ) + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) + + +@pytest.fixture(scope="module") +def suggested_post_approval_failed(): + return SuggestedPostApprovalFailed( + price=SuggestedPostApprovalFailedTestBase.price, + suggested_post_message=SuggestedPostApprovalFailedTestBase.suggested_post_message, + ) + + +class SuggestedPostApprovalFailedTestBase: + price = SuggestedPostPrice(currency="XTR", amount=100) + suggested_post_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="post this pls.") + + +class TestSuggestedPostApprovalFailedWithoutRequest(SuggestedPostApprovalFailedTestBase): + def test_slot_behaviour(self, suggested_post_approval_failed): + for attr in suggested_post_approval_failed.__slots__: + assert getattr(suggested_post_approval_failed, attr, "err") != "err", ( + f"got extra slot '{attr}'" + ) + assert len(mro_slots(suggested_post_approval_failed)) == len( + set(mro_slots(suggested_post_approval_failed)) + ), "duplicate slot" + + def test_de_json(self, offline_bot): + json_dict = { + "price": self.price.to_dict(), + "suggested_post_message": self.suggested_post_message.to_dict(), + } + spaf = SuggestedPostApprovalFailed.de_json(json_dict, offline_bot) + assert spaf.price == self.price + assert spaf.suggested_post_message == self.suggested_post_message + assert spaf.api_kwargs == {} + + def test_to_dict(self, suggested_post_approval_failed): + spaf_dict = suggested_post_approval_failed.to_dict() + + assert isinstance(spaf_dict, dict) + assert spaf_dict["price"] == self.price.to_dict() + assert spaf_dict["suggested_post_message"] == self.suggested_post_message.to_dict() + + def test_equality(self, suggested_post_approval_failed): + a = suggested_post_approval_failed + b = SuggestedPostApprovalFailed( + price=self.price, + suggested_post_message=self.suggested_post_message, + ) + c = SuggestedPostApprovalFailed( + price=SuggestedPostPrice(currency="XTR", amount=self.price.amount - 1), + suggested_post_message=self.suggested_post_message, + ) + e = Dice(4, "emoji") + + assert a == b + assert hash(a) == hash(b) + + assert a != c + assert hash(a) != hash(c) + + assert a != e + assert hash(a) != hash(e) diff --git a/tests/test_uniquegift.py b/tests/test_uniquegift.py index bb317a0e9f7..3a21ad3fca9 100644 --- a/tests/test_uniquegift.py +++ b/tests/test_uniquegift.py @@ -23,6 +23,7 @@ from telegram import ( BotCommand, + Chat, Sticker, UniqueGift, UniqueGiftBackdrop, @@ -45,6 +46,7 @@ def unique_gift(): model=UniqueGiftTestBase.model, symbol=UniqueGiftTestBase.symbol, backdrop=UniqueGiftTestBase.backdrop, + publisher_chat=UniqueGiftTestBase.publisher_chat, ) @@ -67,6 +69,7 @@ class UniqueGiftTestBase: colors=UniqueGiftBackdropColors(0x00FF00, 0xEE00FF, 0xAA22BB, 0x20FE8F), rarity_per_mille=30, ) + publisher_chat = Chat(1, Chat.PRIVATE) class TestUniqueGiftWithoutRequest(UniqueGiftTestBase): @@ -83,6 +86,7 @@ def test_de_json(self, offline_bot): "model": self.model.to_dict(), "symbol": self.symbol.to_dict(), "backdrop": self.backdrop.to_dict(), + "publisher_chat": self.publisher_chat.to_dict(), } unique_gift = UniqueGift.de_json(json_dict, offline_bot) assert unique_gift.api_kwargs == {} @@ -93,6 +97,7 @@ def test_de_json(self, offline_bot): assert unique_gift.model == self.model assert unique_gift.symbol == self.symbol assert unique_gift.backdrop == self.backdrop + assert unique_gift.publisher_chat == self.publisher_chat def test_to_dict(self, unique_gift): gift_dict = unique_gift.to_dict() @@ -104,6 +109,7 @@ def test_to_dict(self, unique_gift): assert gift_dict["model"] == self.model.to_dict() assert gift_dict["symbol"] == self.symbol.to_dict() assert gift_dict["backdrop"] == self.backdrop.to_dict() + assert gift_dict["publisher_chat"] == self.publisher_chat.to_dict() def test_equality(self, unique_gift): a = unique_gift @@ -114,6 +120,7 @@ def test_equality(self, unique_gift): self.model, self.symbol, self.backdrop, + self.publisher_chat, ) c = UniqueGift( "other_base_name", @@ -122,6 +129,7 @@ def test_equality(self, unique_gift): self.model, self.symbol, self.backdrop, + self.publisher_chat, ) d = BotCommand("start", "description")