Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bot API 8.0: Add Bot.edit_user_star_subscription #4571

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions docs/source/inclusions/bot_methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,35 @@
.. raw:: html

<details>
<summary>Miscellaneous</summary>
<summary>Payments and Stars</summary>

.. list-table::
:align: left
:widths: 1 4

* - :meth:`~telegram.Bot.create_invoice_link`
- Used to generate an HTTP link for an invoice
* - :meth:`~telegram.Bot.edit_user_star_subscription`
- Used for editing a user's star subscription
* - :meth:`~telegram.Bot.get_star_transactions`
- Used for obtaining the bot's Telegram Stars transactions
* - :meth:`~telegram.Bot.refund_star_payment`
- Used for refunding a payment in Telegram Stars

.. raw:: html

</details>
<br>

.. raw:: html

<details>
<summary>Miscellaneous</summary>

.. list-table::
:align: left
:widths: 1 4

* - :meth:`~telegram.Bot.close`
- Used for closing server instance when switching to another local server
* - :meth:`~telegram.Bot.log_out`
Expand All @@ -371,10 +392,6 @@
- Used for getting basic info about a file
* - :meth:`~telegram.Bot.get_me`
- Used for getting basic information about the bot
* - :meth:`~telegram.Bot.get_star_transactions`
- Used for obtaining the bot's Telegram Stars transactions
* - :meth:`~telegram.Bot.refund_star_payment`
- Used for refunding a payment in Telegram Stars

.. raw:: html

Expand Down
49 changes: 49 additions & 0 deletions telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9254,6 +9254,53 @@ async def get_star_transactions(
bot=self,
)

async def edit_user_star_subscription(
self,
user_id: int,
telegram_payment_charge_id: str,
is_canceled: bool,
*,
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:
"""Allows the bot to cancel or re-enable extension of a subscription paid in Telegram
Stars.

.. versionadded:: NEXT.VERSION

Args:
user_id (:obj:`int`): Identifier of the user whose subscription will be edited.
telegram_payment_charge_id (:obj:`str`): Telegram payment identifier for the
subscription.
is_canceled (:obj:`bool`): Pass :obj:`True` to cancel extension of the user
subscription; the subscription must be active up to the end of the current
subscription period. Pass :obj:`False` to allow the user to re-enable a
subscription that was previously canceled by the bot.

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

Raises:
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {
"user_id": user_id,
"telegram_payment_charge_id": telegram_payment_charge_id,
"is_canceled": is_canceled,
}
return await self._post(
"editUserStartSubscription",
data,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

async def send_paid_media(
self,
chat_id: Union[str, int],
Expand Down Expand Up @@ -9729,6 +9776,8 @@ def to_dict(self, recursive: bool = True) -> JSONDict: # noqa: ARG002
"""Alias for :meth:`refund_star_payment`"""
getStarTransactions = get_star_transactions
"""Alias for :meth:`get_star_transactions`"""
editUserStarSubscription = edit_user_star_subscription
"""Alias for :meth:`edit_user_star_subscription`"""
sendPaidMedia = send_paid_media
"""Alias for :meth:`send_paid_media`"""
createChatSubscriptionInviteLink = create_chat_subscription_invite_link
Expand Down
25 changes: 25 additions & 0 deletions telegram/ext/_extbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4255,6 +4255,30 @@ async def get_star_transactions(
api_kwargs=self._merge_api_rl_kwargs(api_kwargs, rate_limit_args),
)

async def edit_user_star_subscription(
self,
user_id: int,
telegram_payment_charge_id: str,
is_canceled: bool,
*,
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().edit_user_star_subscription(
user_id=user_id,
telegram_payment_charge_id=telegram_payment_charge_id,
is_canceled=is_canceled,
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 send_paid_media(
self,
chat_id: Union[str, int],
Expand Down Expand Up @@ -4478,6 +4502,7 @@ async def edit_chat_subscription_invite_link(
replaceStickerInSet = replace_sticker_in_set
refundStarPayment = refund_star_payment
getStarTransactions = get_star_transactions
editUserStarSubscription = edit_user_star_subscription
createChatSubscriptionInviteLink = create_chat_subscription_invite_link
editChatSubscriptionInviteLink = edit_chat_subscription_invite_link
sendPaidMedia = send_paid_media
16 changes: 16 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2321,6 +2321,22 @@ async def do_request(url, request_data: RequestData, *args, **kwargs):
obj = await offline_bot.get_star_transactions(offset=3)
assert isinstance(obj, StarTransactions)

async def test_edit_user_star_subscription(self, offline_bot, monkeypatch):
"""Can't properly test, so we only check that the correct values are passed"""

async def make_assertion(url, request_data: RequestData, *args, **kwargs):
return (
request_data.parameters.get("user_id") == 42
and request_data.parameters.get("telegram_payment_charge_id")
== "telegram_payment_charge_id"
and request_data.parameters.get("is_canceled") is False
)

monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.edit_user_star_subscription(
42, "telegram_payment_charge_id", False
)

async def test_create_chat_subscription_invite_link(
self,
monkeypatch,
Expand Down
Loading