Inline keyboard pagination for CallbackQuery
Branch: master
Clone or download
Pull request Compare This branch is 15 commits ahead of lartie:master.
Latest commit d899229 Sep 8, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.gitignore
.scrutinizer.yml Add travis and scrutinizer configs and update dependencies. Aug 19, 2017
.travis.yml Version 1.0.0 Sep 8, 2017
CHANGELOG.md Version 1.0.0 Sep 8, 2017
LICENSE git init Nov 22, 2016
README.md Version 1.0.0 Sep 8, 2017
composer.json Add travis and scrutinizer configs and update dependencies. Aug 19, 2017
composer.lock Update dependencies Sep 7, 2017
phpunit.xml.dist Move code to PHP Telegram Bot namespace and adjust all links. May 5, 2017

README.md

Telegram Bot Inline Keyboard Pagination

Scrutinizer Code Quality Codecov Build Status

Latest Stable Version Total Downloads License

Installation

Composer

composer require php-telegram-bot/inline-keyboard-pagination:^1.0.0

Usage

Test Data

$items         = range(1, 100); // required. 
$command       = 'testCommand'; // optional. Default: pagination
$selected_page = 10;            // optional. Default: 1
$labels        = [              // optional. Change button labels (showing defaults)
    'default'  => '%d',
    'first'    => '« %d',
    'previous' => '‹ %d',
    'current'  => '· %d ·',
    'next'     => '%d ›',
    'last'     => '%d »',
];

// optional. Change the callback_data format, adding placeholders for data (showing default)
$callback_data_format = 'command={COMMAND}&oldPage={OLD_PAGE}&newPage={NEW_PAGE}'

How To Use

// Define inline keyboard pagination.
$ikp = new InlineKeyboardPagination($items, $command);
$ikp->setMaxButtons(7, true); // Second parameter set to always show 7 buttons if possible.
$ikp->setLabels($labels);
$ikp->setCallbackDataFormat($callback_data_format);

// Get pagination.
$pagination = $ikp->getPagination($selected_page);

// or, in 2 steps.
$ikp->setSelectedPage($selected_page);
$pagination = $ikp->getPagination();

Now, $pagination['keyboard'] is basically a row that contains the pagination.

// Use it in your request.
if (!empty($pagination['keyboard'])) {
    //$pagination['keyboard'][0]['callback_data']; // command=testCommand&oldPage=10&newPage=1
    //$pagination['keyboard'][1]['callback_data']; // command=testCommand&oldPage=10&newPage=7
    
    ...
    $data['reply_markup' => [
        'inline_keyboard' => [
            $pagination['keyboard'],
        ],
    ];
    ...
}

To get the callback data, you can use the provided helper method (only works when using the default callback data format):

// e.g. Callback data.
$callback_data = 'command=testCommand&oldPage=10&newPage=1';

$params = InlineKeyboardPagination::getParametersFromCallbackData($callback_data);

//$params = [
//    'command' => 'testCommand',
//    'oldPage' => '10',
//    'newPage' => '1',
//];

// or, just use PHP directly if you like. (literally what the helper does!)
parse_str($callback_data, $params);

Code Quality

Run the PHPUnit tests via Composer script.

composer test

License

The MIT License (MIT). Please see License File for more information.

Project based on Telegram Bot Pagination by lartie.