Skip to content

EN Pagination

Andrewkydev edited this page Aug 26, 2026 · 2 revisions

Pagination

Русский: RU-Pagination

navigation.Pager builds a cross-platform inline keyboard and can own both opening a page and handling its callbacks.

pager := navigation.NewPager("inventory", 5, func(ctx *botctx.Context, page int) (string, error) {
	return fmt.Sprintf("Inventory: page %d", page), nil
})

app.Command("inventory", func(ctx *botctx.Context) error {
	return pager.Open(ctx, 1)
})

app.Callback(pager.Pattern(), pager.Handle)

What Pager does

  • Open clamps the page, invokes the renderer, and sends the page.
  • Pattern returns the namespace.* callback pattern.
  • Handle acknowledges the callback, checks ownership, renders the target page, and calls ctx.Edit.
  • If an adapter cannot edit the message, the shared Context falls back to sending a new message.
  • The current/total button uses noop and only acknowledges the callback.

Keyboard Builder

Every button is created through the shared keyboard.Builder, so callback payloads use each adapter's normal encoding path.

pager.ShowEdges = true

ShowEdges adds first/last controls next to previous/next. Duplicate targets are omitted on the second and penultimate pages.

Callback ownership

Open stores Event.UserID in every button payload. Handle does not mutate the message when another user presses a button or the owner is missing. Create the initial pager keyboard through Open instead of hand-writing its payload.

Platforms

The same Pager uses the shared ctx.Edit path:

  • Telegram — callback data and Bot API editing;
  • VK — JSON payload and message editing;
  • Discord — compact CustomID and component interaction update/edit.

Adapter tests cover the namespace/page/owner payload round trip on all three platforms. This proves synthetic mapping, while real-client behavior still requires live smoke testing.

Localization

The renderer receives the current Context, so page text can use the same request locale:

pager := navigation.NewPager("inventory", 5, func(ctx *botctx.Context, page int) (string, error) {
	return ctx.T("inventory.page", i18n.Args{"page": page})
})

Build localized custom button labels with keyboard.Builder. See Localization.

Legacy stateless API

Pager{...}.Keyboard(), Clamp(), and ParsePage(...) remain compatible for manual flows. Prefer Open + Handle when the pager owns its callback lifecycle.

Clone this wiki locally