reymon/easy-keyboard

一个简单的Telegram Api语法键盘构建器

2.0.2 2024-08-04 19:03 UTC

This package is auto-updated.

Last update: 2024-09-24 07:06:55 UTC


README

一个简单的Telegram Api语法键盘构建器

目录
  1. 安装
  2. 使用
    1. 定义键盘
    2. 定义按钮
    3. 将按钮绑定到键盘
      1. 按行
      2. 按按钮
      3. 按坐标
      4. 作为堆栈
    4. KeyboardForceReply 和 KeyboardHide
    5. 键盘对等类型
    6. 将Telegram键盘转换为Easy键盘

安装

使用composer安装包

composer require reymon/easy-keyboard

(回到顶部)

使用

如果您需要创建键盘,可以使用此包提供的类作为直接替换。

以下通过示例进行最佳解释

$this->sendMessage(
    peer   : 12345,
    message: 'Keyboard Example',
    replyMarkup:  KeyboardMarkup::new()
        ->singleUse()
        ->addButton(KeyboardButton::Text('Cancel'))
        ->addButton(KeyboardButton::Text('OK'))
);

通过在KeyboardMarkup上调用静态new()方法创建ReplyKeyboardMarkup。之后,每个字段,如singleUse,...添加一些额外的信息。可以通过调用addButton()方法添加按钮。我们稍后会对这个进行详细说明。

(回到顶部)

定义键盘

您可以通过在它的类上调用静态new()方法来创建键盘。

之后,您可以调用placeholder()方法来设置Bot API中可用的额外字段。

KeyboardMarkup::new()
    ->placeholder('Placeholder');

(回到顶部)

定义按钮

按钮以不同的方式创建

KeyboardButton::Phone('Send my Contact');

对于InlineButton,以相同的方式进行

InlineButton::Url('hello','https://example.com');

(回到顶部)

将按钮绑定到键盘

键盘没有按钮将无法工作,因此您需要将按钮传递给键盘。有几种方法可以做到这一点。

按行

KeyboardMarkup::new()
    ->row(
        KeyboardButton::Text('Cancel'),
        KeyboardButton::Text('OK')
    );

如果您需要多于一行,多次调用row()

KeyboardInline::new()
    ->row(
        InlineButton::Callback('1','page-1'),
        InlineButton::Callback('2','page-2'),
        InlineButton::Callback('3','page-3')
    )
    ->row(
        InlineButton::Callback('prev','page-prev'),
        InlineButton::Callback('next','page-next')
    );

您还可以以另一种方式添加回调数组或文本键盘!

KeyboardInline::new()
    ->addCallbacks([
        '1' => 'page-1',
        '2' => 'page-2',
        '3' => 'page-3',
    ],[
        'prev' => 'page-prev',
        'next' => 'page-next'
    ]);
KeyboardMarkup::new()
    ->addTexts([
       'Cancel',
       'Ok'
    ]);

您甚至可以使用这些方法

为InlineKeyboard

以及对于ReplyKeyboard

按按钮

KeyboardMarkup::new()
    ->addButton(KeyboardButton::Text('First Button'))
    ->addButton(KeyboardButton::Text('Second Button'));

如果您需要多于一行,只需不带参数调用row方法,并继续调用addButton()

KeyboardInline::new()
    ->addButton(
        InlineButton::Callback('A','answer-a'),
        InlineButton::Callback('B','answer-b')
    )
    ->row()
    ->addButton(
        InlineButton::Callback('C','answer-c'),
        InlineButton::Callback('D','answer-d')
    );

您可以选择是否像这些示例一样直接定义按钮,或者先生成整个行,然后将其传递给row方法。

您可以通过调用remove方法删除最后一个按钮,以下是一个示例

KeyboardInline::new()
    ->addButton(InlineButton::Callback('A','answer-a'))
    ->addButton(InlineButton::Callback('B','answer-b'))
    ->row()
    ->addButton(InlineButton::Callback('C','answer-c'))
    ->addButton(InlineButton::Callback('D','answer-d'))
    ->remove();

