diff --git a/examples/i18n_example.py b/examples/i18n_example.py index f69482af..3bb624bd 100644 --- a/examples/i18n_example.py +++ b/examples/i18n_example.py @@ -37,7 +37,7 @@ from pathlib import Path from aiogram import Bot, Dispatcher, executor, types from aiogram.contrib.middlewares.i18n import I18nMiddleware -TOKEN = 'BOT TOKEN HERE' +TOKEN = 'BOT_TOKEN_HERE' I18N_DOMAIN = 'mybot' BASE_DIR = Path(__file__).parent diff --git a/examples/inline_keyboard_example.py b/examples/inline_keyboard_example.py index 2478b9e0..8b950f98 100644 --- a/examples/inline_keyboard_example.py +++ b/examples/inline_keyboard_example.py @@ -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__':