0

I need to implement pagination for list of entities in a telegram bot's reply keyboard.

the problem is that i need to have a show more button that loads next set of entities and i don't know how to get the page that user is currently viewing.

the next problem is how to know which list of entities is user currently viewing.

the only way that there is in my mind is to store the current state of user in database or cache it but i don't know that is there some way to add additional data to telegram reply keyboard's button or some other way to do this so i don't have to do this load of work.

Saeid Raei
  • 128
  • 2
  • 12

2 Answers2

0

You can add page number to message text or callback_data, and editMessage method is helpful.

For instance, this case using callback_data to pass current page and actions.

Sean Wei
  • 7,433
  • 1
  • 19
  • 39
  • i am not using the inline buttons . i want to use the reply keyboard buttons . i have seed bots that have this functionality without the inline button – Saeid Raei Dec 23 '17 at 20:11
  • You can 1. Record that to database, 2. Just delete previous message, and open another keyboard with new message, message text will in `.message.reply_to_message.text` – Sean Wei Dec 24 '17 at 00:40
0

I started with this model once. Maybe it's helpful for someone else:

class PaginationModel(Generic[T]):
    def __init__(self, all_items: Iterable[T] = None, items_per_page: int = 1):
        self.all_items = list(all_items or [])
        self._current_page = 0
        self.items_per_page = items_per_page

    @property
    def _begin(self):
        return self._current_page * self.items_per_page

    @property
    def _end(self):
        return min((self._current_page + 1) * self.items_per_page, self.item_count)

    @property
    def item_count(self):
        return len(self.all_items)

    @property
    def page_items(self) -> List[T]:
        return self.all_items[self._begin : self._end]

    @property
    def current_page_number(self) -> int:
        return self._current_page + 1

    @property
    def has_next_page(self) -> bool:
        return self._end < self.item_count

    @property
    def has_previous_page(self) -> bool:
        return self._begin > 0

    def flip_next_page(self):
        self._current_page += 1

    def flip_previous_page(self):
        self._current_page -= 
Joscha Götzer
  • 433
  • 4
  • 12