Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from aiogram import Router, types, filters
- from contextlib import suppress
- from aiogram.exceptions import TelegramBadRequest
- echo_router = Router()
- def get_keyboard():
- buttons = [
- [
- # callback_data будет передана в callback.data при нажатии
- types.InlineKeyboardButton(text="-1", callback_data="num_decr"),
- types.InlineKeyboardButton(text="+1", callback_data="num_incr")
- ],
- ]
- keyboard = types.InlineKeyboardMarkup(inline_keyboard=buttons)
- return keyboard
- # Сработает при нажатии на любую Inline кнопку
- @echo_router.callback_query(filters.Text(startswith='num_'))
- async def update_num_text(callback: types.CallbackQuery):
- new_value = int(callback.message.text)
- # Удаляет num_ с начала
- command = callback.data.removeprefix('num_')
- if command == 'incr':
- new_value += 1
- elif command == 'decr':
- new_value -= 1
- # Если сообщение не изменилось, то выдаст TelegramBadRequest
- # С помощью suppress мы подавляем ошибку
- with suppress(TelegramBadRequest):
- await callback.message.edit_text(
- f"{new_value}",
- reply_markup=get_keyboard()
- )
- # Сработает при получении /numbers
- @echo_router.message(filters.Command(commands=['numbers']))
- async def echo(message: types.Message):
- await message.answer('0', reply_markup=get_keyboard())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement