pyrofork: Refactor ShippingQuery

Signed-off-by: wulan17 <wulan17@komodos.id>
This commit is contained in:
wulan17 2025-03-26 22:29:03 +07:00
parent c427601210
commit 8c47410bba
No known key found for this signature in database
GPG key ID: 737814D4B5FF0420
4 changed files with 35 additions and 31 deletions

View file

@ -17,14 +17,12 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>. # along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.
from typing import Union, List, Match, Optional
import pyrogram import pyrogram
from pyrogram import raw, enums from pyrogram import raw, types
from pyrogram import types from typing import Dict
from ..object import Object from ..object import Object
from ..update import Update from ..update import Update
from ... import utils
class PreCheckoutQuery(Object, Update): class PreCheckoutQuery(Object, Update):
@ -76,7 +74,11 @@ class PreCheckoutQuery(Object, Update):
self.payment_info = payment_info self.payment_info = payment_info
@staticmethod @staticmethod
async def _parse(client: "pyrogram.Client", pre_checkout_query, users) -> "PreCheckoutQuery": async def _parse(
client: "pyrogram.Client",
pre_checkout_query: "raw.types.UpdateBotPrecheckoutQuery",
users: Dict[int, "raw.types.User"] = None
) -> "PreCheckoutQuery":
# Try to decode pre-checkout query payload into string. If that fails, fallback to bytes instead of decoding by # 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. # ignoring/replacing errors, this way, button clicks will still work.
try: try:
@ -95,14 +97,7 @@ class PreCheckoutQuery(Object, Update):
name=pre_checkout_query.info.name, name=pre_checkout_query.info.name,
phone_number=pre_checkout_query.info.phone, phone_number=pre_checkout_query.info.phone,
email=pre_checkout_query.info.email, email=pre_checkout_query.info.email,
shipping_address=types.ShippingAddress( shipping_address=types.ShippingAddress._parse(pre_checkout_query.info.shipping_address)
street_line1=pre_checkout_query.info.shipping_address.street_line1,
street_line2=pre_checkout_query.info.shipping_address.street_line2,
city=pre_checkout_query.info.shipping_address.city,
state=pre_checkout_query.info.shipping_address.state,
post_code=pre_checkout_query.info.shipping_address.post_code,
country_code=pre_checkout_query.info.shipping_address.country_iso2
)
) if pre_checkout_query.info else None, ) if pre_checkout_query.info else None,
client=client client=client
) )

View file

@ -17,6 +17,8 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrofork. If not, see <http://www.gnu.org/licenses/>. # along with Pyrofork. If not, see <http://www.gnu.org/licenses/>.
from pyrogram import raw
from ..object import Object from ..object import Object
@ -60,3 +62,17 @@ class ShippingAddress(Object):
self.state = state self.state = state
self.post_code = post_code self.post_code = post_code
self.country_code = country_code self.country_code = country_code
@staticmethod
def _parse(shipping_address: "raw.types.raw.types.PostAddress") -> "ShippingAddress":
if shipping_address is None:
return None
return ShippingAddress(
street_line1=shipping_address.street_line1,
street_line2=shipping_address.street_line2,
city=shipping_address.city,
state=shipping_address.state,
post_code=shipping_address.post_code,
country_code=shipping_address.country_code
)

View file

@ -1,5 +1,6 @@
# Pyrogram - Telegram MTProto API Client Library for Python # Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance> # Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
# #
# This file is part of Pyrogram. # This file is part of Pyrogram.
# #

View file

@ -1,5 +1,6 @@
# Pyrogram - Telegram MTProto API Client Library for Python # Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance> # Copyright (C) 2017-present Dan <https://github.com/delivrance>
# Copyright (C) 2022-present Mayuri-Chan <https://github.com/Mayuri-Chan>
# #
# This file is part of Pyrogram. # This file is part of Pyrogram.
# #
@ -16,8 +17,6 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union, Optional
import pyrogram import pyrogram
from pyrogram import raw, types from pyrogram import raw, types
@ -49,20 +48,20 @@ class ShippingQuery(Object, Update):
client: "pyrogram.Client" = None, client: "pyrogram.Client" = None,
id: str, id: str,
from_user: "types.User", from_user: "types.User",
invoice_payload: str, payload: str,
shipping_address: "types.ShippingAddress" = None shipping_address: "types.ShippingAddress" = None
): ):
super().__init__(client) super().__init__(client)
self.id = id self.id = id
self.from_user = from_user self.from_user = from_user
self.invoice_payload = invoice_payload self.payload = payload
self.shipping_address = shipping_address self.shipping_address = shipping_address
@staticmethod @staticmethod
async def _parse( async def _parse(
client: "pyrogram.Client", client: "pyrogram.Client",
shipping_query: "raw.types.updateBotShippingQuery", shipping_query: "raw.types.UpdateBotShippingQuery",
users: dict users: dict
) -> "types.PreCheckoutQuery": ) -> "types.PreCheckoutQuery":
# Try to decode pre-checkout query payload into string. If that fails, fallback to bytes instead of decoding by # Try to decode pre-checkout query payload into string. If that fails, fallback to bytes instead of decoding by
@ -72,19 +71,12 @@ class ShippingQuery(Object, Update):
except (UnicodeDecodeError, AttributeError): except (UnicodeDecodeError, AttributeError):
payload = shipping_query.payload payload = shipping_query.payload
return types.PreCheckoutQuery( return ShippingQuery(
id=str(shipping_query.query_id), client=client,
from_user=types.User._parse(client, users[shipping_query.user_id]), id=shipping_query.query_id,
invoice_payload=payload, from_user=types.User._parse(client, users.get(shipping_query.user_id)),
shipping_address=types.ShippingAddress( payload=payload,
country_code=shipping_query.shipping_address.country_iso2, shipping_address=types.ShippingAddress._parse(shipping_query.shipping_address)
state=shipping_query.shipping_address.state,
city=shipping_query.shipping_address.city,
street_line1=shipping_query.shipping_address.street_line1,
street_line2=shipping_query.shipping_address.street_line2,
post_code=shipping_query.shipping_address.post_code
),
client=client
) )
async def answer( async def answer(