import io from aiogram import types from aiogram.types import ContentType from aiogram.dispatcher import FSMContext from load import bot, dp, messages from utils.helper import download_file from keyboard.default.admin.main_menu import base_menu, continue_btn, all_right from keyboard.default.main_menu import back_to_main_menu from state.post import Post from utils.database.market import add_item @dp.message_handler(lambda x: x.text == messages.add_post, is_admin=True) async def cmd_add_post(message: types.Message): await bot.send_message(message.chat.id, "Укажите имя товара", reply_markup=base_menu()) await Post.name.set() @dp.message_handler(is_admin=True, state=Post.name) async def admin_form_name(message: types.Message, state: FSMContext): await state.update_data(name=message.text) await bot.send_message(message.chat.id, "Укажите описание товара", reply_markup=base_menu(skip=True)) await Post.description.set() @dp.message_handler(is_admin=True, state=Post.description) async def admin_form_desk(message: types.Message, state: FSMContext): if message.text == messages.skip_message: await state.update_data(description="") else: await state.update_data(description=message.text) await bot.send_message(message.chat.id, "Укажите цену", reply_markup=base_menu()) await Post.price.set() @dp.message_handler(is_admin=True, state=Post.price) async def admin_form_price(message: types.message, state: FSMContext): if not message.text.replace('.','').replace(',', '').isdigit(): await Post.price.set() await state.update_data(price=float(message.text.replace(",", "."))) await bot.send_message(message.chat.id, "Отправьте фото товара") await Post.image.set() @dp.message_handler(is_admin=True, state=Post.image, content_types=[ContentType.PHOTO]) async def admin_form_image(message:types.Message, state: FSMContext): file_id = message.photo[-1].file_id file_info = await bot.get_file(file_id) photo = await download_file(file_info.file_path) await state.update_data(image=photo.read()) await bot.send_message( message.chat.id, "Все данные для поста были заполнены", reply_markup=continue_btn ) await Post.finish.set() @dp.message_handler(lambda x:x.text == messages.continue_, state=Post.finish, is_admin=True) async def finish(message: types.Message, state: FSMContext): data = await state.get_data() await bot.send_photo( message.chat.id, caption=messages.product_message.format( name=data["name"], description=data["description"], price=data["price"] ), photo=data["image"], reply_markup=all_right, parse_mode='Markdown' ) @dp.message_handler(lambda x: x.text == messages.all_right_message, is_admin=True, state=Post.finish) async def write_on_db(message: types.Message, state: FSMContext): data = await state.get_data() add_item(**data) await state.finish() await bot.send_message( message.chat.id, "Товар был добавлен", reply_markup=back_to_main_menu )