From 1dd095cc36a5dbc7e46a4b030caaa4b69d67f6fa Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Fri, 10 Jan 2025 13:02:34 +0100 Subject: [PATCH] Fix subscription_period in create_invoice_link Closes #135 --- compiler/errors/source/400_BAD_REQUEST.tsv | 1 + .../methods/business/create_invoice_link.py | 46 ++++++++++--------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/compiler/errors/source/400_BAD_REQUEST.tsv b/compiler/errors/source/400_BAD_REQUEST.tsv index ae31f67d1..fcbca2b72 100644 --- a/compiler/errors/source/400_BAD_REQUEST.tsv +++ b/compiler/errors/source/400_BAD_REQUEST.tsv @@ -464,6 +464,7 @@ STORY_NOT_MODIFIED The new story information you passed is equal to the previous STORY_PERIOD_INVALID The specified story period is invalid for this account. STORY_SEND_FLOOD_MONTHLY_X You've hit the monthly story limit as specified by the [`stories_sent_monthly_limit_*` client configuration parameters](https://core.telegram.org/api/config#stories-sent-monthly-limit-default): wait for the specified number of seconds before posting a new story. STORY_SEND_FLOOD_WEEKLY_X You've hit the weekly story limit as specified by the [`stories_sent_weekly_limit_*` client configuration parameters](https://core.telegram.org/api/config#stories-sent-weekly-limit-default): wait for the specified number of seconds before posting a new story. +SUBSCRIPTION_PERIOD_INVALID The subscription period is invalid. SWITCH_PM_TEXT_EMPTY The switch_pm.text field was empty. SWITCH_WEBVIEW_URL_INVALID The URL specified in switch_webview.url is invalid! TAKEOUT_INVALID The takeout id is invalid diff --git a/pyrogram/methods/business/create_invoice_link.py b/pyrogram/methods/business/create_invoice_link.py index 3aa7e5134..2de6da860 100644 --- a/pyrogram/methods/business/create_invoice_link.py +++ b/pyrogram/methods/business/create_invoice_link.py @@ -17,11 +17,11 @@ # along with Pyrogram. If not, see . import logging -from datetime import datetime +from datetime import timedelta from typing import Optional, Union import pyrogram -from pyrogram import enums, raw, utils, types +from pyrogram import raw, types log = logging.getLogger(__name__) @@ -34,23 +34,23 @@ async def create_invoice_link( payload: Union[str, bytes], currency: str, prices: list["types.LabeledPrice"], - provider_token: str = None, - subscription_period: datetime = None, - max_tip_amount: int = None, - suggested_tip_amounts: list[int] = None, - start_parameter: str = None, - provider_data: str = None, - photo_url: str = None, - photo_size: int = None, - photo_width: int = None, - photo_height: int = None, - need_name: bool = None, - need_phone_number: bool = None, - need_email: bool = None, - need_shipping_address: bool = None, - send_phone_number_to_provider: bool = None, - send_email_to_provider: bool = None, - is_flexible: bool = None + provider_token: Optional[str] = None, + subscription_period: Optional[Union[int, timedelta]] = None, + max_tip_amount: Optional[int] = None, + suggested_tip_amounts: Optional[list[int]] = None, + start_parameter: Optional[str] = None, + provider_data: Optional[str] = None, + photo_url: Optional[str] = None, + photo_size: Optional[int] = None, + photo_width: Optional[int] = None, + photo_height: Optional[int] = None, + need_name: Optional[bool] = None, + need_phone_number: Optional[bool] = None, + need_email: Optional[bool] = None, + need_shipping_address: Optional[bool] = None, + send_phone_number_to_provider: Optional[bool] = None, + send_email_to_provider: Optional[bool] = None, + is_flexible: Optional[bool] = None ) -> str: """Use this method to create a link for an invoice. @@ -75,7 +75,7 @@ async def create_invoice_link( provider_token (``str``, *optional*): Payment provider token, obtained via `@BotFather `_. Pass an empty string for payments in `Telegram Stars `_. - subscription_period (:py:obj:`~datetime.datetime`, *optional*): + subscription_period (``int`` | :py:obj:`~datetime.timedelta`, *optional*): The number of seconds the subscription will be active for before the next payment. The currency must be set to "XTR" (Telegram Stars) if the parameter is used. Currently, it must always be 2592000 (30 days) if specified. Any number of subscriptions can be active for a given bot at the same time, including multiple concurrent subscriptions from the same user. Subscription price must no exceed ``stars_paid_post_amount_max`` Telegram Stars. max_tip_amount (``int``, *optional*): @@ -154,7 +154,11 @@ async def create_invoice_link( flexible=is_flexible, phone_to_provider=send_phone_number_to_provider, email_to_provider=send_email_to_provider, - subscription_period=utils.datetime_to_timestamp(subscription_period) + subscription_period=int( + subscription_period.total_seconds() + if isinstance(subscription_period, timedelta) + else subscription_period + ) if subscription_period else None ), payload=payload.encode() if isinstance(payload, str) else payload, provider=provider_token,