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_after(): for test_case in one_page_test_cases: paginator = InlineKeyboardPaginator(*test_case) paginator.add_after( *extra_buttons ) keyboard = json.loads(paginator.markup)['inline_keyboard'] _verify_extra_buttons(keyboard[0])
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')
async def callback_paginator_for_paying(call: CallbackQuery, state: FSMContext): # print(f'callback_paginator for paying -> {str(call.from_user.id)}') await state.reset_state(with_data=False) call_data = call.data state_name = "" # print(f"call_data = {call_data}") if call_data == 'signed': page = 1 state_name = get_state_name(str(call_data)) enquiries = Enquiry(call.from_user.id) # fetching data from DB entities = enquiries.get_entities_for_paying(state_name, "Нет") await state.update_data(request_data=entities, state_name=state_name, call_data=call_data) data_state = await state.get_data() request_data = data_state.get("request_data") # print(f"request_data = {request_data}") else: data_state = await state.get_data() request_data = data_state.get("request_data") # print(f"request_data = {request_data}") page = int(call.data.split('&')[1]) # print(f'page={str(page)}') paginator = InlineKeyboardPaginator( 0 if request_data is None else len(request_data), current_page=int(page), data_pattern='page&{page}') if len(request_data) if request_data is not None else 0 > 0: if data_state.get("call_data") == 'signed': paginator.add_after( InlineKeyboardButton( text="{} Запросить выплату для всех выполненных заявок". format(emojize(":bell:")), callback_data="task_sign&{}".format(request_data[ page - 1]["id"] if len(request_data) > 0 else []))) paginator.add_after( InlineKeyboardButton("{} Назад".format( emojize(":leftwards_arrow_with_hook:")), callback_data="choice_buttons")) await call.message.edit_text(format_enquiry_for_paying( request_data[page - 1] if len(request_data) > 0 else []), reply_markup=paginator.markup)
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 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, )
def send_character_page(message, page=1): url = last_queries[str(message.chat.id)] result, meta = csapi.query_clearspending_text(url, page) if meta == None or meta["pages"] == 1: set_last_query(message, None) bot.send_message(message.chat.id, result, parse_mode="HTML") return pieces = util.split_string(result, 3000) if len(pieces) > 1: for piece in pieces[:len(pieces) - 2]: msg = bot.send_message(message.chat.id, piece, parse_mode="HTML") add_to_delete_buffer(msg) paginator = InlineKeyboardPaginator(meta["pages"], current_page=page, data_pattern='character#{page}') paginator.add_after(InlineKeyboardButton('Отмена', callback_data='back')) bot.send_message(message.chat.id, pieces[-1], reply_markup=paginator.markup, parse_mode='HTML')
async def callback_paginator(call: CallbackQuery, state: FSMContext): # print(f'callback_paginator -> {str(call.from_user.id)}') await state.reset_state(with_data=False) call_data = call.data state_name = "" # print(f"call_data = {call_data}") if call_data == 'assigned_to' or call_data == 'setup': page = 1 state_name = get_state_name(str(call_data)) enquiries = Enquiry(call.from_user.id) # enquiries = Enquiry('1771817746') # fetching data from DB entities = enquiries.get_entities(state_name) if call_data == "assigned_to": second_entities = enquiries.get_entities( get_state_name("trip_planned")) entities += second_entities await state.update_data(request_data=entities, state_name=state_name, call_data=call_data) data_state = await state.get_data() request_data = data_state.get("request_data") # print(f"request_data = {request_data}") else: data_state = await state.get_data() request_data = data_state.get("request_data") # print(f"request_data = {request_data}") page = int(call.data.split('#')[1]) # print(f'page={str(page)}') paginator = InlineKeyboardPaginator( 0 if request_data is None else len(request_data), current_page=int(page), data_pattern='page#{page}') if len(request_data) if request_data is not None else 0 > 0: if data_state.get("call_data") == 'assigned_to': # print("task_assign_to#{}".format(request_data[0]["id"])) paginator.add_after( InlineKeyboardButton( text="{} Запланировать".format(emojize(":clipboard:")), callback_data="task_to_plan#{}".format(request_data[ page - 1]["id"] if len(request_data) > 0 else []))) paginator.add_after( InlineKeyboardButton( text="{} Закрыть заявку".format(emojize(":closed_book:")), callback_data="task_close_inquire#{}".format(request_data[ page - 1]["id"] if len(request_data) > 0 else []))) if data_state.get("call_data") == 'setup': # print("task_setup#{}".format(request_data[0]["id"])) paginator.add_after( InlineKeyboardButton( text="{} Редактировать пробег".format(emojize(":car:")), callback_data="task_edit_distance#{}".format(request_data[ page - 1]["id"] if len(request_data) > 0 else []))) paginator.add_after( InlineKeyboardButton( text="{} Загрузить акт".format( emojize(":page_facing_up:")), callback_data="task_edit_act#{}".format(request_data[ page - 1]["id"] if len(request_data) > 0 else []))) paginator.add_after( InlineKeyboardButton( text="{} Загрузить УПД".format(emojize(":inbox_tray:")), callback_data="task_edit_upd#{}".format(request_data[ page - 1]["id"] if len(request_data) > 0 else []))) paginator.add_after( InlineKeyboardButton(text="{} Назад".format( emojize(":leftwards_arrow_with_hook:")), callback_data="choice_buttons")) await call.message.edit_text( format_enquiry(request_data[page - 1] if len(request_data) > 0 else []), reply_markup=paginator.markup)