mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Refactor examples/inline_keyboard_example.py
This commit is contained in:
parent
c6871f8071
commit
7a155794e2
2 changed files with 26 additions and 20 deletions
|
|
@ -6,50 +6,56 @@ import logging
|
|||
|
||||
from aiogram import Bot, Dispatcher, executor, types
|
||||
|
||||
|
||||
API_TOKEN = 'BOT_TOKEN_HERE'
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
# Initialize bot and dispatcher
|
||||
bot = Bot(token=API_TOKEN)
|
||||
dp = Dispatcher(bot)
|
||||
|
||||
|
||||
@dp.message_handler(commands=['start'])
|
||||
@dp.message_handler(commands='start')
|
||||
async def start_cmd_handler(message: types.Message):
|
||||
keyboard_markup = types.InlineKeyboardMarkup(row_width=3)
|
||||
# default row_width is 3, so here we can omit it actually
|
||||
# kept for clearness
|
||||
|
||||
keyboard_markup.row(types.InlineKeyboardButton("Yes!", callback_data='yes'),
|
||||
# in real life for the callback_data the callback data factory should be used
|
||||
# here the raw string is used for the simplicity
|
||||
types.InlineKeyboardButton("No!", callback_data='no'))
|
||||
text_and_data = (
|
||||
('Yes!', 'yes'),
|
||||
('No!', 'no'),
|
||||
)
|
||||
# in real life for the callback_data the callback data factory should be used
|
||||
# here the raw string is used for the simplicity
|
||||
row_btns = (types.InlineKeyboardButton(text, callback_data=data) for text, data in text_and_data)
|
||||
|
||||
keyboard_markup.add(types.InlineKeyboardButton("aiogram link",
|
||||
url='https://github.com/aiogram/aiogram'))
|
||||
# url buttons has no callback data
|
||||
keyboard_markup.row(*row_btns)
|
||||
keyboard_markup.add(
|
||||
# url buttons have no callback data
|
||||
types.InlineKeyboardButton('aiogram source', url='https://github.com/aiogram/aiogram'),
|
||||
)
|
||||
|
||||
await message.reply("Hi!\nDo you love aiogram?", reply_markup=keyboard_markup)
|
||||
|
||||
|
||||
@dp.callback_query_handler(lambda cb: cb.data in ['yes', 'no']) # if cb.data is either 'yes' or 'no'
|
||||
# @dp.callback_query_handler(text='yes') # if cb.data == 'yes'
|
||||
# Use multiple registrators. Handler will execute when one of the filters is OK
|
||||
@dp.callback_query_handler(text='no') # if cb.data == 'no'
|
||||
@dp.callback_query_handler(text='yes') # if cb.data == 'yes'
|
||||
async def inline_kb_answer_callback_handler(query: types.CallbackQuery):
|
||||
await query.answer() # send answer to close the rounding circle
|
||||
|
||||
answer_data = query.data
|
||||
logger.debug(f"answer_data={answer_data}")
|
||||
# here we can work with query.data
|
||||
# always answer callback queries, even if you have nothing to say
|
||||
await query.answer(f'You answered with {answer_data!r}')
|
||||
|
||||
if answer_data == 'yes':
|
||||
await bot.send_message(query.from_user.id, "That's great!")
|
||||
text = 'Great, me too!'
|
||||
elif answer_data == 'no':
|
||||
await bot.send_message(query.from_user.id, "Oh no...Why so?")
|
||||
text = 'Oh no...Why so?'
|
||||
else:
|
||||
await bot.send_message(query.from_user.id, "Invalid callback data!")
|
||||
text = f'Unexpected callback data {answer_data!r}!'
|
||||
|
||||
await bot.send_message(query.from_user.id, text)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue