def test_json_markup_with_before_after(): for args, labels in label_test_cases.items(): paginator = InlineKeyboardPaginator(*args) paginator.add_before( *extra_buttons ) paginator.add_after( *extra_buttons ) keyboard = json.loads(paginator.markup)['inline_keyboard'] _verify_extra_buttons(keyboard[0]) if not labels: _verify_extra_buttons(keyboard[1]) continue assert len(keyboard[1]) == len(labels), \ 'In case init args {} button count not correct. Mast be {}'.format(args, len(labels)) # for button, label in zip(keyboard[1], labels): # assert button['text'] == label, \ # 'In case init args {} button label not correct. Must be {}'.format(args, label) _verify_extra_buttons(keyboard[2])
def test_one_page_keyboard_json_markup_with_before(): for test_case in one_page_test_cases: paginator = InlineKeyboardPaginator(*test_case) paginator.add_before( *extra_buttons ) keyboard = json.loads(paginator.markup)['inline_keyboard'] _verify_extra_buttons(keyboard[0])
def test_json_markup_with_before(): for args, labels in label_test_cases.items(): paginator = InlineKeyboardPaginator(*args) paginator.add_before( *extra_buttons ) keyboard = json.loads(paginator.markup)['inline_keyboard'] _verify_extra_buttons(keyboard[0]) if not labels: continue assert len(keyboard[1]) == len(labels), \ 'In case init args {} button count not correct. Mast be {}'.format(args, len(labels))
def send_character_page(message, page=1): paginator = InlineKeyboardPaginator(len(character_pages), current_page=page, data_pattern='character#{page}') paginator.add_before( InlineKeyboardButton('Like', callback_data='like#{}'.format(page)), InlineKeyboardButton('Dislike', callback_data='dislike#{}'.format(page))) paginator.add_after(InlineKeyboardButton('Go back', callback_data='back')) bot.send_message(message.chat.id, character_pages[page - 1], reply_markup=paginator.markup, parse_mode='Markdown')
def characters_page_callback(update, context): query = update.callback_query query.answer() page = int(query.data.split('#')[1]) paginator = InlineKeyboardPaginator(len(character_pages), current_page=page, data_pattern='character#{page}') paginator.add_before( InlineKeyboardButton('Like', callback_data='like#{}'.format(page)), InlineKeyboardButton('Dislike', callback_data='dislike#{}'.format(page))) paginator.add_after(InlineKeyboardButton('Go back', callback_data='back')) query.edit_message_text(text=character_pages[page - 1], reply_markup=paginator.markup, parse_mode='Markdown')
def vacancy_paginator(vacancies: list, pattern: str, page: int, text: str, update, context): vacancies = vacancies[::-1] if len(vacancies) == 0: inline_keyboard = [[ InlineKeyboardButton('Вернуться ', callback_data='start') ]] inline_buttons = InlineKeyboardMarkup(inline_keyboard=inline_keyboard) update.callback_query.edit_message_text( text='Не найдено. Попробуйте изменить фильтр', reply_markup=inline_buttons) else: page_num = page_definer(vacancies) paginator = InlineKeyboardPaginator(page_num, current_page=page, data_pattern=f'{pattern}' + '#{page}') # Defining a range of vacancies that should be applicable to current page # And adding each vacancy as a new button vacancies_range = vacancies[page * PAGE_SPLIT - PAGE_SPLIT:page * PAGE_SPLIT] for i in vacancies_range: paginator.add_before( InlineKeyboardButton( text=f'{i["title"]} | {i["salary"]} | {i["joining_date"]}', callback_data=f'detail_{pattern}' + f'#{page}' + f'_{i["id"]}')) # If user pressed current page twicely, than will add space to button # To change markup and avoid BadRequest error from telegram if context.user_data[CURRENT_PAGE] == page: paginator.add_after( InlineKeyboardButton('Вернуться ', callback_data='start')) else: paginator.add_after( InlineKeyboardButton('Вернуться', callback_data='start')) update.callback_query.edit_message_text(text=text, reply_markup=paginator.markup, parse_mode='Markdown') # Updating current page to real current page context.user_data[CURRENT_PAGE] = page return ConversationHandler.END
def send_character_page(message, page=1): # print(pers) chat_id = message.chat.id global clients if not clients[chat_id]: bot.send_message(chat_id, 'База пуста, отправьте файл. Информация /start') return paginator = InlineKeyboardPaginator(len(clients[chat_id]) // 6 + 1, current_page=page, data_pattern='character#{page}') for i in clients[chat_id][6 * (page - 1):6 * page]: paginator.add_before( InlineKeyboardButton(str(i[0]) + ':' + i[1], callback_data='pers:{}'.format(i[0]))) print(page) bot.send_message(message.chat.id, msg(clients[chat_id][page - 1]), reply_markup=paginator.markup, parse_mode='Markdown')
def reply_text_or_edit_with_keyboard_paginator( message: Message, query: Optional[CallbackQuery], text: str, page_count: int, items_per_page: int, current_page: int, data_pattern: str, before_inline_buttons: List[InlineKeyboardButton] = None, after_inline_buttons: List[InlineKeyboardButton] = None, quote: bool = False, **kwargs, ): page_count = math.ceil(page_count / items_per_page) paginator = InlineKeyboardPaginator( page_count=page_count, current_page=current_page, data_pattern=data_pattern, ) if before_inline_buttons: paginator.add_before(*before_inline_buttons) if after_inline_buttons: paginator.add_after(*after_inline_buttons) reply_markup = paginator.markup reply_text_or_edit_with_keyboard( message, query, text, reply_markup, quote=quote, **kwargs, )