An easy keyboard builder for Telegram Api syntax
Table of Contents
Install the package using composer:
composer require reymon/easy-keyboard
If you need to create a keyboard you can use the classes provided by this package as a drop-in replacement.
This is best explained with an example:
$this->sendMessage(
peer : 12345,
message: 'Keyboard Example',
replyMarkup: KeyboardMarkup::new()
->singleUse()
->addButton(KeyboardButton::Text('Cancel'))
->addButton(KeyboardButton::Text('OK'))
);
A ReplyKeyboardMarkup is created by calling the static new()
method on KeyboardMarkup
. After that every field,
like singleUse
, ... add some extras. Buttons can be added by calling
the addButton()
method. We have a detailed look on that later.
You can create a keyboard by calling the static new()
method on its class.
After that you can chain methods to set additional fields that are available in the Bot API. This is done by calling the
placeholder()
method.
KeyboardMarkup::new()
->placeholder('Placeholder');
The Buttons are created in the different way:
KeyboardButton::Phone('Send my Contact');
This is done the same way for InlineButton
:
InlineButton::Url('hello','https://example.com');
The keyboard does not work without any buttons, so you need to pass the buttons to the keyboard. There are a few ways to do this.
KeyboardMarkup::new()
->row(
KeyboardButton::Text('Cancel'),
KeyboardButton::Text('OK')
);
If you need more than one row, call row()
multiple times:
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')
);
You can add array of callbacks or texts keyboard in another way!
KeyboardInline::new()
->addCallbacks([
'1' => 'page-1',
'2' => 'page-2',
'3' => 'page-3',
],[
'prev' => 'page-prev',
'next' => 'page-next'
]);
KeyboardMarkup::new()
->addTexts([
'Cancel',
'Ok'
]);
You can even use these methods
for InlineKeyboard:
and for ReplyKeyboard:
- addText
- addTexts
- addProfile
- addWebApp
- requestPoll
- requestPollQuiz
- requestPollRegular
- requestLocation
- requestPhone
- requestUsers
- requestGroup
- requestChannel
KeyboardMarkup::new()
->addButton(KeyboardButton::Text('First Button'))
->addButton(KeyboardButton::Text('Second Button'));
If you need more than one row, just call the row method without arguments, and continue calling 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')
);
It's up to you if you define your buttons inline like in these examples or if you'd like to generate a whole row beforehand and
pass the variable to the row()
method.
You can remove the last button by calling remove
method here is an example :
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();
In this example button D will remove from buttons.
You can add button to each coordinates you want! (Note that coordinates start from 0 just like array indexes.) for example imagine we have this keyboard :
$keyboard = KeyboardInline::new()
->addButton(InlineButton::Callback('Numbers','Numbers'))
->addButton(InlineButton::Callback('Status','Status'))
->row()
->addButton(InlineButton::Callback('Add','Add'))
->addButton(InlineButton::Callback('Remove','Remove'));
we can add new button with it coordinates(raw and column) by calling addToCoordinates
method.
This methods will add new button in the coordinate that you passed and shift next buttons of the coordinates.
This picture show you the position of new button :
$keyboard->addToCoordinates(0,1,InlineButton::Callback('Middle','Middle'));
The results should like this image :
You can also replace into specific coordinates unlike addToCoordinates
the replaceIntoCoordinates
method will replace
your new button into passed coordinate for example if we want to replace Add in this example like this picture :
we should use this code :
$keyboard->replaceIntoCoordinates(1,0,InlineButton::Callback('Replaced Add','Add'));
The result should like this image :
You can also remove the button by it's coordinates for example if we want remove Add button(in last example) we should run this code:
$keyboard->removeFromCoordinates(1,0);
If you want to add a bunch of buttons that have each a row for themselves you can use the Stack()
method.
KeyboardInline::new()
->Stack(
InlineButton::Login('Login','https://example.com/login'),
InlineButton::Url('Visit Homepage','https://example.com')
);
You can mix and match the row()
, Stack()
and addButton()
methods as it fits your needs.
KeyboardForceReply and KeyboardHide can be used the same way as a normal keyboard, but they do not receive any buttons:
#[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');
We have 3 types of peer type can be requested by bots RequestUsers , RequestGroup and 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));
You can also use easier syntax to create better one
KeyboardMarkup::new()
->requestUsers('Request for user', 0);
KeyboardMarkup::new()
->RequestGroup('Request for chat', 1);
KeyboardMarkup::new()
->requestChannel('Request for broadcast', 2);
You can now easily convert telegram keyboards to easy keyboard for modify and ...
using tryFrom
methods! here is and example
$easyKeyboard = Keyboard::tryFrom($replyMarkup);
As you know $easyKeyboard
is object here and you can modify and add more buttons to it.
here is an example if $easyKeyboard
instance of KeyboardInline
$easyKeyboard->addButton(InlineButton::Callback('End','End'));