Last active
April 11, 2020 18:05
-
-
Save MasterGroosha/17775abb5b1e417d39a4dd58f2ed6c43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Здесь будут куски кода к 14-му уроку моего учебника |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dp.message_handler(commands="set_commands", state="*") | |
async def cmd_set_commands(message: types.Message): | |
if message.from_user.id == 1234567: # Подставьте сюда свой Telegram ID | |
commands = [types.BotCommand(command="/drinks", description="Заказать напитки"), | |
types.BotCommand(command="/food", description="Заказать блюда")] | |
await bot.set_my_commands(commands) | |
await message.answer("Команды настроены.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dp.message_handler(commands="food", state="*") | |
async def food_step_1(message: types.Message): | |
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True) | |
for name in available_food_names: | |
keyboard.add(name) | |
await message.answer("Выберите блюдо:", reply_markup=keyboard) | |
await OrderFood.waiting_for_food_name.set() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dp.message_handler(state=OrderFood.waiting_for_food_name, content_types=types.ContentTypes.TEXT) | |
async def food_step_2(message: types.Message, state: FSMContext): # обратите внимание, есть второй аргумент | |
if message.text.lower() not in available_food_names: | |
await message.reply("Пожалуйста, выберите блюдо, используя клавиатуру ниже.") | |
return | |
await state.update_data(chosen_food=message.text.lower()) | |
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True) | |
for size in available_food_sizes: | |
keyboard.add(size) | |
await OrderFood.next() # для простых шагов можно не указывать название состояния, обходясь next() | |
await message.answer("Теперь выберите размер порции:", reply_markup=keyboard) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dp.message_handler(state=OrderFood.waiting_for_food_size, content_types=types.ContentTypes.TEXT) | |
async def food_step_3(message: types.Message, state: FSMContext): | |
if message.text.lower() not in available_food_sizes: | |
await message.reply("Пожалуйста, выберите размер порции, используя клавиатуру ниже.") | |
return | |
user_data = await state.get_data() | |
await message.answer(f"Вы заказали {message.text.lower()} порцию {user_data['chosen_food']}.\n" | |
f"Попробуйте теперь заказать напитки: /drinks", reply_markup=types.ReplyKeyboardRemove()) | |
await state.finish() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment