php-telegram-bot / fluent-keyboard
用于创建ReplyKeyboardMarkup和InlineKeyboardMarkup的Fluent键盘构建器。
Requires
- php: ^8.0
Requires (Dev)
- ext-json: *
- pestphp/pest: ^1.21
README
安装
使用composer安装包
composer require php-telegram-bot/fluent-keyboard
(返回顶部)
使用方法
如果您需要创建键盘,可以使用此包提供的类作为直接替换。
以下是一个示例来解释这一点
Request::sendMessage([ 'chat_id' => 12345, 'text' => 'Keyboard Example', 'reply_markup' => ReplyKeyboardMarkup::make() ->oneTimeKeyboard() ->button(KeyboardButton::make('Cancel')) ->button(KeyboardButton::make('OK')), ]);
通过在ReplyKeyboardMarkup上调用静态make()方法创建ReplyKeyboardMarkup。之后,每个字段,如one_time_keyboard,都可以通过在camelCase中调用它来链式调用。按钮可以通过调用button()方法来添加。我们稍后会详细介绍这一点。
类和字段命名与Telegram Bot API中的对应类型和字段一致。
(返回顶部)
定义键盘
您可以通过在其类上调用静态make()方法来创建键盘。
之后,您可以链式调用方法来设置Bot API中可用的其他字段。这是通过以camelCase调用字段名称来完成的。因此,您需要调用inputFieldPlaceholder()而不是input_field_placeholder。
ReplyKeyboardMarkup::make() ->inputFieldPlaceholder('Placeholder');
(返回顶部)
定义按钮
按钮以相同的方式创建
KeyboardButton::make() ->text('Send my Contact') ->requestContact();
作为一个快捷方式,您可以将必需的text字段作为参数传递给静态方法make(),如下所示
KeyboardButton::make('Send my Location') ->requestLocation();
对于InlineKeyboardButton也是同样的方式
InlineKeyboardButton::make('Login') ->loginUrl(['url' => 'https://example.com']);
要找出哪些字段可用,请参阅Bot API文档。
(返回顶部)
将按钮绑定到键盘
键盘没有按钮将无法工作,因此您需要将按钮传递给键盘。有几种方法可以实现这一点。
按行
ReplyKeyboardMarkup::make() ->row([ KeyboardButton::make('Cancel'), KeyboardButton::make('OK') ]);
如果您需要多于一行,多次调用row()方法
InlineKeyboardMarkup::make() ->row([ InlineKeyboardButton::make('1')->callbackData('page-1'), InlineKeyboardButton::make('2')->callbackData('page-2'), InlineKeyboardButton::make('3')->callbackData('page-3') ]) ->row([ InlineKeyboardButton::make('prev')->callbackData('page-prev'), InlineKeyboardButton::make('next')->callbackData('page-next') ]);
按按钮
ReplyKeyboardMarkup::make() ->button(KeyboardButton::make('First Button')) ->button(KeyboardButton::make('Second Button'));
如果您需要多于一行,只需不带参数调用row方法,然后继续调用button()方法
InlineKeyboardMarkup::make() ->button(InlineKeyboardButton::make('A')->callbackData('answer-a')) ->button(InlineKeyboardButton::make('B')->callbackData('answer-b')) ->row() ->button(InlineKeyboardButton::make('C')->callbackData('answer-c')) ->button(InlineKeyboardButton::make('D')->callbackData('answer-d'));
您可以根据需要直接在示例中定义按钮,或者先生成整个行,然后将变量传递给row()方法。
作为堆栈
如果您想添加一大堆每个按钮都有自己的行的按钮,可以使用stack()方法。
InlineKeyboardMarkup::make() ->stack([ InlineKeyboardButton::make('Login')->loginUrl('https://example.com/login'), InlineKeyboardButton::make('Visit Homepage')->url('https://example.com') ]);
您可以根据需要混合使用row()、stack()和button()方法。
(返回顶部)
ForceReply和ReplyKeyboardRemove
ForceReply和ReplyKeyboardRemove可以像正常键盘一样使用,但它们不接受任何按钮
$this->replyToUser('Thank you', [ 'reply_markup' => ReplyKeyboardRemove::make()->selective(), ]);
$data['reply_markup'] = ForceReply::make()->inputFieldPlaceholder('Please type something...');
(返回顶部)
KeyboardButtonPollType
request_poll字段有一点特殊。您可以通过传递KeyboardButtonPollType对象来指定用户可以创建哪种类型的投票。
KeyboardButton::make()->requestPoll(KeyboardButtonPollType::regular())
KeyboardButtonPollType
类为每种可能类型都提供了静态方法。但如果将来有新的类型,您无需等待我们发布更新。您可以直接将数组结构传递给 requestPoll()
方法,或者将数组结构传递给 KeyboardButtonPollType
的构造函数。
$pollButton = new KeyboardButtonPollType([ 'type' => 'quiz' ]);