Если вы продаете физические продукты, то для вас актуален вопрос о доставке в разные регионы и страны, а также расчет окончательной цены за продукт. В этом видео мы научим вас, как с помощью Aiogram добавить функцию доставки для физических продуктов в ваш бот.
Исходный код к уроку
handlers/pay.py
from aiogram import Bot
from aiogram.types import Message, LabeledPrice, PreCheckoutQuery, InlineKeyboardButton, InlineKeyboardMarkup, \
ShippingOption, ShippingQuery
keyboards = InlineKeyboardMarkup(inline_keyboard=[
[
InlineKeyboardButton(
text='Оплатить заказ',
pay=True
)
],
[
InlineKeyboardButton(
text='Link',
url='https://nztcoder.com'
)
]
])
BY_SHIPPING = ShippingOption(
id='by',
title='Доставка в Беларусь',
prices=[
LabeledPrice(
label='Доставка Белпочтой',
amount=500
)
]
)
RU_SHIPPING = ShippingOption(
id='ru',
title='Доставка в Россию',
prices=[
LabeledPrice(
label='Доставка почтой России',
amount=1000
)
]
)
UA_SHIPPING = ShippingOption(
id='ua',
title='Доставка в Украину',
prices=[
LabeledPrice(
label='Доставка Укрпочтой',
amount=1500
)
]
)
CITIES_SHIPPING = ShippingOption(
id='capitals',
title='Быстрая доставка по городу',
prices=[
LabeledPrice(
label='Доставка курьером Торетто',
amount=2000
)
]
)
async def shipping_check(shipping_query: ShippingQuery, bot: Bot):
shipping_options = []
countries = ['BY', 'RU', 'UA']
if shipping_query.shipping_address.country_code not in countries:
return await bot.answer_shipping_query(shipping_query.id, ok=False,
error_message='Наш гринго в Вашу страну не доставляет')
if shipping_query.shipping_address.country_code == 'BY':
shipping_options.append(BY_SHIPPING)
if shipping_query.shipping_address.country_code == 'RU':
shipping_options.append(RU_SHIPPING)
if shipping_query.shipping_address.country_code == 'UA':
shipping_options.append(UA_SHIPPING)
cities = ['Минск', 'Москва', 'Киев']
if shipping_query.shipping_address.city in cities:
shipping_options.append(CITIES_SHIPPING)
await bot.answer_shipping_query(shipping_query.id, ok=True, shipping_options=shipping_options)
async def order(message: Message, bot: Bot):
await bot.send_invoice(
chat_id=message.chat.id,
title='Покупка через Telegram бот',
description='Учимся принимать платежи через Telegram бот',
payload='Payment through a bot',
provider_token='381764678:TEST:33621',
currency='rub',
prices=[
LabeledPrice(
label='Доступ к секретной информации',
amount=99000
),
LabeledPrice(
label='НДС',
amount=20000
),
LabeledPrice(
label='Скидка',
amount=-20000
),
LabeledPrice(
label='Бонус',
amount=-40000
)
],
max_tip_amount=5000,
suggested_tip_amounts=[1000, 2000, 3000, 4000],
start_parameter='nztcoder',
provider_data=None,
photo_url='https://i.ibb.co/zGw5X0B/image.jpg',
photo_size=100,
photo_width=800,
photo_height=450,
need_name=False,
need_phone_number=False,
need_email=False,
need_shipping_address=False,
send_phone_number_to_provider=False,
send_email_to_provider=False,
is_flexible=True,
disable_notification=False,
protect_content=False,
reply_to_message_id=None,
allow_sending_without_reply=True,
reply_markup=keyboards,
request_timeout=15
)
async def pre_checkout_query(pre_checkout_query: PreCheckoutQuery, bot: Bot):
await bot.answer_pre_checkout_query(pre_checkout_query.id, ok=True)
async def successful_payment(message: Message):
msg = f'Спасибо за оплату {message.successful_payment.total_amount // 100} {message.successful_payment.currency}.' \
f'\r\nНаш менеджер получил заявку и уже набирает Ваш номер телефона.' \
f'\r\nПока можете скачать цифровую версию нашего продукта https://nztcoder.com'
await message.answer(msg)main.py
from aiogram import Bot, Dispatcher
from aiogram.types import Message, ContentType
from core.handlers.basic import get_start, get_photo, get_hello
from core.filters.iscontact import IsTrueContact
from core.handlers.contact import get_fake_contact, get_true_contact
import asyncio
import logging
from core.settings import settings
from aiogram.filters import ContentTypesFilter, Command, CommandStart
from aiogram import F
from core.utils.commands import set_commands
from core.handlers.basic import get_location
from core.handlers.basic import get_inline
from core.handlers.callback import select_macbook
from core.utils.callbackdata import MacInfo
from core.handlers.pay import order, pre_checkout_query, successful_payment, shipping_check
async def start_bot(bot: Bot):
await set_commands(bot)
await bot.send_message(settings.bots.admin_id, text='Бот запущен!')
async def stop_bot(bot: Bot):
await bot.send_message(settings.bots.admin_id, text='Бот остановлен!')
async def start():
logging.basicConfig(level=logging.INFO,
format="%(asctime)s - [%(levelname)s] - %(name)s - "
"(%(filename)s).%(funcName)s(%(lineno)d) - %(message)s"
)
bot = Bot(token=settings.bots.bot_token, parse_mode='HTML')
dp = Dispatcher()
dp.startup.register(start_bot)
dp.shutdown.register(stop_bot)
dp.message.register(order, Command(commands='pay'))
dp.pre_checkout_query.register(pre_checkout_query)
dp.message.register(successful_payment, ContentTypesFilter(content_types=[ContentType.SUCCESSFUL_PAYMENT]))
dp.shipping_query.register(shipping_check)
dp.message.register(get_inline, Command(commands='inline'))
dp.callback_query.register(select_macbook, MacInfo.filter(F.model == 'pro'))
# dp.message.register(get_photo, ContentTypesFilter(content_types=[ContentType.PHOTO]))
dp.message.register(get_location, ContentTypesFilter(content_types=[ContentType.LOCATION]))
dp.message.register(get_hello, F.text == 'Привет')
dp.message.register(get_true_contact, ContentTypesFilter(content_types=[ContentType.CONTACT]), IsTrueContact())
dp.message.register(get_fake_contact, ContentTypesFilter(content_types=[ContentType.CONTACT]))
dp.message.register(get_photo, F.photo)
dp.message.register(get_start, Command(commands=['start', 'run']))
# dp.message.register(get_start, CommandStart)
try:
await dp.start_polling(bot)
finally:
await bot.session.close()
if __name__ == "__main__":
asyncio.run(start())














