Skip to content
Prev Previous commit
Next Next commit
answer_pre_checkout_query
  • Loading branch information
SpEcHiDe committed Jun 6, 2024
commit ba5f94eae93b435a95eca59aca12a79b0077da26
2 changes: 2 additions & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def get_title_list(s: str) -> list:
""",
business="""
Telegram Business & Fragment
answer_pre_checkout_query
get_business_connection
get_collectible_item_info
refund_star_payment
Expand Down Expand Up @@ -568,6 +569,7 @@ def get_title_list(s: str) -> list:
Invoice
LabeledPrice
OrderInfo
PreCheckoutQuery
ShippingAddress
SuccessfulPayment
""",
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from .answer_pre_checkout_query import AnswerPreCheckoutQuery
from .get_business_connection import GetBusinessConnection
from .get_collectible_item_info import GetCollectibleItemInfo
from .refund_star_payment import RefundStarPayment
from .send_invoice import SendInvoice


class TelegramBusiness(
AnswerPreCheckoutQuery,
GetBusinessConnection,
GetCollectibleItemInfo,
RefundStarPayment,
Expand Down
67 changes: 67 additions & 0 deletions pyrogram/methods/business/answer_pre_checkout_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram 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 General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

import pyrogram
from pyrogram import raw


class AnswerPreCheckoutQuery:
async def answer_pre_checkout_query(
self: "pyrogram.Client",
pre_checkout_query_id: str,
ok: bool,
error_message: str = None
):
"""Once the user has confirmed their payment and shipping details, the API sends the final confirmation in the form of an :obj:`~pyrogram.handlers.PreCheckoutQueryHandler`.

Use this method to respond to such pre-checkout queries.

**Note**: The API must receive an answer within 10 seconds after the pre-checkout query was sent.

.. include:: /_includes/usable-by/bots.rst

Parameters:
pre_checkout_query_id (``str``):
Unique identifier for the query to be answered.

ok (``bool``):
Specify True if everything is alright (goods are available, etc.) and the bot is ready to proceed with the order. Use False if there are any problems.

error_message (``str``, *optional*):
Required if ok is False. Error message in human readable form that explains the reason for failure to proceed with the checkout (e.g. "Sorry, somebody just bought the last of our amazing black T-shirts while you were busy filling out your payment details. Please choose a different color or garment!"). Telegram will display this message to the user.

Returns:
``bool``: True, on success.

Example:
.. code-block:: python

# Proceed with the order
await app.answer_pre_checkout_query(query_id, ok=True)

# Answer with error message
await app.answer_pre_checkout_query(query_id, ok=False, error_message="Error Message displayed to the user")

"""
return await self.invoke(
raw.functions.messages.SetBotPrecheckoutResults(
query_id=int(pre_checkout_query_id),
success=ok or None,
error=error_message or None
)
)
2 changes: 2 additions & 0 deletions pyrogram/types/business/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .invoice import Invoice
from .labeled_price import LabeledPrice
from .order_info import OrderInfo
from .pre_checkout_query import PreCheckoutQuery
from .shipping_address import ShippingAddress
from .successful_payment import SuccessfulPayment

Expand All @@ -38,6 +39,7 @@
"Invoice",
"LabeledPrice",
"OrderInfo",
"PreCheckoutQuery",
"ShippingAddress",
"SuccessfulPayment",
]
149 changes: 149 additions & 0 deletions pyrogram/types/business/pre_checkout_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram 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 General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

from typing import Union, List, Match, Optional

import pyrogram
from pyrogram import raw, enums, types, utils

from ..object import Object
from ..update import Update


class PreCheckoutQuery(Object, Update):
"""This object contains information about an incoming pre-checkout query.

Parameters:
id (``str``):
Unique query identifier.

from_user (:obj:`~pyrogram.types.User`):
User who sent the query.

currency (``str``):
Three-letter ISO 4217 `currency <https://core.telegram.org/bots/payments#supported-currencies>`_ code, or ``XTR`` for payments in `Telegram Stars <https://t.me/BotNews/90>`_.

total_amount (``int``):
Total price in the smallest units of the currency (integer, **not** float/double). For example, for a price of ``US$ 1.45`` pass ``amount = 145``. See the __exp__ parameter in `currencies.json <https://core.telegram.org/bots/payments/currencies.json>`_, it shows the number of digits past the decimal point for each currency (2 for the majority of currencies).

invoice_payload (``str``):
Bot specified invoice payload. Only available to the bot that received the payment.

shipping_option_id (``str``, *optional*):
Identifier of the shipping option chosen by the user. Only available to the bot that received the payment.

order_info (:obj:`~pyrogram.types.OrderInfo`, *optional*):
Order information provided by the user. Only available to the bot that received the payment.

"""

def __init__(
self,
*,
client: "pyrogram.Client" = None,
id: str,
from_user: "types.User",
currency: str,
total_amount: int,
invoice_payload: str,
shipping_option_id: str = None,
order_info: "types.OrderInfo" = None
):
super().__init__(client)

self.id = id
self.from_user = from_user
self.currency = currency
self.total_amount = total_amount
self.invoice_payload = invoice_payload
self.shipping_option_id = shipping_option_id
self.order_info = order_info

@staticmethod
async def _parse(
client: "pyrogram.Client",
pre_checkout_query: "raw.types.UpdateBotPrecheckoutQuery",
users: dict
) -> "PreCheckoutQuery":
# Try to decode pre-checkout query payload into string. If that fails, fallback to bytes instead of decoding by
# ignoring/replacing errors, this way, button clicks will still work.
try:
payload = pre_checkout_query.payload.decode()
except (UnicodeDecodeError, AttributeError):
payload = pre_checkout_query.payload

return PreCheckoutQuery(
id=str(pre_checkout_query.query_id),
from_user=types.User._parse(client, users[pre_checkout_query.user_id]),
currency=pre_checkout_query.currency,
total_amount=pre_checkout_query.total_amount,
invoice_payload=payload,
shipping_option_id=pre_checkout_query.shipping_option_id,
order_info=types.OrderInfo(
name=pre_checkout_query.info.name,
phone_number=pre_checkout_query.info.phone,
email=pre_checkout_query.info.email,
shipping_address=types.ShippingAddress(
country_code=pre_checkout_query.info.shipping_address.country_iso2,
state=pre_checkout_query.info.shipping_address.state,
city=pre_checkout_query.info.shipping_address.city,
street_line1=pre_checkout_query.info.shipping_address.street_line1,
street_line2=pre_checkout_query.info.shipping_address.street_line2,
post_code=pre_checkout_query.info.shipping_address.post_code
)
) if pre_checkout_query.info else None,
client=client
)

async def answer(
self,
ok: bool,
error_message: str = None
):
"""Bound method *answer* of :obj:`~pyrogram.types.PreCheckoutQuery`.

Use this method as a shortcut for:

.. code-block:: python

await client.answer_pre_checkout_query(
pre_checkout_query.id,
success=True
)

Example:
.. code-block:: python

await pre_checkout_query.answer(ok=True)

Parameters:
ok (``bool``):
Specify True if everything is alright (goods are available, etc.) and the bot is ready to proceed with the order. Use False if there are any problems.

error_message (``str``, *optional*):
Required if ok is False. Error message in human readable form that explains the reason for failure to proceed with the checkout (e.g. "Sorry, somebody just bought the last of our amazing black T-shirts while you were busy filling out your payment details. Please choose a different color or garment!"). Telegram will display this message to the user.

Returns:
``bool``: True, on success.

"""
return await self._client.answer_pre_checkout_query(
pre_checkout_query_id=self.id,
ok=ok,
error_message=error_message
)