mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 18:19:34 +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
|
|
@ -37,7 +37,7 @@ from pathlib import Path
|
||||||
from aiogram import Bot, Dispatcher, executor, types
|
from aiogram import Bot, Dispatcher, executor, types
|
||||||
from aiogram.contrib.middlewares.i18n import I18nMiddleware
|
from aiogram.contrib.middlewares.i18n import I18nMiddleware
|
||||||
|
|
||||||
TOKEN = 'BOT TOKEN HERE'
|
TOKEN = 'BOT_TOKEN_HERE'
|
||||||
I18N_DOMAIN = 'mybot'
|
I18N_DOMAIN = 'mybot'
|
||||||
|
|
||||||
BASE_DIR = Path(__file__).parent
|
BASE_DIR = Path(__file__).parent
|
||||||
|
|
|
||||||
|
|
@ -6,50 +6,56 @@ import logging
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher, executor, types
|
from aiogram import Bot, Dispatcher, executor, types
|
||||||
|
|
||||||
|
|
||||||
API_TOKEN = 'BOT_TOKEN_HERE'
|
API_TOKEN = 'BOT_TOKEN_HERE'
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
logger.setLevel(logging.DEBUG)
|
|
||||||
|
|
||||||
# Initialize bot and dispatcher
|
# Initialize bot and dispatcher
|
||||||
bot = Bot(token=API_TOKEN)
|
bot = Bot(token=API_TOKEN)
|
||||||
dp = Dispatcher(bot)
|
dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
@dp.message_handler(commands=['start'])
|
@dp.message_handler(commands='start')
|
||||||
async def start_cmd_handler(message: types.Message):
|
async def start_cmd_handler(message: types.Message):
|
||||||
keyboard_markup = types.InlineKeyboardMarkup(row_width=3)
|
keyboard_markup = types.InlineKeyboardMarkup(row_width=3)
|
||||||
# default row_width is 3, so here we can omit it actually
|
# default row_width is 3, so here we can omit it actually
|
||||||
# kept for clearness
|
# kept for clearness
|
||||||
|
|
||||||
keyboard_markup.row(types.InlineKeyboardButton("Yes!", callback_data='yes'),
|
text_and_data = (
|
||||||
# in real life for the callback_data the callback data factory should be used
|
('Yes!', 'yes'),
|
||||||
# here the raw string is used for the simplicity
|
('No!', 'no'),
|
||||||
types.InlineKeyboardButton("No!", callback_data='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",
|
keyboard_markup.row(*row_btns)
|
||||||
url='https://github.com/aiogram/aiogram'))
|
keyboard_markup.add(
|
||||||
# url buttons has no callback data
|
# 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)
|
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'
|
# Use multiple registrators. Handler will execute when one of the filters is OK
|
||||||
# @dp.callback_query_handler(text='yes') # if cb.data == 'yes'
|
@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):
|
async def inline_kb_answer_callback_handler(query: types.CallbackQuery):
|
||||||
await query.answer() # send answer to close the rounding circle
|
|
||||||
|
|
||||||
answer_data = query.data
|
answer_data = query.data
|
||||||
logger.debug(f"answer_data={answer_data}")
|
# always answer callback queries, even if you have nothing to say
|
||||||
# here we can work with query.data
|
await query.answer(f'You answered with {answer_data!r}')
|
||||||
|
|
||||||
if answer_data == 'yes':
|
if answer_data == 'yes':
|
||||||
await bot.send_message(query.from_user.id, "That's great!")
|
text = 'Great, me too!'
|
||||||
elif answer_data == 'no':
|
elif answer_data == 'no':
|
||||||
await bot.send_message(query.from_user.id, "Oh no...Why so?")
|
text = 'Oh no...Why so?'
|
||||||
else:
|
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__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue