mehrdadkhoddami/php-telegram-bot-inline-keyboard-pagination

自定义PHP Telegram Bot内联键盘分页

1.0.1 2017-10-18 09:20 UTC

This package is not auto-updated.

Last update: 2024-10-01 18:51:41 UTC


README

Scrutinizer Code Quality Codecov Build Status

Latest Stable Version Total Downloads License

关于

基于Telegram Bot Inline Keyboard Pagination的包

安装

Composer

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

使用方法

测试数据

$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}'

如何使用

// Define inline keyboard pagination.
$ikp = new InlineKeyboardPagination($items, $command);
$ikp->setMaxButtons(7, true); // Second parameter set to always show 7 buttons if possible.
$ikp->setRangeOffset(1); //optional: if you change offsets of selected page, you can use this method. e.g if selected page is 6 and Range offset set as 1, you will have 5 & 7 in pagination
$ikp->setLabels($labels);

/*
 *optional: But recommended. if you want that max_page will set according to labels you defined,
 * please call this method. if you remove $label elements and then call this method, max_page will be defined according to labels
 */
$ikp->setMaxButtonsBasedOnLabels();
$ikp->setCallbackDataFormat($callback_data_format);

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

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

现在,$pagination['keyboard']基本上是一行,包含分页

// 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'],
        ],
    ];
    ...
}

要获取回调数据,可以使用提供的辅助方法(仅当使用默认回调数据格式时适用)

// 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);

代码质量

通过Composer脚本来运行PHPUnit测试

composer test

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息

本项目基于Telegram Bot Pagination,由lartie开发。