ShopBot/handlers/user/ordering.py

109 lines
3.9 KiB
Python
Raw Permalink Normal View History

2022-06-04 22:02:51 +03:00
from aiogram import types
from aiogram.dispatcher import FSMContext
from load import dp, bot, messages
from keyboard.default.checkout import confirm_all_info
from keyboard.default.main_menu import cancel_btn, get_phone_number, continue_btn
from keyboard.default.ordering import load_info
from state.checkout import Checkout
from utils.database.ordering import get_info
2022-06-16 00:32:17 +03:00
@dp.message_handler(lambda x: x.text == messages.all_right_message, state="load_or_create_profile")
2022-06-04 22:02:51 +03:00
async def confirm(message: types.Message, state: FSMContext):
await state.finish()
info = get_info(message.from_user.id)
await bot.send_message(
2022-06-05 15:02:41 +03:00
message.chat.id,
2022-06-04 22:02:51 +03:00
"Может у вас есть сохраненные данные?",
reply_markup=load_info(bool(info))
)
2022-06-05 15:02:41 +03:00
await state.set_state(state="load_data")
2022-06-04 22:02:51 +03:00
2022-06-05 15:02:41 +03:00
@dp.message_handler(lambda x: x.text == messages.load_data, state="load_data")
2022-06-04 22:02:51 +03:00
async def load_form(message: types.message, state: FSMContext):
info = get_info(message.from_user.id)
await state.update_data(
2022-06-05 15:02:41 +03:00
first_name=info["first_name"],
last_name=info["last_name"],
phone_number=info["phone_number"],
2022-06-16 00:32:17 +03:00
address=info["address"],
load=True
2022-06-04 22:02:51 +03:00
)
await bot.send_message(message.chat.id, "Ваши данные загружены!", reply_markup=continue_btn)
2022-06-05 15:02:41 +03:00
await state.set_state("finish_send_form")
2022-06-04 22:02:51 +03:00
2022-06-05 15:02:41 +03:00
@dp.message_handler(lambda x: x.text == messages.rewrite_data, state="load_data")
2022-06-16 00:32:17 +03:00
async def new_form(message: types.Message):
2022-06-04 22:02:51 +03:00
await bot.send_message(
message.chat.id,
"Напишите вашу фамилию",
reply_markup=cancel_btn
)
await Checkout.last_name.set()
@dp.message_handler(state=Checkout.last_name)
async def form_last_name(message: types.Message, state: FSMContext):
await state.update_data(last_name=message.text)
await bot.send_message(
message.chat.id,
"Ваше имя",
reply_markup=cancel_btn
)
await Checkout.first_name.set()
@dp.message_handler(state=Checkout.first_name)
async def form_first_name(message: types.Message, state: FSMContext):
await state.update_data(first_name=message.text)
await bot.send_message(
message.chat.id,
("Номер телефона\n\n"
2022-06-05 15:02:41 +03:00
"Можете написать номер телефона вручную"),
2022-06-04 22:02:51 +03:00
reply_markup=get_phone_number
)
await Checkout.phone_number.set()
@dp.message_handler(state=Checkout.phone_number, content_types=[types.ContentType.CONTACT, types.ContentType.TEXT])
async def form_phone(message: types.Message, state: FSMContext):
2022-06-05 15:02:41 +03:00
if message.contact is not None:
phone = message.contact.phone_number
else:
phone = message.text
if len(phone) not in [10, 12, 13]:
2022-06-04 22:02:51 +03:00
await bot.send_message(message.chat.id, "Номер телефона не правильный!")
await Checkout.phone_number.set()
2022-06-05 15:02:41 +03:00
return
await state.update_data(phone_number=phone)
await bot.send_message(message.chat.id, "Отправте адрес доставки", reply_markup=cancel_btn)
await Checkout.address.set()
2022-06-04 22:02:51 +03:00
@dp.message_handler(state=Checkout.address)
async def form_address(message: types.Message, state: FSMContext):
await state.update_data(address=message.text)
2022-06-05 15:02:41 +03:00
await bot.send_message(message.chat.id, "Отлично, заявка заполнена!", reply_markup=continue_btn)
await state.set_state("finish_send_form")
2022-06-04 22:02:51 +03:00
2022-06-05 15:02:41 +03:00
@dp.message_handler(state="finish_send_form")
2022-06-04 22:02:51 +03:00
async def finish_form(message: types.Message, state: FSMContext):
data = await state.get_data()
text = (f"Имя: {data['first_name']}\n"
2022-06-05 15:02:41 +03:00
f"Фамилия: {data['last_name']}\n"
f"Номер телефона: {data['phone_number']}\n"
2022-06-16 00:32:17 +03:00
f"Адрес: {data['address']}\n")
2022-06-04 22:02:51 +03:00
await bot.send_message(
2022-06-05 15:02:41 +03:00
message.chat.id,
2022-06-04 22:02:51 +03:00
text,
2022-06-16 00:32:17 +03:00
reply_markup=confirm_all_info(load=data.pop("load", False))
2022-06-04 22:02:51 +03:00
)
2022-06-05 15:02:41 +03:00
await state.set_state("save_or_continue")