在这个示例中,按钮D将从按钮中删除。

按坐标

您可以将按钮添加到任何您想要的坐标!(注意,坐标从0开始,就像数组索引一样。)例如,假设我们有一个这样的键盘

$keyboard = KeyboardInline::new()
    ->addButton(InlineButton::Callback('Numbers','Numbers'))
    ->addButton(InlineButton::Callback('Status','Status'))
    ->row()
    ->addButton(InlineButton::Callback('Add','Add'))
    ->addButton(InlineButton::Callback('Remove','Remove'));

我们可以通过调用addToCoordinates方法添加具有指定坐标(行和列)的新按钮。这个方法会在您传入的坐标处添加新按钮,并将下一个按钮的坐标移动。这张图片显示了新按钮的位置

Screenshot_20230907_212829

$keyboard->addToCoordinates(0,1,InlineButton::Callback('Middle','Middle'));

结果应该像这张图片一样

Screenshot_20230907_213111

您也可以替换到特定的坐标,与addToCoordinates不同,replaceIntoCoordinates方法将您的按钮替换到传入的坐标。例如,如果我们想在示例中替换“添加”,如下面的图片所示

Screenshot_20230907_213957

我们应该使用这段代码

$keyboard->replaceIntoCoordinates(1,0,InlineButton::Callback('Replaced Add','Add'));

结果应该像这张图片一样

Screenshot_20230907_214232

您也可以通过坐标删除按钮,例如,如果您想在上一个示例中删除“添加”按钮,我们应该运行以下代码

$keyboard->removeFromCoordinates(1,0);

作为堆栈

如果您想要添加一些每个都有自己的行的按钮,可以使用Stack()方法。

KeyboardInline::new()
    ->Stack(
        InlineButton::Login('Login','https://example.com/login'),
        InlineButton::Url('Visit Homepage','https://example.com')
    );

您可以根据需要混合使用 row()Stack()addButton() 方法。

(回到顶部)

KeyboardForceReply 和 KeyboardHide

KeyboardForceReply 和 KeyboardHide 可以像普通键盘一样使用,但它们不接收任何按钮。

#[FilterAnd(new FilterPrivate)]
public function handleExit(Message $message) {
    $message->reply('Thank you',
        replyMarkup : KeyboardHide::new()
    );
}
$data['reply_markup'] = KeyboardForceReply::new()
    ->addButton(KeyboardButton::Text('Hello please reply'))
    ->placeholder('must reply');

(回到顶部)

键盘对等类型

我们有三种类型的对等类型可以由机器人请求:RequestUsers、RequestGroup 和 RequestChannel。

KeyboardMarkup::new()
    ->addButton(KeyboardButton::PeerUsers('Request for user', 0, bot: false));
KeyboardMarkup::new()
    ->addButton(KeyboardButton::PeerGroup('Request for chat', 1));
KeyboardMarkup::new()
    ->addButton(KeyboardButton::PeerChannel('Request for channel', 2));

您还可以使用更简单的语法来创建更好的键盘。

KeyboardMarkup::new()
    ->requestUsers('Request for user', 0);
KeyboardMarkup::new()
    ->RequestGroup('Request for chat', 1);
KeyboardMarkup::new()
    ->requestChannel('Request for broadcast', 2);

(回到顶部)

将Telegram键盘转换为Easy键盘

现在,您可以使用 tryFrom 方法轻松地将 Telegram 键盘转换为简单键盘进行修改等操作!以下是一个示例:

$easyKeyboard = Keyboard::tryFrom($replyMarkup);

如您所知,$easyKeyboard 是一个对象,您可以对它进行修改并添加更多按钮。以下是一个 $easyKeyboard 实例为 KeyboardInline 的示例。

$easyKeyboard->addButton(InlineButton::Callback('End','End'));

(回到顶部)