From 7d931abe376195f7fd8b8e859d213dacbd942125 Mon Sep 17 00:00:00 2001 From: coder2020official Date: Tue, 21 Jun 2022 15:22:00 +0500 Subject: [PATCH] Added secret token and create_invoice_link for asynctelebot --- telebot/async_telebot.py | 66 +++++++++++++++++++++++++++++++++++++-- telebot/asyncio_helper.py | 45 +++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index e2df525..1566657 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -1384,7 +1384,7 @@ class AsyncTeleBot: self.current_states = StatePickleStorage(file_path=filename) async def set_webhook(self, url=None, certificate=None, max_connections=None, allowed_updates=None, ip_address=None, - drop_pending_updates = None, timeout=None): + drop_pending_updates = None, timeout=None, secret_token=None): """ Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, @@ -1409,10 +1409,11 @@ class AsyncTeleBot: resolved through DNS :param drop_pending_updates: Pass True to drop all pending updates :param timeout: Integer. Request connection timeout + :param secret_token: Secret token to be used to verify the webhook :return: """ return await asyncio_helper.set_webhook(self.token, url, certificate, max_connections, allowed_updates, ip_address, - drop_pending_updates, timeout) + drop_pending_updates, timeout, secret_token) @@ -3066,6 +3067,67 @@ class AsyncTeleBot: max_tip_amount, suggested_tip_amounts, protect_content) return types.Message.de_json(result) + + async def create_invoice_link(self, + title: str, description: str, payload:str, provider_token: str, + currency: str, prices: List[types.LabeledPrice], + max_tip_amount: Optional[int] = None, + suggested_tip_amounts: Optional[List[int]]=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. + Returns the created invoice link as String on success. + + Telegram documentation: + https://core.telegram.org/bots/api#createinvoicelink + + :param title: Product name, 1-32 characters + :param description: Product description, 1-255 characters + :param payload: Bot-defined invoice payload, 1-128 bytes. This will not be displayed to the user, + use for your internal processes. + :param provider_token: Payments provider token, obtained via @Botfather + :param currency: Three-letter ISO 4217 currency code, + see https://core.telegram.org/bots/payments#supported-currencies + :param prices: Price breakdown, a list of components + (e.g. product price, tax, discount, delivery cost, delivery tax, bonus, etc.) + :param max_tip_amount: The maximum accepted amount for tips in the smallest units of the currency + :param suggested_tip_amounts: A JSON-serialized array of suggested amounts of tips in the smallest + :param provider_data: A JSON-serialized data about the invoice, which will be shared with the payment provider. + A detailed description of required fields should be provided by the payment provider. + :param photo_url: URL of the product photo for the invoice. Can be a photo of the goods + :param photo_size: Photo size in bytes + :param photo_width: Photo width + :param photo_height: Photo height + :param need_name: Pass True, if you require the user's full name to complete the order + :param need_phone_number: Pass True, if you require the user's phone number to complete the order + :param need_email: Pass True, if you require the user's email to complete the order + :param need_shipping_address: Pass True, if you require the user's shipping address to complete the order + :param send_phone_number_to_provider: Pass True, if user's phone number should be sent to provider + :param send_email_to_provider: Pass True, if user's email address should be sent to provider + :param is_flexible: Pass True, if the final price depends on the shipping method + + :return: Created invoice link as String on success. + """ + result = await asyncio_helper.create_invoice_link( + self.token, title, description, payload, provider_token, + currency, prices, max_tip_amount, suggested_tip_amounts, provider_data, + photo_url, photo_size, photo_width, photo_height, need_name, need_phone_number, + need_email, need_shipping_address, send_phone_number_to_provider, + send_email_to_provider, is_flexible) + return result + # noinspection PyShadowingBuiltins async def send_poll( self, chat_id: Union[int, str], question: str, options: List[str], diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index a7bcccf..51b60e9 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -171,7 +171,7 @@ async def download_file(token, file_path): async def set_webhook(token, url=None, certificate=None, max_connections=None, allowed_updates=None, ip_address=None, - drop_pending_updates = None, timeout=None): + drop_pending_updates = None, timeout=None, secret_token=None): method_url = r'setWebhook' payload = { 'url': url if url else "", @@ -189,6 +189,8 @@ async def set_webhook(token, url=None, certificate=None, max_connections=None, a payload['drop_pending_updates'] = drop_pending_updates if timeout: payload['timeout'] = timeout + if secret_token: + payload['secret_token'] = secret_token return await _process_request(token, method_url, params=payload, files=files) @@ -1599,6 +1601,47 @@ async def delete_sticker_from_set(token, sticker): return await _process_request(token, method_url, params=payload, method='post') + +async def create_invoice_link(token, title, description, payload, provider_token, + currency, prices, max_tip_amount=None, suggested_tip_amounts=None, provider_data=None, + photo_url=None, photo_size=None, photo_width=None, photo_height=None, need_name=None, need_phone_number=None, + need_email=None, need_shipping_address=None, send_phone_number_to_provider=None, + send_email_to_provider=None, is_flexible=None): + method_url = r'createInvoiceLink' + payload = {'title': title, 'description': description, 'payload': payload, 'provider_token': provider_token, + 'currency': currency, 'prices': await _convert_list_json_serializable(prices)} + if max_tip_amount: + payload['max_tip_amount'] = max_tip_amount + if suggested_tip_amounts: + payload['suggested_tip_amounts'] = json.dumps(suggested_tip_amounts) + if provider_data: + payload['provider_data'] = provider_data + if photo_url: + payload['photo_url'] = photo_url + if photo_size: + payload['photo_size'] = photo_size + if photo_width: + payload['photo_width'] = photo_width + if photo_height: + payload['photo_height'] = photo_height + if need_name: + payload['need_name'] = need_name + if need_phone_number: + payload['need_phone_number'] = need_phone_number + if need_email: + payload['need_email'] = need_email + if need_shipping_address: + payload['need_shipping_address'] = need_shipping_address + if send_phone_number_to_provider: + payload['send_phone_number_to_provider'] = send_phone_number_to_provider + if send_email_to_provider: + payload['send_email_to_provider'] = send_email_to_provider + if is_flexible: + payload['is_flexible'] = is_flexible + return await _process_request(token, method_url, params=payload, method='post') + + + # noinspection PyShadowingBuiltins async def send_poll( token, chat_id,