ShopBot/handlers/user/ordering.py
2022-06-16 00:32:17 +03:00

109 lines
3.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
@dp.message_handler(lambda x: x.text == messages.all_right_message, state="load_or_create_profile")
async def confirm(message: types.Message, state: FSMContext):
await state.finish()
info = get_info(message.from_user.id)
await bot.send_message(
message.chat.id,
"Может у вас есть сохраненные данные?",
reply_markup=load_info(bool(info))
)
await state.set_state(state="load_data")
@dp.message_handler(lambda x: x.text == messages.load_data, state="load_data")
async def load_form(message: types.message, state: FSMContext):
info = get_info(message.from_user.id)
await state.update_data(
first_name=info["first_name"],
last_name=info["last_name"],
phone_number=info["phone_number"],
address=info["address"],
load=True
)
await bot.send_message(message.chat.id, "Ваши данные загружены!", reply_markup=continue_btn)
await state.set_state("finish_send_form")
@dp.message_handler(lambda x: x.text == messages.rewrite_data, state="load_data")
async def new_form(message: types.Message):
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"
"Можете написать номер телефона вручную"),
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):
if message.contact is not None:
phone = message.contact.phone_number
else:
phone = message.text
if len(phone) not in [10, 12, 13]:
await bot.send_message(message.chat.id, "Номер телефона не правильный!")
await Checkout.phone_number.set()
return
await state.update_data(phone_number=phone)
await bot.send_message(message.chat.id, "Отправте адрес доставки", reply_markup=cancel_btn)
await Checkout.address.set()
@dp.message_handler(state=Checkout.address)
async def form_address(message: types.Message, state: FSMContext):
await state.update_data(address=message.text)
await bot.send_message(message.chat.id, "Отлично, заявка заполнена!", reply_markup=continue_btn)
await state.set_state("finish_send_form")
@dp.message_handler(state="finish_send_form")
async def finish_form(message: types.Message, state: FSMContext):
data = await state.get_data()
text = (f"Имя: {data['first_name']}\n"
f"Фамилия: {data['last_name']}\n"
f"Номер телефона: {data['phone_number']}\n"
f"Адрес: {data['address']}\n")
await bot.send_message(
message.chat.id,
text,
reply_markup=confirm_all_info(load=data.pop("load", False))
)
await state.set_state("save_or_continue")